/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* The HttpInputStream class assists the HttpSendSocket and HttpReceiveSocket
* classes by filtering out the header for the message as well as any
* data after its proper content length.
*/
/** bytes remaining to be read from proper content of message */
protected int bytesLeft;
/** bytes remaining to be read at time of last mark */
protected int bytesLeftAtMark;
/**
* Create new filter on a given input stream.
* @param in the InputStream to filter from
*/
{
super(in);
if (in.markSupported())
// pull out header, looking for content length
boolean contentLengthFound = false;
do {
}
throw new EOFException();
if (contentLengthFound) {
throw new IOException(
"Multiple Content-length entries found.");
} else {
contentLengthFound = true;
}
}
// The idea here is to go past the first blank line.
// Some DataInputStream.readLine() documentation specifies that
// it does include the line-terminating character(s) in the
// returned string, but it actually doesn't, so we'll cover
// all cases here...
// This really shouldn't happen, but if it does, shoud we fail??
// For now, just give up and let a whole lot of bytes through...
}
"content length: " + bytesLeft);
}
}
/**
* Returns the number of bytes that can be read with blocking.
* Make sure that this does not exceed the number of bytes remaining
* in the proper content of the message.
*/
{
if (bytesAvailable > bytesLeft)
return bytesAvailable;
}
/**
* Read a byte of data from the stream. Make sure that one is available
* from the proper content of the message, else -1 is returned to
* indicate to the user that the end of the stream has been reached.
*/
{
if (bytesLeft > 0) {
if (data != -1)
-- bytesLeft;
"received byte: '" +
"' " + data);
}
return data;
}
else {
"read past content length");
return -1;
}
}
{
"read past content length");
return -1;
}
}
return bytesRead;
}
/**
* Mark the current position in the stream (for future calls to reset).
* Remember where we are within the proper content of the message, so
* that a reset method call can recreate our state properly.
* @param readlimit how many bytes can be read before mark becomes invalid
*/
{
if (in.markSupported())
}
/**
* Repositions the stream to the last marked position. Make sure to
* adjust our position within the proper content accordingly.
*/
{
}
/**
* Skips bytes of the stream. Make sure to adjust our
* position within the proper content accordingly.
* @param n number of bytes to be skipped
*/
{
if (n > bytesLeft)
n = bytesLeft;
return bytesSkipped;
}
}