1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.services.io.InputStreamUtil |
4 | |
5 | Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.services.io; |
22 | |
23 | import java.io.*; |
24 | |
25 | /** |
26 | Utility methods for InputStream that are stand-ins for |
27 | a small subset of DataInput methods. This avoids pushing |
28 | a DataInputStream just to get this functionality. |
29 | */ |
30 | public final class InputStreamUtil { |
31 | |
32 | /** |
33 | Read an unsigned byte from an InputStream, throwing an EOFException |
34 | if the end of the input is reached. |
35 | |
36 | @exception IOException if an I/O error occurs. |
37 | @exception EOFException if the end of the stream is reached |
38 | |
39 | @see DataInput#readUnsignedByte |
40 | |
41 | */ |
42 | public static int readUnsignedByte(InputStream in) throws IOException { |
43 | int b = in.read(); |
44 | if (b < 0) |
45 | throw new EOFException(); |
46 | |
47 | return b; |
48 | } |
49 | |
50 | /** |
51 | Read a number of bytes into an array. |
52 | |
53 | @exception IOException if an I/O error occurs. |
54 | @exception EOFException if the end of the stream is reached |
55 | |
56 | @see DataInput#readFully |
57 | |
58 | */ |
59 | public static void readFully(InputStream in, byte b[], |
60 | int offset, |
61 | int len) throws IOException |
62 | { |
63 | do { |
64 | int bytesRead = in.read(b, offset, len); |
65 | if (bytesRead < 0) |
66 | throw new EOFException(); |
67 | len -= bytesRead; |
68 | offset += bytesRead; |
69 | } while (len != 0); |
70 | } |
71 | |
72 | |
73 | /** |
74 | Read a number of bytes into an array. |
75 | Keep reading in a loop until len bytes are read or EOF is reached or |
76 | an exception is thrown. Return the number of bytes read. |
77 | (InputStream.read(byte[],int,int) does not guarantee to read len bytes |
78 | even if it can do so without reaching EOF or raising an exception.) |
79 | |
80 | @exception IOException if an I/O error occurs. |
81 | */ |
82 | public static int readLoop(InputStream in, |
83 | byte b[], |
84 | int offset, |
85 | int len) |
86 | throws IOException |
87 | { |
88 | int firstOffset = offset; |
89 | do { |
90 | int bytesRead = in.read(b, offset, len); |
91 | if (bytesRead <= 0) |
92 | break; |
93 | len -= bytesRead; |
94 | offset += bytesRead; |
95 | } while (len != 0); |
96 | return offset - firstOffset; |
97 | } |
98 | |
99 | |
100 | /** |
101 | Skip a number of bytes in the stream. Note that this version takes and returns |
102 | a long instead of the int used by skipBytes. |
103 | |
104 | @exception IOException if an I/O error occurs. |
105 | @exception EOFException if the end of the stream is reached |
106 | |
107 | @see DataInput#skipBytes |
108 | */ |
109 | public static long skipBytes(InputStream in, long n) throws IOException { |
110 | |
111 | while (n > 0) { |
112 | //System.out.println(" skip n = " + n); |
113 | long delta = in.skip(n); |
114 | //System.out.println(" skipped = " + delta); |
115 | if (delta < 0) |
116 | throw new EOFException(); |
117 | n -= delta; |
118 | } |
119 | |
120 | return n; |
121 | } |
122 | } |