Package org.apache.james.mime4j.util

Examples of org.apache.james.mime4j.util.ByteArrayBuffer


    public void testReset() throws Exception {
        DefaultFieldBuilder builder = new DefaultFieldBuilder(0);
        builder.reset();
        builder.append(line("raw: some stuff\r\n"));
        ByteArrayBuffer buf = builder.getRaw();
        assertNotNull(buf);
        assertEquals("raw: some stuff\r\n", new String(buf.toByteArray(), "US-ASCII"));
        builder.reset();
        buf = builder.getRaw();
        assertTrue(buf.isEmpty());
        try {
            builder.build();
            fail("MimeException should have been thrown");
        } catch (MimeException expected) {
        }
View Full Code Here


            // expected
        }
    }

    public void testRemove() throws Exception {
        ByteArrayBuffer b = new ByteArrayBuffer(16);
        byte tmp[] = "--+++-".getBytes("US-ASCII");
        b.append(tmp, 0, tmp.length);

        b.remove(2, 3);
        assertEquals(3, b.length());
        assertEquals("---", new String(b.buffer(), 0, b.length(), "US-ASCII"));
        b.remove(2, 1);
        b.remove(1, 1);
        b.remove(0, 1);
        assertEquals(0, b.length());

        tmp = "+++---".getBytes("US-ASCII");
        b.append(tmp, 0, tmp.length);

        b.remove(0, 3);
        assertEquals(3, b.length());
        assertEquals("---", new String(b.buffer(), 0, b.length(), "US-ASCII"));
        b.remove(0, 3);
        assertEquals(0, b.length());

        tmp = "---+++".getBytes("US-ASCII");
        b.append(tmp, 0, tmp.length);

        b.remove(3, 3);
        assertEquals(3, b.length());
        assertEquals("---", new String(b.buffer(), 0, b.length(), "US-ASCII"));
        b.remove(0, 3);

        assertEquals(0, b.length());
    }
View Full Code Here

        assertEquals(0, b.length());
    }

    public void testInvalidRemove() throws Exception {
        ByteArrayBuffer buffer = new ByteArrayBuffer(16);
        buffer.setLength(8);
        try {
            buffer.remove(-1, 0);
            fail("IndexOutOfBoundsException should have been thrown");
        } catch (IndexOutOfBoundsException ex) {
            // expected
        }
        try {
            buffer.remove(0, -1);
            fail("IndexOutOfBoundsException should have been thrown");
        } catch (IndexOutOfBoundsException ex) {
            // expected
        }
        try {
            buffer.remove(0, 9);
            fail("IndexOutOfBoundsException should have been thrown");
        } catch (IndexOutOfBoundsException ex) {
            // expected
        }
        try {
            buffer.remove(10, 2);
            fail("IndexOutOfBoundsException should have been thrown");
        } catch (IndexOutOfBoundsException ex) {
            // expected
        }
    }
View Full Code Here

        }
        byte[] raw = outstream.toByteArray();

        BufferedLineReaderInputStream instream = new BufferedLineReaderInputStream(new ByteArrayInputStream(raw), 16);

        ByteArrayBuffer linebuf = new ByteArrayBuffer(8);
        for (String teststr : teststrs) {
            linebuf.clear();
            instream.readLine(linebuf);
            String s = new String(linebuf.toByteArray(), "US-ASCII");
            assertEquals(teststr, s);
        }
        assertEquals(-1, instream.readLine(linebuf));
        assertEquals(-1, instream.readLine(linebuf));
    }
View Full Code Here

        String teststr = "\n\n\r\n\r\r\n\n\n\n\n\n";
        byte[] raw = teststr.getBytes("US-ASCII");

        LineReaderInputStream instream = new BufferedLineReaderInputStream(new ByteArrayInputStream(raw), 4);

        ByteArrayBuffer linebuf = new ByteArrayBuffer(8);
        linebuf.clear();
        instream.readLine(linebuf);
        String s = new String(linebuf.toByteArray(), "US-ASCII");
        assertEquals("\n", s);

        linebuf.clear();
        instream.readLine(linebuf);
        s = new String(linebuf.toByteArray(), "US-ASCII");
        assertEquals("\n", s);

        linebuf.clear();
        instream.readLine(linebuf);
        s = new String(linebuf.toByteArray(), "US-ASCII");
        assertEquals("\r\n", s);

        linebuf.clear();
        instream.readLine(linebuf);
        s = new String(linebuf.toByteArray(), "US-ASCII");
        assertEquals("\r\r\n", s);

        linebuf.clear();
        instream.readLine(linebuf);
        s = new String(linebuf.toByteArray(), "US-ASCII");
        assertEquals("\n", s);

        linebuf.clear();
        instream.readLine(linebuf);
        s = new String(linebuf.toByteArray(), "US-ASCII");
        assertEquals("\n", s);

        linebuf.clear();
        instream.readLine(linebuf);
        s = new String(linebuf.toByteArray(), "US-ASCII");
        assertEquals("\n", s);

        linebuf.clear();
        instream.readLine(linebuf);
        s = new String(linebuf.toByteArray(), "US-ASCII");
        assertEquals("\n", s);

        linebuf.clear();
        instream.readLine(linebuf);
        s = new String(linebuf.toByteArray(), "US-ASCII");
        assertEquals("\n", s);

        assertEquals(-1, instream.readLine(linebuf));
        assertEquals(-1, instream.readLine(linebuf));
    }
View Full Code Here

        String teststr = "1234567890\r\n";
        byte[] raw = teststr.getBytes("US-ASCII");

        LineReaderInputStream instream1 = new BufferedLineReaderInputStream(
                new ByteArrayInputStream(raw), 1024, 13);
        ByteArrayBuffer linebuf = new ByteArrayBuffer(8);
        linebuf.clear();
        instream1.readLine(linebuf);

        LineReaderInputStream instream2 = new BufferedLineReaderInputStream(
                new ByteArrayInputStream(raw), 1024, 12);
        linebuf.clear();
        try {
            instream2.readLine(linebuf);
            fail("MaxLineLimitException should have been thrown");
        } catch (MaxLineLimitException ex) {
        }
View Full Code Here

    private final ByteArrayBuffer buf;
    private final int maxlen;

    public DefaultFieldBuilder(int maxlen) {
        this.buf = new ByteArrayBuffer(1024);
        this.maxlen = maxlen;
    }
View Full Code Here

            }
            if (this.buf.byteAt(len - 1) == '\r') {
                len --;
            }
        }
        ByteArrayBuffer copy = new ByteArrayBuffer(this.buf.buffer(), len, false);
        RawField field = RawFieldParser.DEFAULT.parseField(copy);
        String name = field.getName();
        for (int i = 0; i < name.length(); i++) {
            char ch = name.charAt(i);
            if (!FIELD_CHARS.get(ch)) {
View Full Code Here

    public void raw(InputStream is) throws MimeException, IOException {
        throw new UnsupportedOperationException("Not supported");
    }

    private static ByteSequence loadStream(InputStream in) throws IOException {
        ByteArrayBuffer bab = new ByteArrayBuffer(64);

        int b;
        while ((b = in.read()) != -1) {
            bab.append(b);
        }

        return bab;
    }
View Full Code Here

    }

    private void writeBytes(ByteSequence byteSequence, OutputStream out)
            throws IOException {
        if (byteSequence instanceof ByteArrayBuffer) {
            ByteArrayBuffer bab = (ByteArrayBuffer) byteSequence;
            out.write(bab.buffer(), 0, bab.length());
        } else {
            out.write(byteSequence.toByteArray());
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.james.mime4j.util.ByteArrayBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.