Debian Codegraph @

src/main/org/h2/store/fs/zip/FileZip.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.store.fs.zip;
7
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.nio.ByteBuffer;
11 import java.nio.channels.FileChannel;
12 import java.nio.channels.FileLock;
13 import java.nio.channels.NonWritableChannelException;
14 import java.util.zip.ZipEntry;
15 import java.util.zip.ZipFile;
16 import org.h2.store.fs.FakeFileChannel;
17 import org.h2.store.fs.FileBase;
18 import org.h2.util.IOUtils;
19
20 /**
21 * The file is read from a stream. When reading from start to end, the same
22 * input stream is re-used, however when reading from end to start, a new input
23 * stream is opened for each request.
24 */
25 class FileZip extends FileBase {
26
27 private static final byte[] SKIP_BUFFER = new byte[1024];
28
29 private final ZipFile file;
30 private final ZipEntry entry;
31 private long pos;
32 private InputStream in;
33 private long inPos;
34 private final long length;
35 private boolean skipUsingRead;
36
37 FileZip(ZipFile file, ZipEntry entry) {
38 this.file = file;
39 this.entry = entry;
40 length = entry.getSize();
41 }
42
43 @Override
44 public long position() {
45 return pos;
46 }
47
48 @Override
49 public long size() {
50 return length;
51 }
52
53 @Override
54 public int read(ByteBuffer dst) throws IOException {
55 seek();
56 int len = in.read(dst.array(), dst.arrayOffset() + dst.position(),
57 dst.remaining());
58 if (len > 0) {
59 dst.position(dst.position() + len);
60 pos += len;
61 inPos += len;
62 }
63 return len;
64 }
65
66 private void seek() throws IOException {
67 if (inPos > pos) {
68 if (in != null) {
69 in.close();
70 }
71 in = null;
72 }
73 if (in == null) {
74 in = file.getInputStream(entry);
75 inPos = 0;
76 }
77 if (inPos < pos) {
78 long skip = pos - inPos;
79 if (!skipUsingRead) {
80 try {
81 IOUtils.skipFully(in, skip);
82 } catch (NullPointerException e) {
83 // workaround for Android
84 skipUsingRead = true;
85 }
86 }
87 if (skipUsingRead) {
88 while (skip > 0) {
89 int s = (int) Math.min(SKIP_BUFFER.length, skip);
90 s = in.read(SKIP_BUFFER, 0, s);
91 skip -= s;
92 }
93 }
94 inPos = pos;
95 }
96 }
97
98 @Override
99 public FileChannel position(long newPos) {
100 this.pos = newPos;
101 return this;
102 }
103
104 @Override
105 public FileChannel truncate(long newLength) throws IOException {
106 throw new IOException("File is read-only");
107 }
108
109 @Override
110 public void force(boolean metaData) throws IOException {
111 // nothing to do
112 }
113
114 @Override
115 public int write(ByteBuffer src) throws IOException {
116 throw new NonWritableChannelException();
117 }
118
119 @Override
120 public synchronized FileLock tryLock(long position, long size,
121 boolean shared) throws IOException {
122 if (shared) {
123 return new FileLock(FakeFileChannel.INSTANCE, position, size, shared) {
124
125 @Override
126 public boolean isValid() {
127 return true;
128 }
129
130 @Override
131 public void release() throws IOException {
132 // ignore
133 }};
134 }
135 return null;
136 }
137
138 @Override
139 protected void implCloseChannel() throws IOException {
140 if (in != null) {
141 in.close();
142 in = null;
143 }
144 file.close();
145 }
146
147 }