Exemplo n.º 1
0
 @Test
 public void testCopy() {
   AsciiInputStream in = new AsciiInputStream("input");
   StringOutputStream out = new StringOutputStream();
   try {
     StreamUtil.copy(in, out);
   } catch (IOException ioex) {
     fail("StreamUtil.copy " + ioex.toString());
   }
   assertEquals("input", out.toString());
   StreamUtil.close(out);
   StreamUtil.close(in);
 }
Exemplo n.º 2
0
  @Test
  public void testCompare() {
    try {
      File file = new File(dataRoot, "file/a.txt");
      FileInputStream in1 = new FileInputStream(file);

      String content = "test file\r\n";
      if (file.length() == 10) {
        content = StringUtil.remove(content, '\r');
      }
      AsciiInputStream in2 = new AsciiInputStream(content);
      assertTrue(StreamUtil.compare(in1, in2));
      StreamUtil.close(in2);
      StreamUtil.close(in1);
    } catch (FileNotFoundException e) {
      fail("StreamUtil.testCloneCompare " + e.toString());
    } catch (IOException e) {
      fail("StreamUtil.testCloneCompare " + e.toString());
    }
  }
Exemplo n.º 3
0
 /** Verify that using a ByteArrayInputStream does not allocate a new byte array. */
 @Test
 public void testByteArrayInputStream() throws Exception {
   byte[] bytes = new byte[8];
   InputStream input = new ByteArrayInputStream(bytes);
   try {
     byte[] bytesRead = StreamUtil.getBytesFromStream(input);
     assertTrue(Arrays.equals(bytes, bytesRead));
   } finally {
     Closeables.close(input, true);
   }
 }
Exemplo n.º 4
0
 /** Verify that using an offset with ByteArrayInputStream still produces correct output. */
 @Test
 public void testByteArrayInputStreamWithOffset() throws Exception {
   byte[] bytes = new byte[] {0, 1, 2, 3, 4};
   InputStream input = new ByteArrayInputStream(bytes, 1, 4);
   try {
     byte[] bytesRead = StreamUtil.getBytesFromStream(input);
     byte[] expectedBytes = new byte[] {1, 2, 3, 4};
     assertTrue(Arrays.equals(expectedBytes, bytesRead));
   } finally {
     Closeables.close(input, true);
   }
 }
Exemplo n.º 5
0
  @Test
  public void testGetBytes() {
    try {
      FileInputStream in = new FileInputStream(new File(dataRoot, "file/a.txt"));
      byte[] data = StreamUtil.readBytes(in);
      StreamUtil.close(in);

      String s = new String(data);
      s = StringUtil.remove(s, '\r');
      assertEquals("test file\n", s);

      in = new FileInputStream(new File(dataRoot, "file/a.txt"));
      String str = new String(StreamUtil.readChars(in));
      StreamUtil.close(in);
      str = StringUtil.remove(str, '\r');
      assertEquals("test file\n", str);
    } catch (FileNotFoundException e) {
      fail("StreamUtil.testGetBytes " + e.toString());
    } catch (IOException e) {
      fail("StreamUtil.testGetBytes " + e.toString());
    }
  }
Exemplo n.º 6
0
 @Test
 public void testSuccessfulSkip() throws Exception {
   InputStream inputStream = mock(InputStream.class);
   when(inputStream.skip(anyLong())).thenReturn(2L);
   assertEquals(10, StreamUtil.skip(inputStream, 10));
   InOrder order = inOrder(inputStream);
   order.verify(inputStream).skip(10);
   order.verify(inputStream).skip(8);
   order.verify(inputStream).skip(6);
   order.verify(inputStream).skip(4);
   order.verify(inputStream).skip(2);
   verifyNoMoreInteractions(inputStream);
 }
Exemplo n.º 7
0
 @Test
 public void testUnsuccessfulSkip() throws Exception {
   InputStream inputStream = mock(InputStream.class);
   when(inputStream.skip(anyLong())).thenReturn(3L, 5L, 0L, 6L, 0L);
   when(inputStream.read()).thenReturn(3, -1);
   assertEquals(15, StreamUtil.skip(inputStream, 20));
   InOrder order = inOrder(inputStream);
   order.verify(inputStream).skip(20);
   order.verify(inputStream).skip(17);
   order.verify(inputStream).skip(12);
   order.verify(inputStream).read();
   order.verify(inputStream).skip(11);
   order.verify(inputStream).skip(5);
   order.verify(inputStream).read();
   verifyNoMoreInteractions(inputStream);
 }
Exemplo n.º 8
0
  private void checkFileInputStream(int size) throws IOException {
    byte[] bytesToWrite = new byte[size];
    for (int i = 0; i < size; i++) {
      bytesToWrite[i] = (byte) i; // It's okay to truncate
    }

    File tmpFile = File.createTempFile("streamUtil", "test");
    InputStream input = null;
    OutputStream output = null;
    try {
      output = new FileOutputStream(tmpFile);
      output.write(bytesToWrite);
      output.close();

      input = new FileInputStream(tmpFile);
      byte[] bytesRead = StreamUtil.getBytesFromStream(input);
      assertTrue(Arrays.equals(bytesToWrite, bytesRead));
    } finally {
      Closeables.close(input, true);
      Closeables.close(output, false);
      assertTrue(tmpFile.delete());
    }
  }