01: // PageCompileFile.java
02: // $Id: PageCompileFile.java,v 1.3 2000/08/16 21:37:43 ylafon Exp $
03: // (c) COPYRIGHT MIT and INRIA, 1998.
04: // Please first read the full copyright statement in file COPYRIGHT.html
05:
06: package org.w3c.jigsaw.pagecompile;
07:
08: import java.io.ByteArrayOutputStream;
09: import java.io.File;
10: import java.io.FileInputStream;
11: import java.io.IOException;
12: import java.io.OutputStream;
13:
14: /**
15: * @version $Revision: 1.3 $
16: * @author Benoît Mahé (bmahe@w3.org)
17: */
18: public class PageCompileFile {
19:
20: private byte[] filedata = null;
21:
22: protected void readFileData(String filename) throws IOException {
23: File file = new File(filename);
24: ByteArrayOutputStream out = new ByteArrayOutputStream(
25: (int) file.length());
26:
27: FileInputStream in = new FileInputStream(file);
28:
29: byte[] buf = new byte[4096];
30: int len = 0;
31:
32: while ((len = in.read(buf)) != -1)
33: out.write(buf, 0, len);
34:
35: in.close();
36: out.close();
37:
38: filedata = out.toByteArray();
39: }
40:
41: /**
42: * Write some bytes from this file in the given output stream.
43: * @param start start position in the file
44: * @param end end position in the file
45: * @param out the destination output stream
46: * @exception IOException if an IO error occurs
47: */
48: public void writeBytes(int start, int end, OutputStream out)
49: throws IOException {
50: int len = end - start + 1;
51: byte b[] = new byte[len];
52: if (start + len > filedata.length)
53: len--;
54: System.arraycopy(filedata, start, b, 0, len);
55: out.write(b);
56: }
57:
58: /**
59: * Create a PageCompileFile.
60: * @param filename the filename
61: * @exception IOException if an IO error occurs.
62: */
63: public PageCompileFile(String filename) throws IOException {
64: readFileData(filename);
65: }
66: }
|