Package org.apache.http.nio.reactor

Examples of org.apache.http.nio.reactor.SessionOutputBuffer


   
    public void testChunkNoExceed() throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        WritableByteChannel channel = newChannel(baos);
        HttpParams params = new BasicHttpParams();
        SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16, params);
        HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
        ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
        encoder.write(wrap("1234"));
        encoder.complete();
       
        outbuf.flush(channel);
       
        String s = baos.toString("US-ASCII");
       
        assertTrue(encoder.isCompleted());
        assertEquals("4\r\n1234\r\n0\r\n\r\n", s);   
View Full Code Here


   
    public void testChunkExceed() throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        WritableByteChannel channel = newChannel(baos);
        HttpParams params = new BasicHttpParams();
        SessionOutputBuffer outbuf = new SessionOutputBufferImpl(16, 16, params);
        HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
        ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
       
        ByteBuffer src = wrap("0123456789ABCDEF");
       
        assertEquals(6, encoder.write(src));
        assertTrue(src.hasRemaining());
        assertEquals(10, src.remaining());

        assertEquals(6, encoder.write(src));
        assertTrue(src.hasRemaining());
        assertEquals(4, src.remaining());

        assertEquals(4, encoder.write(src));
        assertFalse(src.hasRemaining());
       
        outbuf.flush(channel);
        String s = baos.toString("US-ASCII");
        assertEquals("6\r\n012345\r\n6\r\n6789AB\r\n4\r\nCDEF\r\n", s);   
       
    }
View Full Code Here

    public void testCodingEmptyBuffer() throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        WritableByteChannel channel = newChannel(baos);
        HttpParams params = new BasicHttpParams();
        SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128, params);
        HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
        ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
       
        encoder.write(wrap("12345"));
        encoder.write(wrap("678"));
        encoder.write(wrap("90"));
       
        ByteBuffer empty = ByteBuffer.allocate(100);
        empty.flip();
        encoder.write(empty);
        encoder.write(null);
       
        encoder.complete();
       
        outbuf.flush(channel);
       
        String s = baos.toString("US-ASCII");
       
        assertTrue(encoder.isCompleted());
        assertEquals("5\r\n12345\r\n3\r\n678\r\n2\r\n90\r\n0\r\n\r\n", s);
View Full Code Here

    public void testCodingCompleted() throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        WritableByteChannel channel = newChannel(baos);
        HttpParams params = new BasicHttpParams();
        SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128, params);
        HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
        ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
       
        encoder.write(wrap("12345"));
        encoder.write(wrap("678"));
View Full Code Here

    public void testInvalidConstructor() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        WritableByteChannel channel = newChannel(baos);
        HttpParams params = new BasicHttpParams();
        SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128, params);

        try {
            new ChunkEncoder(null, null, null);
            fail("IllegalArgumentException should have been thrown");
        } catch (IllegalArgumentException ex) {
View Full Code Here

    @Test
    public void testWriteLineChunks() throws Exception {

        HttpParams params = new BasicHttpParams();
        SessionOutputBuffer outbuf = new SessionOutputBufferImpl(16, 16, params);
        SessionInputBuffer inbuf = new SessionInputBufferImpl(16, 16, params);

        ReadableByteChannel inChannel = newChannel("One\r\nTwo\r\nThree");

        inbuf.fill(inChannel);

        CharArrayBuffer line = new CharArrayBuffer(64);

        line.clear();
        Assert.assertTrue(inbuf.readLine(line, false));
        Assert.assertEquals("One", line.toString());

        outbuf.writeLine(line);

        line.clear();
        Assert.assertTrue(inbuf.readLine(line, false));
        Assert.assertEquals("Two", line.toString());

        outbuf.writeLine(line);

        line.clear();
        Assert.assertFalse(inbuf.readLine(line, false));

        inChannel = newChannel("\r\nFour");
        inbuf.fill(inChannel);

        line.clear();
        Assert.assertTrue(inbuf.readLine(line, false));
        Assert.assertEquals("Three", line.toString());

        outbuf.writeLine(line);

        inbuf.fill(inChannel);

        line.clear();
        Assert.assertTrue(inbuf.readLine(line, true));
        Assert.assertEquals("Four", line.toString());

        outbuf.writeLine(line);

        line.clear();
        Assert.assertFalse(inbuf.readLine(line, true));

        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        WritableByteChannel outChannel = newChannel(outstream);
        outbuf.flush(outChannel);

        String s = new String(outstream.toByteArray(), "US-ASCII");
        Assert.assertEquals("One\r\nTwo\r\nThree\r\nFour\r\n", s);
    }
View Full Code Here

        teststrs[3] = "";
        teststrs[4] = "And goodbye";

        HttpParams params = new BasicHttpParams();

        SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16, params);
        for (int i = 0; i < teststrs.length; i++) {
            outbuf.writeLine(teststrs[i]);
        }
        //this write operation should have no effect
        outbuf.writeLine((String)null);
        outbuf.writeLine((CharArrayBuffer)null);

        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        WritableByteChannel outChannel = newChannel(outstream);
        outbuf.flush(outChannel);

        ReadableByteChannel channel = newChannel(outstream.toByteArray());

        SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 16, params);
        inbuf.fill(channel);
