Java Code Examples for java.io.PushbackInputStream#close()

The following examples show how to use java.io.PushbackInputStream#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Test.java    From native-obfuscator with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String argv []) throws IOException {
    try {
        PushbackInputStream in = new PushbackInputStream(
            new ByteArrayInputStream(new byte[] {}));
        in.close();
        in.reset();
        System.out.println("PASSED!");
    } catch (Exception e) {
        System.out.println("OK - expected IOException was thrown!");
    }
}
 
Example 2
Source File: JDBCSequenceContentStore.java    From ontopia with Apache License 2.0 5 votes vote down vote up
@Override
public int add(InputStream data, int length) throws ContentStoreException {
  try {
    // Push length onto stream
    PushbackInputStream pstream = new PushbackInputStream(data, 4);
    byte[] b = new byte[4];    
    b[3] = (byte)(length >>> 0);
    b[2] = (byte)(length >>> 8);
    b[1] = (byte)(length >>> 16);
    b[0] = (byte)(length >>> 24);
    pstream.unread(b);

    // Add new entry
    PreparedStatement ps = null;
    try {
      ps = conn.prepareStatement(sql_put, Statement.RETURN_GENERATED_KEYS);
      ps.setBinaryStream(1, pstream, length + b.length);
      ps.executeUpdate();

      ResultSet rs = ps.getGeneratedKeys();
      if (rs.next())
        return rs.getInt(1);
      else
        throw new RuntimeException("No keys were generated.");
    } finally {
      if (ps != null) ps.close();
      if (pstream != null) pstream.close();
    }
  } catch (Throwable t) {
    throw new ContentStoreException(t);
  }
}
 
Example 3
Source File: JDBCContentStore.java    From ontopia with Apache License 2.0 5 votes vote down vote up
@Override
public int add(InputStream data, int length) throws ContentStoreException {
  try {
    // Push length onto stream
    PushbackInputStream pstream = new PushbackInputStream(data, 4);
    byte[] b = new byte[4];
    b[3] = (byte)(length >>> 0);
    b[2] = (byte)(length >>> 8);
    b[1] = (byte)(length >>> 16);
    b[0] = (byte)(length >>> 24);
    pstream.unread(b);
  
    // Add new entry
    int key = generateId();
    PreparedStatement ps = null;
    try {
      ps = conn.prepareStatement(sql_put);
      ps.setInt(1, key);
      ps.setBinaryStream(2, pstream, length + b.length);
      ps.executeUpdate();
    } finally {
      if (ps != null) ps.close();
      if (pstream != null) pstream.close();
    }

    return key;
  } catch (Throwable t) {
    throw new ContentStoreException(t);
  }
}
 
Example 4
Source File: OldAndroidPushbackInputStreamTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testPushbackInputStream() throws Exception {
    String str = "AbCdEfGhIjKlM\nOpQrStUvWxYz";
    ByteArrayInputStream aa = new ByteArrayInputStream(str.getBytes());
    ByteArrayInputStream ba = new ByteArrayInputStream(str.getBytes());
    ByteArrayInputStream ca = new ByteArrayInputStream(str.getBytes());

    PushbackInputStream a = new PushbackInputStream(aa, 7);
    try {
        a.unread("push".getBytes());
        Assert.assertEquals("pushAbCdEfGhIjKlM\nOpQrStUvWxYz", read(a));
    } finally {
        a.close();
    }

    PushbackInputStream b = new PushbackInputStream(ba, 9);
    try {
        b.unread('X');
        Assert.assertEquals("XAbCdEfGhI", read(b, 10));
    } finally {
        b.close();
    }

    PushbackInputStream c = new PushbackInputStream(ca);
    try {
        Assert.assertEquals("bdfhjl\nprtvxz", skipRead(c));
    } finally {
        c.close();
    }
}