@Test
 public void bufferedSinkFlush() throws IOException {
   Buffer sink = new Buffer();
   BufferedSink bufferedSink = new RealBufferedSink(sink);
   bufferedSink.writeByte('a');
   assertEquals(0, sink.size());
   bufferedSink.flush();
   assertEquals(0, bufferedSink.buffer().size());
   assertEquals(1, sink.size());
 }
 @Test
 public void bufferedSinkEmitsTailWhenItIsComplete() throws IOException {
   Buffer sink = new Buffer();
   BufferedSink bufferedSink = new RealBufferedSink(sink);
   bufferedSink.writeUtf8(repeat('a', Segment.SIZE - 1));
   assertEquals(0, sink.size());
   bufferedSink.writeByte(0);
   assertEquals(Segment.SIZE, sink.size());
   assertEquals(0, bufferedSink.buffer().size());
 }
 @Test
 public void bytesNotEmittedToSinkWithoutFlush() throws Exception {
   Buffer sink = new Buffer();
   BufferedSink bufferedSink = new RealBufferedSink(sink);
   bufferedSink.writeUtf8("abc");
   assertEquals(0, sink.size());
 }
 @Test
 public void incompleteSegmentsNotEmitted() throws Exception {
   Buffer sink = new Buffer();
   BufferedSink bufferedSink = new RealBufferedSink(sink);
   bufferedSink.writeUtf8(repeat('a', Segment.SIZE * 3 - 1));
   assertEquals(Segment.SIZE * 2, sink.size());
 }
 @Test
 public void bufferedSinkEmitMultipleSegments() throws IOException {
   Buffer sink = new Buffer();
   BufferedSink bufferedSink = new RealBufferedSink(sink);
   bufferedSink.writeUtf8(repeat('a', Segment.SIZE * 4 - 1));
   assertEquals(Segment.SIZE * 3, sink.size());
   assertEquals(Segment.SIZE - 1, bufferedSink.buffer().size());
 }
Example #6
0
 @Test
 public void readExhaustedSource() throws Exception {
   Buffer sink = new Buffer();
   sink.writeUtf8(repeat('a', 10));
   assertEquals(-1, source.read(sink, 10));
   assertEquals(10, sink.size());
   assertTrue(source.exhausted());
 }
Example #7
0
  @Test
  public void readZeroBytesFromSource() throws Exception {
    Buffer sink = new Buffer();
    sink.writeUtf8(repeat('a', 10));

    // Either 0 or -1 is reasonable here. For consistency with Android's
    // ByteArrayInputStream we return 0.
    assertEquals(-1, source.read(sink, 0));
    assertEquals(10, sink.size());
    assertTrue(source.exhausted());
  }
Example #8
0
  @Test
  public void readFullyByteArray() throws IOException {
    Buffer data = new Buffer();
    data.writeUtf8("Hello").writeUtf8(repeat('e', Segment.SIZE));

    byte[] expected = data.clone().readByteArray();
    sink.write(data, data.size());

    byte[] sink = new byte[Segment.SIZE + 5];
    source.readFully(sink);
    assertByteArraysEquals(expected, sink);
  }
  @Test
  public void writeAllWritesOneSegmentAtATime() throws IOException {
    Buffer write1 = new Buffer().writeUtf8(TestUtil.repeat('a', Segment.SIZE));
    Buffer write2 = new Buffer().writeUtf8(TestUtil.repeat('b', Segment.SIZE));
    Buffer write3 = new Buffer().writeUtf8(TestUtil.repeat('c', Segment.SIZE));

    Buffer source =
        new Buffer()
            .writeUtf8(
                ""
                    + TestUtil.repeat('a', Segment.SIZE)
                    + TestUtil.repeat('b', Segment.SIZE)
                    + TestUtil.repeat('c', Segment.SIZE));

    MockSink mockSink = new MockSink();
    BufferedSink bufferedSink = Okio.buffer(mockSink);
    assertEquals(Segment.SIZE * 3, bufferedSink.writeAll(source));

    mockSink.assertLog(
        "write(" + write1 + ", " + write1.size() + ")",
        "write(" + write2 + ", " + write2.size() + ")",
        "write(" + write3 + ", " + write3.size() + ")");
  }
Example #10
0
  @Test
  public void sinkFromOutputStream() throws Exception {
    Buffer data = new Buffer();
    data.writeUtf8("a");
    data.writeUtf8(repeat('b', 9998));
    data.writeUtf8("c");

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Sink sink = Okio.sink(out);
    sink.write(data, 3);
    assertEquals("abb", out.toString("UTF-8"));
    sink.write(data, data.size());
    assertEquals("a" + repeat('b', 9998) + "c", out.toString("UTF-8"));
  }
