comparison hg4j/src/main/java/org/tmatesoft/hg/internal/DataAccessProvider.java @ 213:6ec4af642ba8 gradle

Project uses Gradle for build - actual changes
author Alexander Kitaev <kitaev@gmail.com>
date Tue, 10 May 2011 10:52:53 +0200
parents
children
comparison
equal deleted inserted replaced
212:edb2e2829352 213:6ec4af642ba8
1 /*
2 * Copyright (c) 2010-2011 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.internal;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
23 import java.nio.MappedByteBuffer;
24 import java.nio.channels.FileChannel;
25
26 import org.tmatesoft.hg.core.HgBadStateException;
27
28 /**
29 *
30 * @author Artem Tikhomirov
31 * @author TMate Software Ltd.
32 */
33 public class DataAccessProvider {
34
35 private final int mapioMagicBoundary;
36 private final int bufferSize;
37
38 public DataAccessProvider() {
39 this(100 * 1024, 8 * 1024);
40 }
41
42 public DataAccessProvider(int mapioBoundary, int regularBufferSize) {
43 mapioMagicBoundary = mapioBoundary;
44 bufferSize = regularBufferSize;
45 }
46
47 public DataAccess create(File f) {
48 if (!f.exists()) {
49 return new DataAccess();
50 }
51 try {
52 FileChannel fc = new FileInputStream(f).getChannel();
53 int flen = (int) fc.size();
54 if (fc.size() - flen != 0) {
55 throw new HgBadStateException("Files greater than 2Gb are not yet supported");
56 }
57 if (flen > mapioMagicBoundary) {
58 // TESTS: bufLen of 1024 was used to test MemMapFileAccess
59 return new MemoryMapFileAccess(fc, flen, mapioMagicBoundary);
60 } else {
61 // XXX once implementation is more or less stable,
62 // may want to try ByteBuffer.allocateDirect() to see
63 // if there's any performance gain.
64 boolean useDirectBuffer = false;
65 // TESTS: bufferSize of 100 was used to check buffer underflow states when readBytes reads chunks bigger than bufSize
66 return new FileAccess(fc, flen, bufferSize, useDirectBuffer);
67 }
68 } catch (IOException ex) {
69 // unlikely to happen, we've made sure file exists.
70 ex.printStackTrace(); // FIXME log error
71 }
72 return new DataAccess(); // non-null, empty.
73 }
74
75 // DOESN'T WORK YET
76 private static class MemoryMapFileAccess extends DataAccess {
77 private FileChannel fileChannel;
78 private final int size;
79 private long position = 0; // always points to buffer's absolute position in the file
80 private final int memBufferSize;
81 private MappedByteBuffer buffer;
82
83 public MemoryMapFileAccess(FileChannel fc, int channelSize, int /*long?*/ bufferSize) {
84 fileChannel = fc;
85 size = channelSize;
86 memBufferSize = bufferSize;
87 }
88
89 @Override
90 public boolean isEmpty() {
91 return position + (buffer == null ? 0 : buffer.position()) >= size;
92 }
93
94 @Override
95 public int length() {
96 return size;
97 }
98
99 @Override
100 public DataAccess reset() throws IOException {
101 seek(0);
102 return this;
103 }
104
105 @Override
106 public void seek(int offset) {
107 assert offset >= 0;
108 // offset may not necessarily be further than current position in the file (e.g. rewind)
109 if (buffer != null && /*offset is within buffer*/ offset >= position && (offset - position) < buffer.limit()) {
110 buffer.position((int) (offset - position));
111 } else {
112 position = offset;
113 buffer = null;
114 }
115 }
116
117 @Override
118 public void skip(int bytes) throws IOException {
119 assert bytes >= 0;
120 if (buffer == null) {
121 position += bytes;
122 return;
123 }
124 if (buffer.remaining() > bytes) {
125 buffer.position(buffer.position() + bytes);
126 } else {
127 position += buffer.position() + bytes;
128 buffer = null;
129 }
130 }
131
132 private void fill() throws IOException {
133 if (buffer != null) {
134 position += buffer.position();
135 }
136 long left = size - position;
137 buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, position, left < memBufferSize ? left : memBufferSize);
138 }
139
140 @Override
141 public void readBytes(byte[] buf, int offset, int length) throws IOException {
142 if (buffer == null || !buffer.hasRemaining()) {
143 fill();
144 }
145 // XXX in fact, we may try to create a MappedByteBuffer of exactly length size here, and read right away
146 while (length > 0) {
147 int tail = buffer.remaining();
148 if (tail == 0) {
149 throw new IOException();
150 }
151 if (tail >= length) {
152 buffer.get(buf, offset, length);
153 } else {
154 buffer.get(buf, offset, tail);
155 fill();
156 }
157 offset += tail;
158 length -= tail;
159 }
160 }
161
162 @Override
163 public byte readByte() throws IOException {
164 if (buffer == null || !buffer.hasRemaining()) {
165 fill();
166 }
167 if (buffer.hasRemaining()) {
168 return buffer.get();
169 }
170 throw new IOException();
171 }
172
173 @Override
174 public void done() {
175 buffer = null;
176 if (fileChannel != null) {
177 try {
178 fileChannel.close();
179 } catch (IOException ex) {
180 ex.printStackTrace(); // log debug
181 }
182 fileChannel = null;
183 }
184 }
185 }
186
187 // (almost) regular file access - FileChannel and buffers.
188 private static class FileAccess extends DataAccess {
189 private FileChannel fileChannel;
190 private final int size;
191 private ByteBuffer buffer;
192 private int bufferStartInFile = 0; // offset of this.buffer in the file.
193
194 public FileAccess(FileChannel fc, int channelSize, int bufferSizeHint, boolean useDirect) {
195 fileChannel = fc;
196 size = channelSize;
197 final int capacity = size < bufferSizeHint ? size : bufferSizeHint;
198 buffer = useDirect ? ByteBuffer.allocateDirect(capacity) : ByteBuffer.allocate(capacity);
199 buffer.flip(); // or .limit(0) to indicate it's empty
200 }
201
202 @Override
203 public boolean isEmpty() {
204 return bufferStartInFile + buffer.position() >= size;
205 }
206
207 @Override
208 public int length() {
209 return size;
210 }
211
212 @Override
213 public DataAccess reset() throws IOException {
214 seek(0);
215 return this;
216 }
217
218 @Override
219 public void seek(int offset) throws IOException {
220 if (offset > size) {
221 throw new IllegalArgumentException();
222 }
223 if (offset < bufferStartInFile + buffer.limit() && offset >= bufferStartInFile) {
224 buffer.position((int) (offset - bufferStartInFile));
225 } else {
226 // out of current buffer, invalidate it (force re-read)
227 // XXX or ever re-read it right away?
228 bufferStartInFile = offset;
229 buffer.clear();
230 buffer.limit(0); // or .flip() to indicate we switch to reading
231 fileChannel.position(offset);
232 }
233 }
234
235 @Override
236 public void skip(int bytes) throws IOException {
237 final int newPos = buffer.position() + bytes;
238 if (newPos >= 0 && newPos < buffer.limit()) {
239 // no need to move file pointer, just rewind/seek buffer
240 buffer.position(newPos);
241 } else {
242 //
243 seek(bufferStartInFile + newPos);
244 }
245 }
246
247 private boolean fill() throws IOException {
248 if (!buffer.hasRemaining()) {
249 bufferStartInFile += buffer.limit();
250 buffer.clear();
251 if (bufferStartInFile < size) { // just in case there'd be any exception on EOF, not -1
252 fileChannel.read(buffer);
253 // may return -1 when EOF, but empty will reflect this, hence no explicit support here
254 }
255 buffer.flip();
256 }
257 return buffer.hasRemaining();
258 }
259
260 @Override
261 public void readBytes(byte[] buf, int offset, int length) throws IOException {
262 if (!buffer.hasRemaining()) {
263 fill();
264 }
265 while (length > 0) {
266 int tail = buffer.remaining();
267 if (tail == 0) {
268 throw new IOException(); // shall not happen provided stream contains expected data and no attempts to read past isEmpty() == true are made.
269 }
270 if (tail >= length) {
271 buffer.get(buf, offset, length);
272 } else {
273 buffer.get(buf, offset, tail);
274 fill();
275 }
276 offset += tail;
277 length -= tail;
278 }
279 }
280
281 @Override
282 public byte readByte() throws IOException {
283 if (buffer.hasRemaining()) {
284 return buffer.get();
285 }
286 if (fill()) {
287 return buffer.get();
288 }
289 throw new IOException();
290 }
291
292 @Override
293 public void done() {
294 if (buffer != null) {
295 buffer = null;
296 }
297 if (fileChannel != null) {
298 try {
299 fileChannel.close();
300 } catch (IOException ex) {
301 ex.printStackTrace(); // log debug
302 }
303 fileChannel = null;
304 }
305 }
306 }
307 }