Example usage for org.apache.commons.io.input StringInputStream StringInputStream

List of usage examples for org.apache.commons.io.input StringInputStream StringInputStream

Introduction

In this page you can find the example usage for org.apache.commons.io.input StringInputStream StringInputStream.

Prototype

public StringInputStream(String data) 

Source Link

Usage

From source file:org.lockss.util.TestStreamUtil.java

public void testCopyNullOutputStream() throws IOException {
    InputStream is = new StringInputStream("test string");
    assertEquals(0, StreamUtil.copy(is, null));
    is.close();/*from ww w  .ja v a 2 s  .c om*/
}

From source file:org.lockss.util.TestStreamUtil.java

public void testCopyInputStream() throws IOException {
    InputStream is = new StringInputStream("test string");
    OutputStream baos = new ByteArrayOutputStream(11);
    StreamUtil.copy(is, baos);//from w w  w .j  a v a  2s.  com
    is.close();
    String resultStr = baos.toString();
    baos.close();
    assertTrue(resultStr.equals("test string"));
}

From source file:org.lockss.util.TestStreamUtil.java

public void testCopyInputStreamReturnsCount() throws IOException {
    InputStream is = new StringInputStream("test string");
    OutputStream baos = new ByteArrayOutputStream(11);
    assertEquals(11, StreamUtil.copy(is, baos));
    is.close();//w ww .j a  v a2s .c  o  m
    baos.close();
}

From source file:org.lockss.util.TestStreamUtil.java

public void testCopyInputStreamLength() throws IOException {
    InputStream is = new StringInputStream("012345678901234567890");
    OutputStream baos = new ByteArrayOutputStream(20);
    assertEquals(5, StreamUtil.copy(is, baos, 5));
    assertEquals("01234", baos.toString());
    assertEquals(5, StreamUtil.copy(is, baos, 5));
    assertEquals("0123456789", baos.toString());
    assertEquals(5, StreamUtil.copy(is, baos, 5));
    assertEquals("012345678901234", baos.toString());
    StreamUtil.copy(is, baos, 5);//from w w  w  . j  a  v a2s.  c om
    assertEquals("01234567890123456789", baos.toString());
    is.close();
    baos.close();

    is = new StringInputStream("01234567890123456789012345");
    baos = new ByteArrayOutputStream(2);
    assertEquals(2, StreamUtil.copy(is, baos, 2));
    assertEquals("01", baos.toString());
    baos = new ByteArrayOutputStream(5);
    assertEquals(5, StreamUtil.copy(is, baos, 5));
    assertEquals("23456", baos.toString());
    baos = new ByteArrayOutputStream(7);
    assertEquals(7, StreamUtil.copy(is, baos, 7));
    assertEquals("7890123", baos.toString());
    baos = new ByteArrayOutputStream(7);
    assertEquals(12, StreamUtil.copy(is, baos, -1));
    assertEquals("456789012345", baos.toString());
    is.close();
    baos.close();
}

From source file:org.lockss.util.TestStreamUtil.java

public void testCopyExceptionWrapping() throws IOException {
    InputStream is;//from   w  w w .java 2 s  .co  m
    OutputStream os;

    is = new ThrowingInputStream(new StringInputStream("test string"), e1, null);
    os = new NullOutputStream();
    try {
        StreamUtil.copy(is, os, -1, null, false);
        fail("StreamUtil.copy(ThrowingInputStream) didn't throw on read");
    } catch (StreamUtil.InputException e) {
        fail("StreamUtil.copy(ThrowingInputStream) shouldn't have wrapped exception");
    } catch (IOException e) {
        assertSame(e1, e);
    }
    is = new ThrowingInputStream(new StringInputStream("test string"), e1, null);
    try {
        StreamUtil.copy(is, os, -1, null, true);
        fail("StreamUtil.copy(ThrowingInputStream) didn't throw on read");
    } catch (StreamUtil.InputException e) {
        assertSame(e1, e.getCause());
    }
    is = new StringInputStream("test string");
    os = new ThrowingOutputStream(new NullOutputStream(), e2, null);
    try {
        StreamUtil.copy(is, os, -1, null, false);
        fail("StreamUtil.copy(ThrowingOutputStream) didn't throw on write");
    } catch (StreamUtil.OutputException e) {
        fail("StreamUtil.copy(ThrowingOutputStream) shouldn't have wrapped exception");
    } catch (IOException e) {
        assertSame(e2, e);
    }
    is = new StringInputStream("test string");
    os = new ThrowingOutputStream(new NullOutputStream(), e2, null);
    try {
        StreamUtil.copy(is, os, -1, null, true);
        fail("StreamUtil.copy(ThrowingOutputStream) didn't throw on write");
    } catch (StreamUtil.OutputException e) {
        assertSame(e2, e.getCause());
    }
}