View Full Code Here

    @Test
    public void testComplexReadWriteLine() throws Exception {
        HttpParams params = new BasicHttpParams();

        SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16, params);
        outbuf.write(ByteBuffer.wrap(new byte[] {'a', '\n'}));
        outbuf.write(ByteBuffer.wrap(new byte[] {'\r', '\n'}));
        outbuf.write(ByteBuffer.wrap(new byte[] {'\r', '\r', '\n'}));
        outbuf.write(ByteBuffer.wrap(new byte[] {'\n'}));

        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < 14; i++) {
            buffer.append("a");
        }
        String s1 = buffer.toString();
        buffer.append("\r\n");
        outbuf.write(ByteBuffer.wrap(buffer.toString().getBytes("US-ASCII")));

        buffer.setLength(0);
        for (int i = 0; i < 15; i++) {
            buffer.append("a");
        }
        String s2 = buffer.toString();
        buffer.append("\r\n");
        outbuf.write(ByteBuffer.wrap(buffer.toString().getBytes("US-ASCII")));

        buffer.setLength(0);
        for (int i = 0; i < 16; i++) {
            buffer.append("a");
        }
        String s3 = buffer.toString();
        buffer.append("\r\n");
        outbuf.write(ByteBuffer.wrap(buffer.toString().getBytes("US-ASCII")));

        outbuf.write(ByteBuffer.wrap(new byte[] {'a'}));

        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        WritableByteChannel outChannel = newChannel(outstream);
        outbuf.flush(outChannel);

        ReadableByteChannel channel = newChannel(outstream.toByteArray());

        SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 16, params);
        inbuf.fill(channel);
View Full Code Here

    @Test
    public void testWriteByteBuffer() throws Exception {
        byte[] pattern = "0123456789ABCDEF0123456789ABCDEF".getBytes("US-ASCII");

        HttpParams params = new BasicHttpParams();
        SessionOutputBuffer outbuf = new SessionOutputBufferImpl(4096, 1024, params);
        ReadableByteChannel src = newChannel(pattern);
        outbuf.write(src);

        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        WritableByteChannel channel = newChannel(outstream);
        while (outbuf.flush(channel) > 0) {
        }
        Assert.assertEquals(ByteBuffer.wrap(pattern), ByteBuffer.wrap(outstream.toByteArray()));
    }
View Full Code Here

    @Test
    public void testWriteFromChannel() throws Exception {
        byte[] pattern = "0123456789ABCDEF0123456789ABCDEF".getBytes("US-ASCII");

        HttpParams params = new BasicHttpParams();
        SessionOutputBuffer outbuf = new SessionOutputBufferImpl(4096, 1024, params);
        outbuf.write(ByteBuffer.wrap(pattern, 0, 16));
        outbuf.write(ByteBuffer.wrap(pattern, 16, 10));
        outbuf.write(ByteBuffer.wrap(pattern, 26, 6));

        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        WritableByteChannel channel = newChannel(outstream);
        while (outbuf.flush(channel) > 0) {
        }
        Assert.assertEquals(ByteBuffer.wrap(pattern), ByteBuffer.wrap(outstream.toByteArray()));
    }
View Full Code Here

TOP

Related Classes of org.apache.http.nio.reactor.SessionOutputBuffer

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.