001: /*
002: * File.java
003: *
004: *
005: * Copyright (c) 2003 Rimfaxe ApS (www.rimfaxe.com).
006: * All rights reserved.
007: *
008: * This package is written by Lars Andersen <lars@rimfaxe.com>
009: * and licensed by Rimfaxe ApS.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions
013: * are met:
014: *
015: * 1. Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * 2. Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in
020: * the documentation and/or other materials provided with the
021: * distribution.
022: *
023: * 3. The end-user documentation included with the redistribution, if
024: * any, must include the following acknowlegement:
025: * "This product includes software developed by Rimfaxe ApS
026: (www.rimfaxe.com)"
027: * Alternately, this acknowlegement may appear in the software itself,
028: * if and wherever such third-party acknowlegements normally appear.
029: *
030: * 4. The names "Rimfaxe", "Rimfaxe Software", "Lars Andersen" and
031: * "Rimfaxe WebServer" must not be used to endorse or promote products
032: * derived from this software without prior written permission. For written
033: * permission, please contact info@rimfaxe.com
034: *
035: * 5. Products derived from this software may not be called "Rimfaxe"
036: * nor may "Rimfaxe" appear in their names without prior written
037: * permission of the Rimfaxe ApS.
038: *
039: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
040: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
041: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
042: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
043: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
044: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
045: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
046: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
047: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
048: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049: * SUCH DAMAGE.
050: *
051: */
052:
053: package com.rimfaxe.util;
054:
055: import java.io.*;
056: import java.util.*;
057:
058: /**
059: *
060: * @author Lars Andersen
061: */
062: public class RimfaxeFile {
063: private String filename = "";
064: int error = 0;
065: int filelength = 0;
066:
067: String fs = System.getProperty("file.separator");
068:
069: /**
070: * Creates new file handler
071: */
072: public RimfaxeFile() {
073: }
074:
075: /**
076: * Creates new file handler
077: */
078: public RimfaxeFile(String fname) {
079: filename = convert(fname);
080: //System.out.println("FILE -> "+filename);
081: }
082:
083: public boolean delete() {
084: java.io.File f = new java.io.File(filename);
085: return f.delete();
086: }
087:
088: public String convert(String in) {
089: StringBuffer buf = new StringBuffer();
090: StringTokenizer tkz = new StringTokenizer(in, "/\\", true);
091: while (tkz.hasMoreTokens()) {
092: String tmp = tkz.nextToken();
093: if (tmp.equalsIgnoreCase("/"))
094: buf.append(fs);
095: else if (tmp.equalsIgnoreCase("\\"))
096: buf.append(fs);
097: else
098: buf.append(tmp);
099: }
100: return "" + buf;
101: }
102:
103: public int getError() {
104: return error;
105: }
106:
107: public String read(InputStream is) {
108: StringBuffer output = new StringBuffer();
109: try {
110: byte[] buf = new byte[1000];
111: int res = 0;
112:
113: res = is.read(buf);
114: while (res > 0) {
115: String s = new String(buf);
116: s = s.substring(0, res);
117: output.append(s);
118: res = is.read(buf);
119: }
120: is.close();
121: } catch (IOException exc) {
122: error = 1;
123: }
124:
125: return output.toString();
126: }
127:
128: public String read() {
129: StringBuffer output = new StringBuffer();
130: try {
131: FileInputStream stream = new FileInputStream(filename);
132: byte[] buf = new byte[1000];
133: int res = 0;
134:
135: res = stream.read(buf);
136: while (res > 0) {
137: String s = new String(buf);
138: s = s.substring(0, res);
139: output = output.append(s);
140: res = stream.read(buf);
141: }
142: stream.close();
143: } catch (IOException exc) {
144: error = 1;
145: output.append("error reading file : " + filename);
146: }
147:
148: return output.toString();
149: }
150:
151: public byte[] readBytes() {
152: ByteArrayOutputStream baos = new ByteArrayOutputStream();
153: try {
154: FileInputStream stream = new FileInputStream(filename);
155:
156: byte[] buf = new byte[1000];
157: int res = 0;
158:
159: res = stream.read(buf);
160: while (res > 0) {
161: baos.write(buf, 0, res);
162: res = stream.read(buf);
163: }
164: stream.close();
165: } catch (IOException exc) {
166: error = 1;
167: com.rimfaxe.util.Log
168: .log("error reading file : " + filename);
169: }
170:
171: return baos.toByteArray();
172: }
173:
174: public byte[] readBytes(InputStream stream) {
175: ByteArrayOutputStream baos = new ByteArrayOutputStream();
176: try {
177:
178: byte[] buf = new byte[1000];
179: int res = 0;
180:
181: res = stream.read(buf);
182: while (res > 0) {
183: baos.write(buf, 0, res);
184: res = stream.read(buf);
185: }
186: stream.close();
187: } catch (IOException exc) {
188: error = 1;
189:
190: }
191:
192: return baos.toByteArray();
193: }
194:
195: public byte[] readBytesDeflated() {
196: int l = 0;
197: ByteArrayOutputStream baos = new ByteArrayOutputStream();
198: java.util.zip.DeflaterOutputStream dos = new java.util.zip.DeflaterOutputStream(
199: baos);
200: try {
201: FileInputStream stream = new FileInputStream(filename);
202:
203: byte[] buf = new byte[1000];
204: int res = 0;
205:
206: res = stream.read(buf);
207: while (res > 0) {
208: l += res;
209: dos.write(buf, 0, res);
210:
211: res = stream.read(buf);
212: }
213: stream.close();
214: dos.finish();
215: filelength = l;
216: } catch (IOException exc) {
217: error = 1;
218: com.rimfaxe.util.Log
219: .log("error reading file : " + filename);
220: }
221:
222: return baos.toByteArray();
223: }
224:
225: public byte[] deflateByteArray(byte[] inflatedarray) {
226: int l = 0;
227: ByteArrayOutputStream baos = new ByteArrayOutputStream();
228: java.util.zip.DeflaterOutputStream dos = new java.util.zip.DeflaterOutputStream(
229: baos);
230: try {
231: ByteArrayInputStream stream = new ByteArrayInputStream(
232: inflatedarray);
233:
234: byte[] buf = new byte[1000];
235: int res = 0;
236:
237: res = stream.read(buf);
238: while (res > 0) {
239: l += res;
240: dos.write(buf, 0, res);
241:
242: res = stream.read(buf);
243: }
244: stream.close();
245: dos.finish();
246: filelength = l;
247: } catch (IOException exc) {
248: error = 1;
249:
250: }
251:
252: return baos.toByteArray();
253: }
254:
255: public int length() {
256: return filelength;
257: }
258:
259: public byte[] inflateByteArray(byte[] zip) {
260: ByteArrayInputStream bais = new ByteArrayInputStream(zip);
261: java.util.zip.InflaterInputStream stream = new java.util.zip.InflaterInputStream(
262: bais);
263:
264: ByteArrayOutputStream baos = new ByteArrayOutputStream();
265:
266: try {
267: byte[] buf = new byte[1000];
268: int res = 0;
269:
270: res = stream.read(buf);
271: while (res > 0) {
272: baos.write(buf, 0, res);
273:
274: res = stream.read(buf);
275: }
276: stream.close();
277:
278: } catch (IOException exc) {
279:
280: error = 1;
281: }
282:
283: return baos.toByteArray();
284: }
285:
286: public void rewrite(String content) {
287: int res = 0;
288: try {
289: FileOutputStream stream = new FileOutputStream(filename);
290: DataOutputStream data = new DataOutputStream(stream);
291:
292: data.writeBytes(content);
293:
294: data.close();
295: stream.close();
296: } catch (IOException exc) {
297: error = 2;
298: }
299: }
300:
301: public void rewriteBytes(byte[] content) {
302: int res = 0;
303: try {
304: FileOutputStream stream = new FileOutputStream(filename);
305: stream.write(content);
306: stream.close();
307: } catch (IOException exc) {
308: error = 2;
309: }
310: }
311: }
|