From source file:org.lockss.util.TestStreamUtil.java

public void testReadBuf() throws IOException {
    int len = 12;
    byte[] buf = new byte[len];
    InputStream ins = new StringInputStream("01\0003456789abcdefghijklmnopq");
    StreamUtil.readBytes(ins, buf, len);
    byte[] exp = { '0', '1', 0, '3', '4', '5', '6', '7', '8', '9', 'a', 'b' };
    assertEquals(exp, buf);/*from   w w w.j  a  va2s . c o  m*/
}

From source file:org.lockss.util.TestStreamUtil.java

public void testCompareStreams() throws IOException {
    String s1 = "01\0003456789abcdefghijklmnopq";
    assertTrue(StreamUtil.compare(new StringInputStream(""), new StringInputStream("")));
    assertTrue(StreamUtil.compare(new StringInputStream(s1), new StringInputStream(s1)));
    assertFalse(StreamUtil.compare(new StringInputStream(s1), new StringInputStream("")));
    assertFalse(StreamUtil.compare(new StringInputStream(""), new StringInputStream(s1)));
    assertFalse(StreamUtil.compare(new StringInputStream(s1), new StringInputStream(s1 + "a")));
    assertFalse(StreamUtil.compare(new StringInputStream(s1 + "a"), new StringInputStream(s1)));
    assertFalse(StreamUtil.compare(new StringInputStream("foo"), new StringInputStream("bar")));
}

From source file:org.lockss.util.TestStreamUtil.java

public void testGetUncompressedInputStreamDeflate() throws IOException {
    // Currently InflaterInputStream constructor doesn't care what it gets.
    // If that changes this will have to create a deflated stream.
    InputStream dfin = new StringInputStream("foo");
    assertClass(InflaterInputStream.class, StreamUtil.getUncompressedInputStream(dfin, "deflate"));
    assertClass(InflaterInputStream.class, StreamUtil.getUncompressedInputStream(dfin, "Deflate"));
}

From source file:org.lockss.util.TestStreamUtil.java

public void testGetUncompressedInputStreamIdentity() throws IOException {
    InputStream in = new StringInputStream("foo");
    assertSame(in, StreamUtil.getUncompressedInputStream(in, "identity"));
    assertSame(in, StreamUtil.getUncompressedInputStream(in, "IDENTITY"));
    assertSame(in, StreamUtil.getUncompressedInputStream(in, null));
    assertSame(in, StreamUtil.getUncompressedInputStream(in, ""));
}

From source file:org.lockss.util.TestStreamUtil.java

public void testGetReader() {
    InputStream in = new StringInputStream("123");
    InputStreamReader rdr;/*w  ww  .j a  v a 2s .  co  m*/
    Charset def = Charset.forName(Constants.DEFAULT_ENCODING);
    Charset utf = Charset.forName("UTF-8");
    rdr = (InputStreamReader) StreamUtil.getReader(in, null);
    assertTrue(def.aliases().contains(rdr.getEncoding()));
    rdr = (InputStreamReader) StreamUtil.getReader(in, "ISO-8859-1");
    assertTrue(def.aliases().contains(rdr.getEncoding()));
    rdr = (InputStreamReader) StreamUtil.getReader(in, "utf-8");
    assertTrue(utf.aliases().contains(rdr.getEncoding()));
    rdr = (InputStreamReader) StreamUtil.getReader(in, "NoSuchCharset");
    assertTrue(def.aliases().contains(rdr.getEncoding()));
}