root / branches / Mobile_Compatible_Hito_1 / libFMap / src-comp / org / gvsig / datasources / impljse / BigByteBuffer2.java @ 21606
History | View | Annotate | Download (10.1 KB)
1 |
/*
|
---|---|
2 |
* Created on 11-jul-2005
|
3 |
*
|
4 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
5 |
*
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Generalitat Valenciana
|
25 |
* Conselleria d'Infraestructures i Transport
|
26 |
* Av. Blasco Ib??ez, 50
|
27 |
* 46010 VALENCIA
|
28 |
* SPAIN
|
29 |
*
|
30 |
* +34 963862235
|
31 |
* gvsig@gva.es
|
32 |
* www.gvsig.gva.es
|
33 |
*
|
34 |
* or
|
35 |
*
|
36 |
* IVER T.I. S.A
|
37 |
* Salamanca 50
|
38 |
* 46005 Valencia
|
39 |
* Spain
|
40 |
*
|
41 |
* +34 963163400
|
42 |
* dac@iver.es
|
43 |
*/
|
44 |
package org.gvsig.datasources.impljse; |
45 |
|
46 |
import java.io.IOException; |
47 |
import java.nio.Buffer; |
48 |
import java.nio.ByteBuffer; |
49 |
import java.nio.ByteOrder; |
50 |
import java.nio.CharBuffer; |
51 |
import java.nio.DoubleBuffer; |
52 |
import java.nio.FloatBuffer; |
53 |
import java.nio.IntBuffer; |
54 |
import java.nio.LongBuffer; |
55 |
import java.nio.ShortBuffer; |
56 |
import java.nio.channels.FileChannel; |
57 |
|
58 |
/**
|
59 |
* @author Fjp
|
60 |
*
|
61 |
* Clase para trabajar con ficheros grandes. Mapea solo un trozo
|
62 |
* de fichero en memoria, y cuando intentas acceder fuera de esa
|
63 |
* zona, se ocupa de leer autom?ticamente el trozo que le falta.
|
64 |
*/
|
65 |
public class BigByteBuffer2 { |
66 |
|
67 |
private static long DEFAULT_SIZE = 8*1024; // 8 Kbytes |
68 |
|
69 |
ByteBuffer bb;
|
70 |
// byte[] buff = new byte[1024 * 1024];;
|
71 |
FileChannel fc;
|
72 |
long minAbs, maxAbs, posAbs;
|
73 |
int minRel, maxRel, posRel;
|
74 |
long sizeChunk, amountMem;
|
75 |
long fileSize;
|
76 |
FileChannel.MapMode mode;
|
77 |
|
78 |
/**
|
79 |
* Revisa la posici?n absoluta, y si hace falta, carga el buffer
|
80 |
* con la parte de fichero que toca.
|
81 |
* @throws IOException
|
82 |
*/
|
83 |
private synchronized void prepareBuffer(long posActual, int numBytesToRead) |
84 |
{ |
85 |
long desiredPos = posActual + numBytesToRead;
|
86 |
if ((desiredPos > maxAbs) || (posActual < minAbs))
|
87 |
{ |
88 |
// Quiero leer fuera:
|
89 |
sizeChunk = Math.min(fileSize-posActual, amountMem);
|
90 |
try {
|
91 |
mapFrom(posActual); |
92 |
// System.out.println("BigByteBuffer: min=" + minAbs
|
93 |
// + " maxAbs=" + maxAbs + " posAbs = " + posAbs);
|
94 |
|
95 |
} catch (IOException e) { |
96 |
e.printStackTrace(); |
97 |
|
98 |
} |
99 |
|
100 |
} |
101 |
// Dejamos posAbs apuntando a donde va a quedar
|
102 |
// "a priori", antes de leer de verdad, que se hace
|
103 |
// al salir de esta funci?n.
|
104 |
posAbs = desiredPos; |
105 |
|
106 |
} |
107 |
|
108 |
/**
|
109 |
* @param posActual
|
110 |
* @throws IOException
|
111 |
*/
|
112 |
private synchronized ByteBuffer mapFrom(long newPos) throws IOException { |
113 |
ByteOrder lastOrder = bb.order();
|
114 |
// bb = fc.map(mode, newPos, sizeChunk);
|
115 |
fc.position(newPos); |
116 |
// bb = ByteBuffer.wrap(buff);
|
117 |
// bb = ByteBuffer.allocate((int)sizeChunk);
|
118 |
bb.position(0);
|
119 |
int numRead = fc.read(bb);
|
120 |
bb.position(0);
|
121 |
// System.out.println("Mapeo desde " + newPos + " con sizeChunk= " + sizeChunk + " numRead = " + numRead);
|
122 |
minAbs = newPos; |
123 |
maxAbs = sizeChunk + newPos; |
124 |
bb.order(lastOrder); |
125 |
return bb;
|
126 |
} |
127 |
|
128 |
public BigByteBuffer2(FileChannel fc, FileChannel.MapMode mode, long amountMem) throws IOException |
129 |
{ |
130 |
this.amountMem = amountMem;
|
131 |
// this.buff = new byte[(int) amountMem];
|
132 |
this.fc = fc;
|
133 |
this.fileSize = fc.size();
|
134 |
this.mode = mode;
|
135 |
sizeChunk = Math.min(fc.size(), amountMem);
|
136 |
// bb = fc.map(mode, 0L, sizeChunk);
|
137 |
// bb = ByteBuffer.wrap(buff);
|
138 |
bb = ByteBuffer.allocateDirect((int)sizeChunk); |
139 |
int numRead = fc.read(bb);
|
140 |
bb.position(0);
|
141 |
minAbs = 0;
|
142 |
maxAbs = sizeChunk; |
143 |
} |
144 |
public BigByteBuffer2(FileChannel fc, FileChannel.MapMode mode) throws IOException |
145 |
{ |
146 |
this.amountMem = DEFAULT_SIZE;
|
147 |
this.fc = fc;
|
148 |
this.fileSize = fc.size();
|
149 |
this.mode = mode;
|
150 |
sizeChunk = Math.min(fc.size(), amountMem);
|
151 |
// bb = fc.map(mode, 0L, sizeChunk);
|
152 |
bb = ByteBuffer.allocateDirect((int)sizeChunk); |
153 |
int numRead = fc.read(bb);
|
154 |
|
155 |
bb.position(0);
|
156 |
minAbs = 0;
|
157 |
maxAbs = sizeChunk; |
158 |
} |
159 |
|
160 |
|
161 |
public synchronized byte get() { |
162 |
prepareBuffer(posAbs,1);
|
163 |
return bb.get();
|
164 |
} |
165 |
public synchronized ByteBuffer get(byte[] dst) |
166 |
{ |
167 |
prepareBuffer(posAbs, dst.length); |
168 |
return bb.get(dst);
|
169 |
} |
170 |
|
171 |
public synchronized char getChar() { |
172 |
prepareBuffer(posAbs,2);
|
173 |
return bb.getChar();
|
174 |
} |
175 |
|
176 |
public synchronized double getDouble() { |
177 |
prepareBuffer(posAbs,8);
|
178 |
return bb.getDouble();
|
179 |
} |
180 |
|
181 |
public synchronized float getFloat() { |
182 |
prepareBuffer(posAbs,4);
|
183 |
return bb.getFloat();
|
184 |
} |
185 |
|
186 |
public synchronized int getInt() { |
187 |
prepareBuffer(posAbs,4);
|
188 |
return bb.getInt();
|
189 |
} |
190 |
|
191 |
public synchronized long getLong() { |
192 |
prepareBuffer(posAbs,8);
|
193 |
return bb.getLong();
|
194 |
} |
195 |
|
196 |
public synchronized short getShort() { |
197 |
prepareBuffer(posAbs,2);
|
198 |
return bb.getShort();
|
199 |
} |
200 |
|
201 |
public boolean isDirect() { |
202 |
return bb.isDirect();
|
203 |
} |
204 |
|
205 |
public synchronized byte get(int index) { |
206 |
prepareBuffer(index,1);
|
207 |
return bb.get(index - (int) minAbs); |
208 |
} |
209 |
|
210 |
public synchronized char getChar(int index) { |
211 |
prepareBuffer(index,2);
|
212 |
return bb.getChar(index - (int) minAbs); |
213 |
} |
214 |
|
215 |
public synchronized double getDouble(int index) { |
216 |
prepareBuffer(index,8);
|
217 |
return bb.getDouble(index - (int) minAbs); |
218 |
} |
219 |
|
220 |
public synchronized float getFloat(int index) { |
221 |
prepareBuffer(index,4);
|
222 |
return bb.getFloat(index - (int) minAbs); |
223 |
} |
224 |
|
225 |
public synchronized int getInt(int index) { |
226 |
prepareBuffer(index,4);
|
227 |
return bb.getInt(index - (int) minAbs); |
228 |
} |
229 |
|
230 |
public synchronized long getLong(int index) { |
231 |
prepareBuffer(index,8);
|
232 |
return bb.getLong(index - (int) minAbs); |
233 |
} |
234 |
|
235 |
public synchronized short getShort(int index) { |
236 |
prepareBuffer(index,2);
|
237 |
return bb.getShort(index - (int) minAbs); |
238 |
} |
239 |
|
240 |
public ByteBuffer asReadOnlyBuffer() { |
241 |
return bb.asReadOnlyBuffer();
|
242 |
} |
243 |
|
244 |
public ByteBuffer compact() { |
245 |
return bb.compact();
|
246 |
} |
247 |
|
248 |
public ByteBuffer duplicate() { |
249 |
return bb.duplicate();
|
250 |
} |
251 |
|
252 |
public synchronized ByteBuffer slice() { |
253 |
return bb.slice();
|
254 |
} |
255 |
|
256 |
public synchronized ByteBuffer put(byte b) { |
257 |
prepareBuffer(posAbs,1);
|
258 |
return bb.put(b);
|
259 |
} |
260 |
|
261 |
public synchronized ByteBuffer putChar(char value) { |
262 |
prepareBuffer(posAbs,2);
|
263 |
return bb.putChar(value);
|
264 |
} |
265 |
|
266 |
public synchronized ByteBuffer putDouble(double value) { |
267 |
prepareBuffer(posAbs,8);
|
268 |
return bb.putDouble(value);
|
269 |
} |
270 |
|
271 |
public synchronized ByteBuffer putFloat(float value) { |
272 |
prepareBuffer(posAbs,4);
|
273 |
return bb.putFloat(value);
|
274 |
} |
275 |
|
276 |
public synchronized ByteBuffer putInt(int value) { |
277 |
prepareBuffer(posAbs,4);
|
278 |
return bb.putInt(value);
|
279 |
} |
280 |
|
281 |
public synchronized ByteBuffer put(int index, byte b) { |
282 |
prepareBuffer(index,1);
|
283 |
return bb.put(index- (int) minAbs, b); |
284 |
} |
285 |
|
286 |
public synchronized ByteBuffer putChar(int index, char value) { |
287 |
prepareBuffer(index,2);
|
288 |
return bb.putChar(index- (int) minAbs, value); |
289 |
} |
290 |
|
291 |
public synchronized ByteBuffer putDouble(int index, double value) { |
292 |
prepareBuffer(index,8);
|
293 |
return bb.putDouble(index- (int) minAbs, value); |
294 |
} |
295 |
|
296 |
public synchronized ByteBuffer putFloat(int index, float value) { |
297 |
prepareBuffer(index,4);
|
298 |
return bb.putFloat(index- (int) minAbs, value); |
299 |
} |
300 |
|
301 |
public synchronized ByteBuffer putInt(int index, int value) { |
302 |
prepareBuffer(index,4);
|
303 |
return bb.putInt(index- (int) minAbs, value); |
304 |
} |
305 |
|
306 |
public synchronized ByteBuffer putLong(int index, long value) { |
307 |
prepareBuffer(index,8);
|
308 |
return bb.putLong(index- (int) minAbs, value); |
309 |
} |
310 |
|
311 |
public synchronized ByteBuffer putShort(int index, short value) { |
312 |
prepareBuffer(index,2);
|
313 |
return bb.putShort(index- (int) minAbs, value); |
314 |
} |
315 |
|
316 |
public synchronized ByteBuffer putLong(long value) { |
317 |
prepareBuffer(posAbs,8);
|
318 |
return bb.putLong(value);
|
319 |
} |
320 |
|
321 |
public synchronized ByteBuffer putShort(short value) { |
322 |
prepareBuffer(posAbs,2);
|
323 |
return bb.putShort(value);
|
324 |
} |
325 |
|
326 |
public CharBuffer asCharBuffer() { |
327 |
return bb.asCharBuffer();
|
328 |
} |
329 |
|
330 |
public DoubleBuffer asDoubleBuffer() { |
331 |
return bb.asDoubleBuffer();
|
332 |
} |
333 |
|
334 |
public FloatBuffer asFloatBuffer() { |
335 |
return bb.asFloatBuffer();
|
336 |
} |
337 |
|
338 |
public IntBuffer asIntBuffer() { |
339 |
return bb.asIntBuffer();
|
340 |
} |
341 |
|
342 |
public LongBuffer asLongBuffer() { |
343 |
return bb.asLongBuffer();
|
344 |
} |
345 |
|
346 |
public ShortBuffer asShortBuffer() { |
347 |
return bb.asShortBuffer();
|
348 |
} |
349 |
|
350 |
public boolean isReadOnly() { |
351 |
return bb.isReadOnly();
|
352 |
} |
353 |
|
354 |
public synchronized final ByteOrder order() |
355 |
{ |
356 |
return bb.order();
|
357 |
} |
358 |
|
359 |
public synchronized final ByteBuffer order(ByteOrder bo) |
360 |
{ |
361 |
return bb.order(bo);
|
362 |
} |
363 |
public synchronized final long position() |
364 |
{ |
365 |
return posAbs;
|
366 |
} |
367 |
|
368 |
public synchronized final Buffer position(long newPosition) |
369 |
{ |
370 |
prepareBuffer(newPosition,0);
|
371 |
int relPos = (int) (newPosition - minAbs); |
372 |
if (relPos < 0) |
373 |
System.out.println("Position=" + newPosition); |
374 |
return bb.position(relPos);
|
375 |
} |
376 |
|
377 |
} |