Example #11
0
  @Test
  public void sourceFromInputStream() throws Exception {
    InputStream in =
        new ByteArrayInputStream(("a" + repeat('b', Segment.SIZE * 2) + "c").getBytes(UTF_8));

    // Source: ab...bc
    Source source = Okio.source(in);
    Buffer sink = new Buffer();

    // Source: b...bc. Sink: abb.
    assertEquals(3, source.read(sink, 3));
    assertEquals("abb", sink.readUtf8(3));

    // Source: b...bc. Sink: b...b.
    assertEquals(Segment.SIZE, source.read(sink, 20000));
    assertEquals(repeat('b', Segment.SIZE), sink.readUtf8(sink.size()));

    // Source: b...bc. Sink: b...bc.
    assertEquals(Segment.SIZE - 1, source.read(sink, 20000));
    assertEquals(repeat('b', Segment.SIZE - 2) + "c", sink.readUtf8(sink.size()));

    // Source and sink are empty.
    assertEquals(-1, source.read(sink, 1));
  }
Example #12
0
 public final void write(Buffer source, long byteCount) throws IOException {
   Util.checkOffsetAndCount(source.size, 0, byteCount);
   while (byteCount > 0) {
     Segment head = source.head;
     int toDeflate = (int) Math.min(byteCount, (long) (head.limit - head.pos));
     this.deflater.setInput(head.data, head.pos, toDeflate);
     deflate(false);
     source.size -= (long) toDeflate;
     head.pos += toDeflate;
     if (head.pos == head.limit) {
       source.head = head.pop();
       SegmentPool.recycle(head);
     }
     byteCount -= (long) toDeflate;
   }
 }
Example #13
0
 @IgnoreJRERequirement
 private void deflate(boolean syncFlush) throws IOException {
   Buffer buffer = this.sink.buffer();
   while (true) {
     Segment s = buffer.writableSegment(1);
     int deflated =
         syncFlush
             ? this.deflater.deflate(s.data, s.limit, 2048 - s.limit, 2)
             : this.deflater.deflate(s.data, s.limit, 2048 - s.limit);
     if (deflated > 0) {
       s.limit += deflated;
       buffer.size += (long) deflated;
       this.sink.emitCompleteSegments();
     } else if (this.deflater.needsInput()) {
       break;
     }
   }
   if (s.pos == s.limit) {
     buffer.head = s.pop();
     SegmentPool.recycle(s);
   }
 }
