Exemplo n.º 1
0
  @Test
  public void testDecodingFileWithBufferedSessionData() throws Exception {
    final ReadableByteChannel channel =
        new ReadableByteChannelMock(
            new String[] {"stuff; ", "more stuff; ", "a lot more stuff!"}, Consts.ASCII);

    final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, Consts.ASCII);
    final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
    final IdentityDecoder decoder = new IdentityDecoder(channel, inbuf, metrics);

    final int i = inbuf.fill(channel);
    Assert.assertEquals(7, i);

    createTempFile();
    final RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw");
    try {
      final FileChannel fchannel = testfile.getChannel();
      long pos = 0;
      while (!decoder.isCompleted()) {
        final long bytesRead = decoder.transfer(fchannel, pos, 10);
        if (bytesRead > 0) {
          pos += bytesRead;
        }
      }

      // count everything except the initial 7 bytes that went to the session buffer
      Assert.assertEquals(testfile.length() - 7, metrics.getBytesTransferred());
    } finally {
      testfile.close();
    }
    Assert.assertEquals(
        "stuff; more stuff; a lot more stuff!", CodecTestUtils.readFromFile(this.tmpfile));
  }
Exemplo n.º 2
0
  @Test
  public void testDecodingFromSessionBuffer() throws Exception {
    final ReadableByteChannel channel =
        new ReadableByteChannelMock(new String[] {"stuff;", "more stuff"}, Consts.ASCII);

    final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, Consts.ASCII);
    final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();

    inbuf.fill(channel);

    Assert.assertEquals(6, inbuf.length());

    final IdentityDecoder decoder = new IdentityDecoder(channel, inbuf, metrics);

    final ByteBuffer dst = ByteBuffer.allocate(1024);

    int bytesRead = decoder.read(dst);
    Assert.assertEquals(6, bytesRead);
    Assert.assertEquals("stuff;", CodecTestUtils.convert(dst));
    Assert.assertFalse(decoder.isCompleted());
    Assert.assertEquals(0, metrics.getBytesTransferred()); // doesn't count if from session buffer

    dst.clear();
    bytesRead = decoder.read(dst);
    Assert.assertEquals(10, bytesRead);
    Assert.assertEquals("more stuff", CodecTestUtils.convert(dst));
    Assert.assertFalse(decoder.isCompleted());
    Assert.assertEquals(10, metrics.getBytesTransferred());

    dst.clear();
    bytesRead = decoder.read(dst);
    Assert.assertEquals(-1, bytesRead);
    Assert.assertTrue(decoder.isCompleted());
    Assert.assertEquals(10, metrics.getBytesTransferred());

    dst.clear();
    bytesRead = decoder.read(dst);
    Assert.assertEquals(-1, bytesRead);
    Assert.assertTrue(decoder.isCompleted());
    Assert.assertEquals(10, metrics.getBytesTransferred());
  }