KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > util > io > IORoutines


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Mar 19, 2005
23  */

24 package org.lobobrowser.util.io;
25
26 import java.io.*;
27
28 /**
29  * @author J. H. S.
30  */

31 public class IORoutines {
32     public static final byte[] LINE_BREAK_BYTES = { (byte) 13, (byte) 10 };
33     
34     public static String JavaDoc loadAsText(InputStream in, String JavaDoc encoding) throws IOException {
35         return loadAsText(in, encoding, 4096);
36     }
37     
38     public static String JavaDoc loadAsText(InputStream in, String JavaDoc encoding, int bufferSize) throws IOException {
39         InputStreamReader reader = new InputStreamReader(in, encoding);
40         char[] buffer = new char[bufferSize];
41         int offset = 0;
42         for(;;) {
43             int remain = buffer.length - offset;
44             if(remain <= 0) {
45                 char[] newBuffer = new char[buffer.length * 2];
46                 System.arraycopy(buffer, 0, newBuffer, 0, offset);
47                 buffer = newBuffer;
48                 remain = buffer.length - offset;
49             }
50             int numRead = reader.read(buffer, offset, remain);
51             if(numRead == -1) {
52                 break;
53             }
54             offset += numRead;
55         }
56         return new String JavaDoc(buffer, 0, offset);
57     }
58     
59     public static byte[] load(File file) throws IOException {
60         long fileLength = file.length();
61         if(fileLength > Integer.MAX_VALUE) {
62             throw new IOException("File '" + file.getName() + "' too big");
63         }
64         InputStream in = new FileInputStream(file);
65         try {
66             return loadExact(in, (int) fileLength);
67         } finally {
68             in.close();
69         }
70     }
71     
72     public static byte[] load(InputStream in) throws IOException {
73         return load(in, 4096);
74     }
75     public static byte[] load(InputStream in, int initialBufferSize) throws IOException {
76         if(initialBufferSize == 0) {
77             initialBufferSize = 1;
78         }
79         byte[] buffer = new byte[initialBufferSize];
80         int offset = 0;
81         for(;;) {
82             int remain = buffer.length - offset;
83             if(remain <= 0) {
84                 int newSize = buffer.length * 2;
85                 byte[] newBuffer = new byte[newSize];
86                 System.arraycopy(buffer, 0, newBuffer, 0, offset);
87                 buffer = newBuffer;
88                 remain = buffer.length - offset;
89             }
90             int numRead = in.read(buffer, offset, remain);
91             if(numRead == -1) {
92                 break;
93             }
94             offset += numRead;
95         }
96         if(offset < buffer.length) {
97             byte[] newBuffer = new byte[offset];
98             System.arraycopy(buffer, 0, newBuffer, 0, offset);
99             buffer = newBuffer;
100         }
101         return buffer;
102     }
103     
104     public static byte[] loadExact(InputStream in, int length) throws IOException {
105         byte[] buffer = new byte[length];
106         int offset = 0;
107         for(;;) {
108             int remain = length - offset;
109             if(remain <= 0) {
110                 break;
111             }
112             int numRead = in.read(buffer, offset, remain);
113             if(numRead == -1) {
114                 throw new IOException("Reached EOF, read " + offset + " expecting " + length);
115             }
116             offset += numRead;
117         }
118         return buffer;
119     }
120
121     public static boolean equalContent(File file, byte[] content) throws IOException {
122         long length = file.length();
123         if(length > Integer.MAX_VALUE) {
124             throw new IOException("File '" + file + "' too big");
125         }
126         InputStream in = new FileInputStream(file);
127         try {
128             byte[] fileContent = loadExact(in, (int) length);
129             return java.util.Arrays.equals(content, fileContent);
130         } finally {
131             in.close();
132         }
133     }
134     
135     public static void save(File file, byte[] content) throws IOException {
136         FileOutputStream out = new FileOutputStream(file);
137         try {
138             out.write(content);
139         } finally {
140             out.close();
141         }
142     }
143     
144     /**
145      * Reads line without buffering.
146      */

147     public static String JavaDoc readLine(InputStream in) throws IOException {
148         int b;
149         StringBuffer JavaDoc sb = null;
150         OUTER:
151         while((b = in.read()) != -1) {
152             if(sb == null) {
153                 sb = new StringBuffer JavaDoc();
154             }
155             switch(b) {
156                 case (byte) '\n':
157                     break OUTER;
158                 case (byte) '\r':
159                     break;
160                 default:
161                     sb.append((char) b);
162                     break;
163             }
164         }
165         return sb == null ? null : sb.toString();
166     }
167     
168     public static void touch(File file) {
169         file.setLastModified(System.currentTimeMillis());
170     }
171     
172     public static void saveStrings(File file, java.util.Collection JavaDoc list) throws IOException {
173         BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(file));
174         try {
175             PrintWriter writer = new PrintWriter(bout);
176             java.util.Iterator JavaDoc i = list.iterator();
177             while(i.hasNext()) {
178                 String JavaDoc text = (String JavaDoc) i.next();
179                 writer.println(text);
180             }
181             writer.flush();
182         } finally {
183             bout.close();
184         }
185     }
186     
187     public static java.util.List JavaDoc loadStrings(File file) throws IOException {
188         java.util.List JavaDoc list = new java.util.LinkedList JavaDoc();
189         InputStream in = new FileInputStream(file);
190         try {
191             BufferedReader reader = new BufferedReader(new InputStreamReader(in));
192             String JavaDoc line;
193             while((line = reader.readLine()) != null) {
194                 list.add(line);
195             }
196             return list;
197         } finally {
198             in.close();
199         }
200     }
201     
202 }
203
Popular Tags