KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2004,2005 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 package org.apache.axis2.util;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20
21 /**
22  * Utility class containing IO helper methods
23  */

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

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

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