Example #14
0
  public void testLegacy() {
    // 1. Tests of old methods.
    Buffer buf = new Buffer(4);
    Iterator iter = null;

    assertEquals(4, buf.getCapacity());
    assertEquals(0, buf.getSize());
    iter = buf.iterator();
    assertTrue(!iter.hasNext());
    try {
      iter.next();
      assertTrue(false);
    } catch (NoSuchElementException e) {
      assertTrue(true);
    }

    buf.add("test");
    assertEquals(1, buf.getSize());
    iter = buf.iterator();
    assertTrue(iter.hasNext());
    assertEquals("test", iter.next());
    assertTrue(!iter.hasNext());
    try {
      iter.next();
      assertTrue(false);
    } catch (NoSuchElementException e) {
    }

    buf.add("test2");
    assertEquals(2, buf.getSize());
    buf.add("test3");
    assertEquals(3, buf.getSize());
    iter = buf.iterator();
    assertTrue(iter.hasNext());
    assertEquals("test3", iter.next());
    assertTrue(iter.hasNext());
    assertEquals("test2", iter.next());
    assertTrue(iter.hasNext());
    assertEquals("test", iter.next());
    assertTrue(!iter.hasNext());

    buf.add("test4");
    assertEquals(4, buf.getSize());
    buf.add("test5");
    assertEquals(4, buf.getSize());
    iter = buf.iterator();
    assertTrue(iter.hasNext());
    assertEquals("test5", iter.next());
    assertTrue(iter.hasNext());
    assertEquals("test4", iter.next());
    assertTrue(iter.hasNext());
    assertEquals("test3", iter.next());
    assertTrue(iter.hasNext());
    assertEquals("test2", iter.next());
    assertTrue(!iter.hasNext());

    buf.add("test6");
    assertEquals(4, buf.getSize());

    // 2.  Tests of new methods.  These are definitely not sufficient.
    buf = new Buffer(4);
    assertEquals(0, buf.getSize());
    assertNull(buf.addLast("a"));
    assertEquals(1, buf.getSize());
    assertNull(buf.addLast("b"));
    assertEquals(2, buf.getSize());
    assertNull(buf.addLast("c"));
    assertEquals(3, buf.getSize());
    assertNull(buf.addLast("d"));
    assertEquals(4, buf.getSize());
    assertEquals("a", buf.first());
    assertEquals("a", buf.removeFirst());
    assertEquals(3, buf.getSize());
    assertEquals("b", buf.first());
    assertEquals("b", buf.removeFirst());
    assertEquals(2, buf.getSize());
    assertNull(buf.addFirst("b"));
    assertEquals(3, buf.getSize());
    assertNull(buf.addFirst("a"));
    assertEquals(4, buf.getSize());
    // buf=[a b c d]

    assertEquals("a", buf.addLast("e"));
    // buf=[b c d e]
    assertEquals("e", buf.last());
    assertEquals("b", buf.first());
    assertEquals("e", buf.removeLast());
    assertEquals("d", buf.removeLast());

    buf = new Buffer(4);
    iter = buf.iterator();
    buf.addFirst("a");
    try {
      iter.hasNext();
      assertTrue(false);
    } catch (ConcurrentModificationException e) {
      assertTrue(true);
    }

    buf = new Buffer(4);
    buf.addFirst("a");
    buf.addLast("b");
    buf.clear();
    assertEquals(0, buf.getSize());
    iter = buf.iterator();
    assertTrue(!iter.hasNext());

    // 3. Tests of get and set.
    buf = new Buffer(3);
    try {
      buf.get(0);
      assertTrue(false);
    } catch (IndexOutOfBoundsException e) {
    }
    try {
      buf.get(-1);
      assertTrue(false);
    } catch (IndexOutOfBoundsException e) {
    }

    buf.addLast("a");
    buf.addLast("b");
    buf.addLast("c");
    buf.addLast("d"); // clobbers a!
    assertEquals("b", buf.get(0));
    assertEquals("c", buf.get(1));
    assertEquals("d", buf.get(2));
    buf.set(2, "bb");
    assertEquals("bb", buf.get(2));

    // 4. Tests of remove and removeAll methods.
    buf = new Buffer(4);
    buf.addLast("a");
    buf.addLast("b");
    buf.addLast("c");
    buf.addLast("d");
    try {
      buf.remove(-1);
      assertTrue(false);
    } catch (IndexOutOfBoundsException e) {
    }
    assertEquals("a", (buf.remove(0)));
    assertEquals(3, buf.size());
    assertEquals("b", buf.get(0));
    assertEquals("c", buf.get(1));
    assertEquals("d", buf.get(2));

    buf = new Buffer(4);
    buf.addLast("x");
    buf.addLast("y");
    buf.addLast("a");
    buf.addLast("b");
    buf.addLast("c");
    buf.addLast("d");
    try {
      buf.remove(5);
      assertTrue(false);
    } catch (IndexOutOfBoundsException e) {
    }
    assertEquals("a", (buf.remove(0)));
    assertEquals(3, buf.size());
    assertEquals("b", buf.get(0));
    assertEquals("c", buf.get(1));
    assertEquals("d", buf.get(2));

    buf = new Buffer(4);
    buf.addLast("a");
    buf.addLast("b");
    buf.addLast("c");
    buf.addLast("d");
    try {
      buf.remove(5);
      assertTrue(false);
    } catch (IndexOutOfBoundsException e) {
    }
    assertEquals("d", buf.remove(3));
    assertEquals(3, buf.size());
    assertEquals("a", buf.get(0));
    assertEquals("b", buf.get(1));
    assertEquals("c", buf.get(2));

    buf = new Buffer(4);
    buf.addLast("a");
    buf.addLast("b");
    buf.addLast("c");
    buf.addLast("d");
    try {
      buf.remove(5);
      assertTrue(false);
    } catch (IndexOutOfBoundsException e) {
    }
    assertEquals("b", buf.remove(1));
    assertEquals(3, buf.size());
    assertEquals("a", buf.get(0));
    assertEquals("c", buf.get(1));
    assertEquals("d", buf.get(2));

    buf = new Buffer(4);
    buf.addLast("b");
    buf.addLast("b");
    buf.addLast("c");
    buf.addLast("d");
    assertTrue(buf.remove("d"));
    assertTrue(buf.remove("b"));
    assertEquals(2, buf.size());
    assertEquals("b", buf.get(0));
    assertEquals("c", buf.get(1));

    buf = new Buffer(4);
    buf.addLast("b");
    buf.addLast("b");
    buf.addLast("c");
    buf.addLast("b");
    assertTrue(buf.removeAll("b"));
    assertEquals(1, buf.size());
    assertEquals("c", buf.get(0));

    // 5. Test clone() method.
    buf = new Buffer(2);
    buf.addLast("a");
    buf.addLast("b");
    Buffer buf2 = new Buffer(buf);
    assertEquals(buf2.size(), buf.size());
    assertEquals(buf2.getCapacity(), buf.getCapacity());
    assertEquals(buf2.first(), buf.first());
    assertEquals(buf2.last(), buf.last());

    assertNotNull(buf.removeFirst()); // buf2 unmodified
    assertEquals(1, buf.size());
    assertEquals(2, buf2.size());
    assertEquals("b", buf.first());
    assertEquals("a", buf2.first());
    assertEquals("b", buf2.last());
  }
 private void doWriteToSocket() throws IOException {
   writeNumber(sockout, buf.size());
   sockout.write(' ');
   buf.writeTo(sockout);
   sockout.flush();
 }