Java Code Examples for java.io.BufferedInputStream

The following are top voted examples for showing how to use java.io.BufferedInputStream. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to product more good examples.

Example 1
Project: jentrata-msh   File: AS2Message.java View source code Vote up 6 votes
/**
 * Creates a new instance of AS2Message.
 * 
 * @param message the message as input stream.
 * @throws AS2MessageException if unable to construct from the given input stream.
 */
public AS2Message(InputStream message) throws AS2MessageException {
    try {
        BufferedInputStream bis= new BufferedInputStream(message);
        load(new InternetHeaders(bis), bis);
        bis.close();
    } catch (Exception e) {
        throw new AS2MessageException(
                "Unable to construct AS2 message from input stream", e);
    }
}
 
Example 2
Project: aws-toolkit-eclipse   File: RemoteCommandUtils.java View source code Vote up 6 votes
/**
 * Reads all available data from the specified input stream and writes it to
 * the specified StringBuiler.
 *
 * @param in
 *            The InputStream to read.
 * @param builder
 *            The StringBuilder to write to.
 *
 * @throws IOException
 *             If there were any problems reading from the specified
 *             InputStream.
 */
private void drainInputStream(InputStream in, StringBuilder builder) throws IOException {
    BufferedInputStream bufferedInputStream = new BufferedInputStream(in);

    byte[] buffer = new byte[1024];

    while (bufferedInputStream.available() > 0) {
        int read = bufferedInputStream.read(buffer, 0, buffer.length);

        if (read > 0) {
            builder.append(new String(buffer, 0, read));
        }
    }
}
 
Example 3
Project: phonegap-geolocation   File: Connection.java View source code Vote up 6 votes
/**
 * Returns true if we are confident that we can read data from this
 * connection. This is more expensive and more accurate than [email protected]
 * #isAlive()}; callers should check [email protected] #isAlive()} first.
 */
public boolean isReadable() {
  if (!(in instanceof BufferedInputStream)) {
    return true; // Optimistic.
  }
  if (isSpdy()) {
    return true; // Optimistic. We can't test SPDY because its streams are in use.
  }
  BufferedInputStream bufferedInputStream = (BufferedInputStream) in;
  try {
    int readTimeout = socket.getSoTimeout();
    try {
      socket.setSoTimeout(1);
      bufferedInputStream.mark(1);
      if (bufferedInputStream.read() == -1) {
        return false; // Stream is exhausted; socket is closed.
      }
      bufferedInputStream.reset();
      return true;
    } finally {
      socket.setSoTimeout(readTimeout);
    }
  } catch (SocketTimeoutException ignored) {
    return true; // Read timed out; socket is good.
  } catch (IOException e) {
    return false; // Couldn't read; socket is closed.
  }
}
 
Example 4
Project: JerseyTest   File: EntityHolderReader.java View source code Vote up 6 votes
public Object readFrom(
        Class<Object> type,
        Type genericType, 
        Annotation annotations[],
        MediaType mediaType, 
        MultivaluedMap<String, String> httpHeaders, 
        InputStream entityStream) throws IOException {

    if (!entityStream.markSupported()) {
        entityStream = new BufferedInputStream(entityStream, ReaderWriter.BUFFER_SIZE);
    }
    entityStream.mark(1);
    if (entityStream.read() == -1) {
        return new EntityHolder();
    }

    entityStream.reset();

    final ParameterizedType pt = (ParameterizedType)genericType;
    final Type t = pt.getActualTypeArguments()[0];
    final Class entityClass = (t instanceof Class) ? (Class)t : (Class)((ParameterizedType)t).getRawType();
    final Type entityGenericType = (t instanceof Class) ? entityClass : t;

    MessageBodyReader br = bodyWorker.getMessageBodyReader(entityClass, entityGenericType, annotations, mediaType);
    if (br == null) {
        LOGGER.severe("A message body reader for the type, " + type + ", could not be found");
        throw new WebApplicationException();
    }
    Object o = br.readFrom(entityClass, entityGenericType, annotations, mediaType, httpHeaders, entityStream);
    return new EntityHolder(o);
}
 
Example 5
Project: rfDynHUD   File: StreamUtils.java View source code Vote up 6 votes
/**
 * Skips and discards the given number of bytes from the given stream.
 * 
 * @param in
 * @param toSkip
 * 
 * @throws IOException
 */
public static final void skipBytes( BufferedInputStream in, long toSkip ) throws IOException
{
    while ( toSkip > 0L )
    {
        long skipped = in.skip( toSkip );
        
        if ( skipped > 0 )
            toSkip -= skipped;
        else if ( skipped < 0 )
            toSkip = 0;
    }
}