--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.iapi.services.io.InputStreamUtil\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.iapi.services.io;\r
+\r
+import java.io.*;\r
+\r
+/**\r
+ Utility methods for InputStream that are stand-ins for\r
+ a small subset of DataInput methods. This avoids pushing\r
+ a DataInputStream just to get this functionality.\r
+*/\r
+public final class InputStreamUtil {\r
+\r
+ /**\r
+ Read an unsigned byte from an InputStream, throwing an EOFException\r
+ if the end of the input is reached.\r
+\r
+ @exception IOException if an I/O error occurs.\r
+ @exception EOFException if the end of the stream is reached\r
+\r
+ @see DataInput#readUnsignedByte\r
+ \r
+ */\r
+ public static int readUnsignedByte(InputStream in) throws IOException {\r
+ int b = in.read();\r
+ if (b < 0)\r
+ throw new EOFException();\r
+\r
+ return b;\r
+ }\r
+\r
+ /**\r
+ Read a number of bytes into an array.\r
+\r
+ @exception IOException if an I/O error occurs.\r
+ @exception EOFException if the end of the stream is reached\r
+\r
+ @see DataInput#readFully\r
+\r
+ */\r
+ public static void readFully(InputStream in, byte b[],\r
+ int offset,\r
+ int len) throws IOException\r
+ {\r
+ do {\r
+ int bytesRead = in.read(b, offset, len);\r
+ if (bytesRead < 0)\r
+ throw new EOFException();\r
+ len -= bytesRead;\r
+ offset += bytesRead;\r
+ } while (len != 0);\r
+ }\r
+\r
+\r
+ /**\r
+ Read a number of bytes into an array.\r
+ Keep reading in a loop until len bytes are read or EOF is reached or\r
+ an exception is thrown. Return the number of bytes read.\r
+ (InputStream.read(byte[],int,int) does not guarantee to read len bytes\r
+ even if it can do so without reaching EOF or raising an exception.)\r
+\r
+ @exception IOException if an I/O error occurs.\r
+ */\r
+ public static int readLoop(InputStream in,\r
+ byte b[],\r
+ int offset,\r
+ int len)\r
+ throws IOException\r
+ {\r
+ int firstOffset = offset;\r
+ do {\r
+ int bytesRead = in.read(b, offset, len);\r
+ if (bytesRead <= 0)\r
+ break;\r
+ len -= bytesRead;\r
+ offset += bytesRead;\r
+ } while (len != 0);\r
+ return offset - firstOffset;\r
+ }\r
+\r
+\r
+ /**\r
+ Skip a number of bytes in the stream. Note that this version takes and returns\r
+ a long instead of the int used by skipBytes.\r
+\r
+ @exception IOException if an I/O error occurs.\r
+ @exception EOFException if the end of the stream is reached\r
+\r
+ @see DataInput#skipBytes\r
+ */\r
+ public static long skipBytes(InputStream in, long n) throws IOException {\r
+\r
+ while (n > 0) {\r
+ //System.out.println(" skip n = " + n);\r
+ long delta = in.skip(n);\r
+ //System.out.println(" skipped = " + delta);\r
+ if (delta < 0)\r
+ throw new EOFException();\r
+ n -= delta;\r
+ }\r
+\r
+ return n;\r
+ }\r
+}\r