01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.store.fs;
07:
08: import java.io.EOFException;
09: import java.io.IOException;
10: import java.io.InputStream;
11: import java.util.zip.ZipEntry;
12: import java.util.zip.ZipFile;
13:
14: import org.h2.util.IOUtils;
15:
16: /**
17: * The file is read from a stream. When reading from start to end, the same
18: * input stream is re-used, however when reading from end to start, a new input
19: * stream is opened for each request.
20: */
21: public class FileObjectZip implements FileObject {
22:
23: private ZipFile file;
24: private ZipEntry entry;
25: private long pos;
26: private InputStream in;
27: private long inPos;
28: private long length;
29:
30: public FileObjectZip(ZipFile file, ZipEntry entry) {
31: this .file = file;
32: this .entry = entry;
33: length = entry.getSize();
34: }
35:
36: public void close() throws IOException {
37: }
38:
39: public long getFilePointer() throws IOException {
40: return pos;
41: }
42:
43: public long length() throws IOException {
44: return length;
45: }
46:
47: public void readFully(byte[] b, int off, int len)
48: throws IOException {
49: if (inPos > pos) {
50: if (in != null) {
51: in.close();
52: }
53: in = null;
54: }
55: if (in == null) {
56: in = file.getInputStream(entry);
57: inPos = 0;
58: }
59: if (inPos < pos) {
60: IOUtils.skipFully(in, pos - inPos);
61: inPos = pos;
62: }
63: int l = IOUtils.readFully(in, b, off, len);
64: if (l != len) {
65: throw new EOFException();
66: }
67: pos += len;
68: inPos += len;
69: }
70:
71: public void seek(long pos) throws IOException {
72: this .pos = pos;
73: }
74:
75: public void setFileLength(long newLength) throws IOException {
76: throw new IOException("File is read-only");
77: }
78:
79: public void sync() throws IOException {
80: }
81:
82: public void write(byte[] b, int off, int len) throws IOException {
83: throw new IOException("File is read-only");
84: }
85:
86: }
|