001: /*
002:
003: Derby - Class org.apache.derby.iapi.services.io.InputStreamUtil
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.iapi.services.io;
023:
024: import java.io.*;
025:
026: /**
027: Utility methods for InputStream that are stand-ins for
028: a small subset of DataInput methods. This avoids pushing
029: a DataInputStream just to get this functionality.
030: */
031: public final class InputStreamUtil {
032:
033: /**
034: Read an unsigned byte from an InputStream, throwing an EOFException
035: if the end of the input is reached.
036:
037: @exception IOException if an I/O error occurs.
038: @exception EOFException if the end of the stream is reached
039:
040: @see DataInput#readUnsignedByte
041:
042: */
043: public static int readUnsignedByte(InputStream in)
044: throws IOException {
045: int b = in.read();
046: if (b < 0)
047: throw new EOFException();
048:
049: return b;
050: }
051:
052: /**
053: Read a number of bytes into an array.
054:
055: @exception IOException if an I/O error occurs.
056: @exception EOFException if the end of the stream is reached
057:
058: @see DataInput#readFully
059:
060: */
061: public static void readFully(InputStream in, byte b[], int offset,
062: int len) throws IOException {
063: do {
064: int bytesRead = in.read(b, offset, len);
065: if (bytesRead < 0)
066: throw new EOFException();
067: len -= bytesRead;
068: offset += bytesRead;
069: } while (len != 0);
070: }
071:
072: /**
073: Read a number of bytes into an array.
074: Keep reading in a loop until len bytes are read or EOF is reached or
075: an exception is thrown. Return the number of bytes read.
076: (InputStream.read(byte[],int,int) does not guarantee to read len bytes
077: even if it can do so without reaching EOF or raising an exception.)
078:
079: @exception IOException if an I/O error occurs.
080: */
081: public static int readLoop(InputStream in, byte b[], int offset,
082: int len) throws IOException {
083: int firstOffset = offset;
084: do {
085: int bytesRead = in.read(b, offset, len);
086: if (bytesRead <= 0)
087: break;
088: len -= bytesRead;
089: offset += bytesRead;
090: } while (len != 0);
091: return offset - firstOffset;
092: }
093:
094: /**
095: Skip a number of bytes in the stream. Note that this version takes and returns
096: a long instead of the int used by skipBytes.
097:
098: @exception IOException if an I/O error occurs.
099: @exception EOFException if the end of the stream is reached
100:
101: @see DataInput#skipBytes
102: */
103: public static long skipBytes(InputStream in, long n)
104: throws IOException {
105:
106: while (n > 0) {
107: //System.out.println(" skip n = " + n);
108: long delta = in.skip(n);
109: //System.out.println(" skipped = " + delta);
110: if (delta < 0)
111: throw new EOFException();
112: n -= delta;
113: }
114:
115: return n;
116: }
117: }
|