KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > io > InputStreamUtil


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.io.InputStreamUtil
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.services.io;
23
24 import java.io.*;
25
26 /**
27     Utility methods for InputStream that are stand-ins for
28     a small subset of DataInput methods. This avoids pushing
29     a DataInputStream just to get this functionality.
30 */

31 public final class InputStreamUtil {
32
33     /**
34         Read an unsigned byte from an InputStream, throwing an EOFException
35         if the end of the input is reached.
36
37         @exception IOException if an I/O error occurs.
38         @exception EOFException if the end of the stream is reached
39
40         @see DataInput#readUnsignedByte
41     
42     */

43     public static int readUnsignedByte(InputStream in) throws IOException {
44         int b = in.read();
45         if (b < 0)
46             throw new EOFException();
47
48         return b;
49     }
50
51     /**
52         Read a number of bytes into an array.
53
54         @exception IOException if an I/O error occurs.
55         @exception EOFException if the end of the stream is reached
56
57         @see DataInput#readFully
58
59     */

60     public static void readFully(InputStream in, byte b[],
61                                  int offset,
62                                  int len) throws IOException
63     {
64         do {
65             int bytesRead = in.read(b, offset, len);
66             if (bytesRead < 0)
67                 throw new EOFException();
68             len -= bytesRead;
69             offset += bytesRead;
70         } while (len != 0);
71     }
72
73
74     /**
75         Read a number of bytes into an array.
76         Keep reading in a loop until len bytes are read or EOF is reached or
77         an exception is thrown. Return the number of bytes read.
78         (InputStream.read(byte[],int,int) does not guarantee to read len bytes
79          even if it can do so without reaching EOF or raising an exception.)
80
81         @exception IOException if an I/O error occurs.
82     */

83     public static int readLoop(InputStream in,
84                                 byte b[],
85                                 int offset,
86                                 int len)
87         throws IOException
88     {
89         int firstOffset = offset;
90         do {
91             int bytesRead = in.read(b, offset, len);
92             if (bytesRead <= 0)
93                 break;
94             len -= bytesRead;
95             offset += bytesRead;
96         } while (len != 0);
97         return offset - firstOffset;
98     }
99
100
101     /**
102         Skip a number of bytes in the stream. Note that this version takes and returns
103         a long instead of the int used by skipBytes.
104
105         @exception IOException if an I/O error occurs.
106         @exception EOFException if the end of the stream is reached
107
108         @see DataInput#skipBytes
109     */

110     public static long skipBytes(InputStream in, long n) throws IOException {
111
112         while (n > 0) {
113             //System.out.println(" skip n = " + n);
114
long delta = in.skip(n);
115             //System.out.println(" skipped = " + delta);
116
if (delta < 0)
117                 throw new EOFException();
118             n -= delta;
119         }
120
121         return n;
122     }
123 }
124
Popular Tags