KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > utils > IOUtils


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

16
17 package org.apache.axis.utils;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21
22 /**
23  * Utility class containing IO helper methods
24  */

25 public class IOUtils
26 {
27     private IOUtils() {
28     }
29
30     /**
31      * Read into a byte array; tries to ensure that the the
32      * full buffer is read.
33      *
34      * Helper method, just calls <tt>readFully(in, b, 0, b.length)</tt>
35      * @see #readFully(java.io.InputStream, byte[], int, int)
36      */

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

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