svn-gvsig-desktop / branches / Mobile_Compatible_Hito_1 / libFMap_mobile_shp_driver / src-comp / org / gvsig / datasources / impljme / BufferedBigByteBufferJME.java @ 21927
History | View | Annotate | Download (4.58 KB)
1 | 21865 | jldominguez | package org.gvsig.datasources.impljme; |
---|---|---|---|
2 | |||
3 | import java.io.IOException; |
||
4 | import java.nio.ByteOrder; |
||
5 | |||
6 | import org.apache.log4j.Logger; |
||
7 | import org.gvsig.datasources.common.IBigByteBuffer; |
||
8 | import org.gvsig.datasources.common.IByteBuffer; |
||
9 | import org.gvsig.datasources.common.IRandomFileChannel; |
||
10 | |||
11 | /**
|
||
12 | * This class implements the IBigByteBuffer for JME. As the desktop version,
|
||
13 | * it keeps a small buffer in memory, by using the classes ByteBufferJME
|
||
14 | * and RandomFileChannelJME.
|
||
15 | *
|
||
16 | * @see org.gvsig.datasources.impljme.ByteBufferJME
|
||
17 | * @see org.gvsig.datasources.impljme.RandomFileChannelJME
|
||
18 | *
|
||
19 | * @author jldominguez
|
||
20 | *
|
||
21 | */
|
||
22 | public class BufferedBigByteBufferJME implements IBigByteBuffer { |
||
23 | |||
24 | private static Logger logger = Logger.getLogger(BufferedBigByteBufferJME.class); |
||
25 | |||
26 | /**
|
||
27 | * Memory buffer size
|
||
28 | */
|
||
29 | public static long DEFAULT_SIZE = 4*1024; // 8 Kbytes |
||
30 | |||
31 | private ByteBufferJME theBuffer = null; |
||
32 | |||
33 | private RandomFileChannelJME fc;
|
||
34 | private long minAbs, maxAbs, posAbs; |
||
35 | private int minRel, maxRel, posRel; |
||
36 | private long sizeChunk, amountMem; |
||
37 | private long fileSize; |
||
38 | private int mode; |
||
39 | |||
40 | public BufferedBigByteBufferJME(IRandomFileChannel ch, int acc_mode) { |
||
41 | |||
42 | amountMem = DEFAULT_SIZE; |
||
43 | fc = (RandomFileChannelJME) ch; |
||
44 | try {
|
||
45 | fileSize = fc.size(); |
||
46 | mode = acc_mode; |
||
47 | sizeChunk = Math.min(fc.size(), amountMem);
|
||
48 | } catch (IOException e) { |
||
49 | logger.error("While instantiating: " + e.getMessage());
|
||
50 | } |
||
51 | |||
52 | theBuffer = (ByteBufferJME) Impl.allocateDirect((int) sizeChunk);
|
||
53 | |||
54 | byte[] aux = new byte[(int) sizeChunk]; |
||
55 | |||
56 | fc.getAsIs(aux); |
||
57 | |||
58 | theBuffer.put(aux); |
||
59 | |||
60 | theBuffer.position(0);
|
||
61 | minAbs = 0;
|
||
62 | maxAbs = sizeChunk; |
||
63 | } |
||
64 | |||
65 | /**
|
||
66 | * Checks the position and loads the data if necessary.
|
||
67 | *
|
||
68 | * @throws IOException
|
||
69 | */
|
||
70 | private void prepareBuffer(long posActual, int numBytesToRead) |
||
71 | { |
||
72 | long desiredPos = posActual + numBytesToRead;
|
||
73 | if ((desiredPos > maxAbs) || (posActual < minAbs)) {
|
||
74 | |||
75 | // logger.debug("Se ha pedido fuera del buffer: Tenemos: ["
|
||
76 | // + minAbs + " , " + maxAbs + "], Se pide hasta: " + desiredPos);
|
||
77 | // Quiero leer fuera:
|
||
78 | sizeChunk = Math.min(fileSize-posActual, amountMem);
|
||
79 | try {
|
||
80 | mapFrom(posActual); |
||
81 | } catch (IOException e) { |
||
82 | e.printStackTrace(); |
||
83 | } |
||
84 | } else {
|
||
85 | theBuffer.position((int) (posActual - minAbs));
|
||
86 | // System.out.println("NOT setting in buffer");
|
||
87 | } |
||
88 | // Dejamos posAbs apuntando a donde va a quedar
|
||
89 | // "a priori", antes de leer de verdad, que se hace
|
||
90 | // al salir de esta funci?n.
|
||
91 | posAbs = desiredPos; |
||
92 | } |
||
93 | |||
94 | |||
95 | |||
96 | /**
|
||
97 | * @param posActual
|
||
98 | * @throws IOException
|
||
99 | */
|
||
100 | private IByteBuffer mapFrom(long newPos) throws IOException { |
||
101 | ByteOrder lastOrder = theBuffer.order();
|
||
102 | // bb = fc.map(mode, newPos, sizeChunk);
|
||
103 | fc.position((int) newPos);
|
||
104 | // bb = ByteBuffer.wrap(buff);
|
||
105 | // bb = ByteBuffer.allocate((int)sizeChunk);
|
||
106 | theBuffer.position(0);
|
||
107 | |||
108 | byte[] newbuf = new byte[(int) sizeChunk]; |
||
109 | fc.getAsIs(newbuf); |
||
110 | |||
111 | theBuffer = new ByteBufferJME(newbuf);
|
||
112 | // System.out.println("Mapeo desde " + newPos + " con sizeChunk= " + sizeChunk + " numRead = " + numRead);
|
||
113 | minAbs = newPos; |
||
114 | maxAbs = sizeChunk + newPos; |
||
115 | theBuffer.order(lastOrder); |
||
116 | return theBuffer;
|
||
117 | } |
||
118 | |||
119 | |||
120 | |||
121 | public void position(long p) { |
||
122 | |||
123 | prepareBuffer(p, 0);
|
||
124 | int relPos = (int) (p - minAbs); |
||
125 | if (relPos < 0) { |
||
126 | System.out.println("Position=" + p); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | public int position() { |
||
131 | // TODO Auto-generated method stub
|
||
132 | return (int) posAbs; |
||
133 | } |
||
134 | |||
135 | public byte get() { |
||
136 | prepareBuffer(posAbs, 1);
|
||
137 | return theBuffer.get();
|
||
138 | } |
||
139 | |||
140 | public byte getByte(int ind) { |
||
141 | prepareBuffer(ind, 1);
|
||
142 | return theBuffer.get(ind - (int) minAbs); |
||
143 | } |
||
144 | |||
145 | public int getInt() { |
||
146 | prepareBuffer(posAbs, 4);
|
||
147 | return theBuffer.getInt();
|
||
148 | } |
||
149 | |||
150 | public short getShort() { |
||
151 | prepareBuffer(posAbs, 2);
|
||
152 | return theBuffer.getShort();
|
||
153 | } |
||
154 | |||
155 | public int getInt(int index) { |
||
156 | prepareBuffer(index, 4);
|
||
157 | return theBuffer.getInt(index - (int) minAbs); |
||
158 | } |
||
159 | |||
160 | public void get(byte[] out) { |
||
161 | prepareBuffer(posAbs, out.length); |
||
162 | theBuffer.get(out); |
||
163 | } |
||
164 | |||
165 | public double getDouble() { |
||
166 | prepareBuffer(posAbs, 8);
|
||
167 | return theBuffer.getDouble();
|
||
168 | } |
||
169 | |||
170 | public void order(ByteOrder o) { |
||
171 | theBuffer.order(o); |
||
172 | } |
||
173 | |||
174 | } |