• BlackBerry Dynamics
  • Runtime library for Android applications
  • 12.0.1.79
ContentLengthInputStream Class Reference

Stream that cuts off after a specified number of bytes. More...

Inheritance diagram for ContentLengthInputStream:
InputStream

Description

Note that this class NEVER closes the underlying stream, even when close gets called. Instead, it will read until the "end" of its chunking on close, which allows for the seamless execution of subsequent HTTP 1.1 requests, while not requiring the client to remember to read the entire contents of the response.

Implementation note: Choices abound. One approach would pass through the java.io.InputStream mark and java.io.InputStream reset calls to the underlying stream. That's tricky, though, because you then have to start duplicating the work of keeping track of how much a reset rewinds. Further, you have to watch out for the "readLimit", and since the semantics for the readLimit leave room for differing implementations, you might get into a lot of trouble.

Alternatively, you could make this class extend java.io.BufferedInputStream and then use the protected members of that class to avoid duplicated effort. That solution has the side effect of adding yet another possible layer of buffering.

Then, there is the simple choice, which this takes - simply don't support java.io.InputStream mark and java.io.InputStream reset. That choice has the added benefit of keeping this class very simple.

Author
Ortwin Glueck
Eric Johnson
Mike Bowler
Since
4.0

Notice

The following notice applies to the original API on which this API is based, and to its documentation. The documentation of this API has been revised from the original.

/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

Public Member Functions

 ContentLengthInputStream (final SessionInputBuffer in, long contentLength)
 Creates a new length limited stream. More...
 
void close () throws IOException
 
int read () throws IOException
 Read the next byte from the stream. More...
 
int read (byte[] b, int off, int len) throws java.io.IOException
 Does standard java.io.InputStream read(byte[], int, int) behavior, but also notifies the watcher when the contents have been consumed. More...
 
int read (byte[] b) throws IOException
 Read more bytes from the stream. More...
 
long skip (long n) throws IOException
 Skips and discards a number of bytes from the input stream. More...
 

Constructor & Destructor Documentation

◆ ContentLengthInputStream()

ContentLengthInputStream ( final SessionInputBuffer  in,
long  contentLength 
)
Parameters
inThe session input buffer to wrap
contentLengthThe maximum number of bytes that can be read from the stream. Subsequent read operations will return -1.

Member Function Documentation

◆ close()

void close ( ) throws IOException

Reads until the end of the known length of content.

Does not close the underlying socket input, but instead leaves it primed to parse the next response.

Exceptions
IOExceptionIf an IO problem occurs.

◆ read() [1/3]

int read ( ) throws IOException
Returns
The next byte or -1 if the end of stream has been reached.
Exceptions
IOExceptionIf an IO problem occurs
See also
java.io.InputStream::read()

◆ read() [2/3]

int read ( byte[]  b,
int  off,
int  len 
) throws java.io.IOException
Parameters
bThe byte array to fill.
offStart filling at this position.
lenThe number of bytes to attempt to read.
Returns
The number of bytes read, or -1 if the end of content has been reached.
Exceptions
java.io.IOExceptionShould an error occur on the wrapped stream.

◆ read() [3/3]

int read ( byte[]  b) throws IOException
Parameters
bThe byte array to put the new data in.
Returns
The number of bytes read into the buffer.
Exceptions
IOExceptionIf an IO problem occurs See java.io.InputStream::read(byte[])

◆ skip()

long skip ( long  n) throws IOException
Parameters
nThe number of bytes to skip.
Returns
The actual number of bytes skipped. <= 0 if no bytes are skipped.
Exceptions
IOExceptionIf an error occurs while skipping bytes. See InputStream::skip(long)