01: // ByteRangeOutputStream.java
02: // $Id: ByteRangeOutputStream.java,v 1.4 2001/04/12 14:08:59 ylafon Exp $
03: // (c) COPYRIGHT MIT and INRIA, 1996.
04: // Please first read the full copyright statement in file COPYRIGHT.html
05:
06: package org.w3c.www.http;
07:
08: import java.io.File;
09: import java.io.FileInputStream;
10: import java.io.IOException;
11: import java.io.InputStream;
12: import java.io.RandomAccessFile;
13:
14: public class ByteRangeOutputStream extends InputStream {
15: int firstp = -1;
16: int lastp = -1;
17: InputStream in = null;
18:
19: // RandomAccessFile in = null;
20:
21: public int read() throws IOException {
22: if (firstp < lastp) {
23: firstp++;
24: return in.read();
25: // return ((int) in.readByte()) & 0xff;
26: }
27: return -1;
28: }
29:
30: public int read(byte b[]) throws IOException {
31: return read(b, 0, b.length);
32: }
33:
34: public int read(byte b[], int off, int len) throws IOException {
35: if (firstp < lastp) {
36: int send = Math.min(lastp - firstp, len);
37: send = in.read(b, off, send);
38: firstp += send;
39: return send;
40: }
41: return -1;
42: }
43:
44: public void close() throws IOException {
45: in.close();
46: }
47:
48: public int available() {
49: return lastp - firstp;
50: }
51:
52: public ByteRangeOutputStream(File file, int firstp, int lastp)
53: throws IOException {
54: this .firstp = firstp;
55: this .lastp = lastp;
56: RandomAccessFile raf = new RandomAccessFile(file, "r");
57: raf.seek((long) firstp);
58: this .in = new FileInputStream(raf.getFD());
59: }
60:
61: public ByteRangeOutputStream(InputStream in, int firstp, int lastp)
62: throws IOException {
63: this .firstp = firstp;
64: this .lastp = lastp;
65: this .in = in;
66: in.skip((long) firstp);
67: }
68: }
|