KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > util > IOUtils


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.util;
20
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23
24 public class IOUtils
25 {
26     private IOUtils()
27     {
28     }
29
30     /**
31      * Helper method, just calls <tt>readFully(in, b, 0, b.length)</tt>
32      */

33     public static int readFully(InputStream JavaDoc in, byte[] b)
34     throws IOException JavaDoc
35     {
36         return readFully(in, b, 0, b.length);
37     }
38
39     /**
40      * Same as the normal <tt>in.read(b, off, len)</tt>, but tries to ensure that
41      * the entire len number of bytes is read.
42      * <p>
43      * If the end of file is reached before any bytes are read, returns -1.
44      * Otherwise, returns the number of bytes read.
45      */

46     public static int readFully(InputStream JavaDoc in, byte[] b, int off, int len)
47     throws IOException JavaDoc
48     {
49         int total = 0;
50         for (;;) {
51             int got = in.read(b, off + total, len - total);
52             if (got < 0) {
53                 return (total == 0) ? -1 : total;
54             } else {
55                 total += got;
56                 if (total == len)
57                     return total;
58             }
59         }
60     }
61 }
62
63
Popular Tags