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