KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > io > IO


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003,2004 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20
21 // Copyright (C) 2003 William Pugh <pugh@cs.umd.edu>
22
// http://www.cs.umd.edu/~pugh
23
//
24
// This library is free software; you can redistribute it and/or
25
// modify it under the terms of the GNU Lesser General Public
26
// License as published by the Free Software Foundation; either
27
// version 2.1 of the License, or (at your option) any later version.
28
//
29
// This library is distributed in the hope that it will be useful,
30
// but WITHOUT ANY WARRANTY; without even the implied warranty of
31
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32
// Lesser General Public License for more details.
33
//
34

35 package edu.umd.cs.findbugs.io;
36
37 import java.io.BufferedReader JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.InputStream JavaDoc;
40 import java.io.InputStreamReader JavaDoc;
41 import java.io.OutputStream JavaDoc;
42 import java.io.Reader JavaDoc;
43 import java.io.StringWriter JavaDoc;
44 import java.io.Writer JavaDoc;
45
46 public class IO {
47     static ThreadLocal JavaDoc<byte[]> myByteBuf = new ThreadLocal JavaDoc<byte[]>() {
48         @Override JavaDoc
49         protected byte[] initialValue() {
50             return new byte[4096];
51         }
52     };
53     static ThreadLocal JavaDoc<char[]> myCharBuf = new ThreadLocal JavaDoc<char[]>() {
54         @Override JavaDoc
55         protected char[] initialValue() {
56             return new char[4096];
57         }
58     };
59
60     public static String JavaDoc readAll(InputStream JavaDoc in) throws IOException JavaDoc {
61         return readAll(new InputStreamReader JavaDoc(in));
62     }
63
64     public static String JavaDoc readAll(Reader JavaDoc reader) throws IOException JavaDoc {
65         BufferedReader JavaDoc r = new BufferedReader JavaDoc(reader);
66         StringWriter JavaDoc w = new StringWriter JavaDoc();
67         copy(r, w);
68         return w.toString();
69     }
70
71     public static long copy(InputStream JavaDoc in, OutputStream JavaDoc out)
72             throws IOException JavaDoc {
73         return copy(in, out, Long.MAX_VALUE);
74     }
75
76     public static long copy(Reader JavaDoc in, Writer JavaDoc out)
77             throws IOException JavaDoc {
78         return copy(in, out, Long.MAX_VALUE);
79     }
80
81
82     public static long copy(InputStream JavaDoc in, OutputStream JavaDoc out,
83                             long maxBytes)
84
85             throws IOException JavaDoc {
86         long total = 0;
87
88         int sz;
89
90         byte buf [] = myByteBuf.get();
91
92         while (maxBytes > 0 &&
93                 (sz = in.read(buf, 0,
94                         (int) Math.min(maxBytes, (long) buf.length)))
95                 > 0) {
96             total += sz;
97             maxBytes -= sz;
98             out.write(buf, 0, sz);
99         }
100         return total;
101     }
102
103     public static long copy(Reader JavaDoc in, Writer JavaDoc out,
104                             long maxChars)
105
106             throws IOException JavaDoc {
107         long total = 0;
108
109         int sz;
110
111         char buf [] = myCharBuf.get();
112
113         while (maxChars > 0 &&
114                 (sz = in.read(buf, 0,
115                         (int) Math.min(maxChars, (long) buf.length)))
116                 > 0) {
117             total += sz;
118             maxChars -= sz;
119             out.write(buf, 0, sz);
120         }
121         return total;
122     }
123     
124     /**
125      * Close given InputStream, ignoring any resulting exception.
126      *
127      * @param inputStream the InputStream to close;
128      * may be null (in which case nothing happens)
129      */

130     public static void close(InputStream JavaDoc inputStream) {
131         if (inputStream == null) {
132             return;
133         }
134         
135         try {
136             inputStream.close();
137         } catch (IOException JavaDoc e) {
138             // Ignore
139
}
140     }
141     
142     /**
143      * Close given OutputStream, ignoring any resulting exception.
144      *
145      * @param outputStream the OutputStream to close;
146      * may be null (in which case nothing happens)
147      */

148     public static void close(OutputStream JavaDoc outputStream) {
149         if (outputStream == null) {
150             return;
151         }
152         
153         try {
154             outputStream.close();
155         } catch (IOException JavaDoc e) {
156             // Ignore
157
}
158     }
159 }
160
Popular Tags