svn-gvsig-desktop / branches / Mobile_Compatible_Hito_1 / libFMap_mobile_shp_driver / src-comp / org / gvsig / datasources / impljme / RandomFileChannelJME.java @ 21865
History | View | Annotate | Download (4.98 KB)
1 |
package org.gvsig.datasources.impljme; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileNotFoundException; |
5 |
import java.io.IOException; |
6 |
import java.io.RandomAccessFile; |
7 |
import java.nio.ByteOrder; |
8 |
|
9 |
import org.apache.log4j.Logger; |
10 |
import org.gvsig.datasources.common.IByteBuffer; |
11 |
import org.gvsig.datasources.common.IRandomFileChannel; |
12 |
|
13 |
/**
|
14 |
* This class impelments the random file channel under JME, based on RandomAccessFile
|
15 |
*
|
16 |
* @see java.io.RandomAccessFile
|
17 |
*
|
18 |
* @author jldominguez
|
19 |
*
|
20 |
*/
|
21 |
public class RandomFileChannelJME implements IRandomFileChannel { |
22 |
|
23 |
private static Logger logger = Logger.getLogger(RandomFileChannelJME.class); |
24 |
|
25 |
private ByteOrder byteOrder = ByteOrder.LITTLE_ENDIAN; |
26 |
private RandomAccessFile raf = null; |
27 |
|
28 |
public RandomFileChannelJME(File f) { |
29 |
|
30 |
try {
|
31 |
raf = new RandomAccessFile(f, "rw"); |
32 |
} catch (FileNotFoundException e) { |
33 |
e.printStackTrace(); |
34 |
} |
35 |
} |
36 |
|
37 |
public RandomFileChannelJME(String file_path) { |
38 |
|
39 |
File f = new File(file_path); |
40 |
try {
|
41 |
raf = new RandomAccessFile(f, "rw"); |
42 |
} catch (FileNotFoundException e) { |
43 |
e.printStackTrace(); |
44 |
} |
45 |
} |
46 |
|
47 |
|
48 |
public int write(IByteBuffer aux, int pos) throws IOException { |
49 |
long old_pos = raf.getFilePointer();
|
50 |
raf.seek(pos); |
51 |
byte[] bb = ((ByteBufferJME) aux).getAllBytes(); |
52 |
raf.write(bb); |
53 |
raf.seek(old_pos); |
54 |
return bb.length;
|
55 |
} |
56 |
|
57 |
public void close() throws IOException { |
58 |
raf.close(); |
59 |
} |
60 |
|
61 |
public void position(int i) throws IOException { |
62 |
raf.seek(i); |
63 |
} |
64 |
|
65 |
public int write(IByteBuffer buffer) throws IOException { |
66 |
byte[] bb = ((ByteBufferJME) buffer).getAllBytes(); |
67 |
raf.write(bb); |
68 |
return bb.length;
|
69 |
} |
70 |
|
71 |
public long size() throws IOException { |
72 |
return raf.length();
|
73 |
} |
74 |
|
75 |
public int getPosition() throws IOException { |
76 |
return (int) raf.getFilePointer(); |
77 |
} |
78 |
|
79 |
/**
|
80 |
* Reads channel into a buffer. Params are ignored.
|
81 |
*
|
82 |
* @param access_mode
|
83 |
* @param i
|
84 |
* @param sz
|
85 |
* @return
|
86 |
*/
|
87 |
public ByteBufferJME map(int access_mode, int i, long sz) { |
88 |
|
89 |
ByteBufferJME resp = null;
|
90 |
|
91 |
try {
|
92 |
int s = (int) size(); |
93 |
byte[] bb = new byte[s]; |
94 |
long curr_pos = raf.getFilePointer();
|
95 |
// logger.debug("RandomFileChannelJME map, seek 0 : " + System.currentTimeMillis());
|
96 |
raf.seek(0);
|
97 |
// logger.debug("RandomFileChannelJME map, read : " + System.currentTimeMillis());
|
98 |
raf.read(bb); |
99 |
// logger.debug("RandomFileChannelJME map, seek curr_pos : " + curr_pos + " - " + System.currentTimeMillis());
|
100 |
raf.seek(curr_pos); |
101 |
// logger.debug("RandomFileChannelJME map, seek done, " + System.currentTimeMillis());
|
102 |
resp = new ByteBufferJME(bb);
|
103 |
} catch (IOException e) { |
104 |
// TODO Auto-generated catch block
|
105 |
e.printStackTrace(); |
106 |
} |
107 |
|
108 |
return resp;
|
109 |
} |
110 |
|
111 |
public byte get() throws IOException { |
112 |
return raf.readByte();
|
113 |
} |
114 |
|
115 |
public byte get(int ind) { |
116 |
|
117 |
byte resp = 0; |
118 |
try {
|
119 |
long old_pos = raf.getFilePointer();
|
120 |
raf.seek((long) ind);
|
121 |
resp = raf.readByte(); |
122 |
raf.seek(old_pos); |
123 |
} catch (IOException ioe) { |
124 |
logger.error("While reading far byte: " + ioe.getMessage());
|
125 |
} |
126 |
return resp;
|
127 |
|
128 |
} |
129 |
|
130 |
public int getInt() { |
131 |
|
132 |
int resp = 0; |
133 |
|
134 |
try {
|
135 |
resp = raf.readInt(); |
136 |
} catch (IOException e) { |
137 |
logger.error("While reading integer: " + e.getMessage());
|
138 |
} |
139 |
|
140 |
if (needsReverse()) {
|
141 |
return ByteUtil.reverseEndian(resp);
|
142 |
} else {
|
143 |
return resp;
|
144 |
} |
145 |
} |
146 |
|
147 |
public short getShort() { |
148 |
short resp = 0; |
149 |
|
150 |
try {
|
151 |
resp = raf.readShort(); |
152 |
} catch (IOException e) { |
153 |
logger.error("While reading short: " + e.getMessage());
|
154 |
} |
155 |
|
156 |
if (needsReverse()) {
|
157 |
return ByteUtil.reverseEndian(resp);
|
158 |
} else {
|
159 |
return resp;
|
160 |
} |
161 |
} |
162 |
|
163 |
public int getInt(int index) { |
164 |
int resp = 0; |
165 |
try {
|
166 |
long old_pos = raf.getFilePointer();
|
167 |
raf.seek((long) index);
|
168 |
resp = raf.readInt(); |
169 |
raf.seek(old_pos); |
170 |
} catch (IOException ioe) { |
171 |
logger.error("While reading far int: " + ioe.getMessage());
|
172 |
} |
173 |
|
174 |
if (needsReverse()) {
|
175 |
return ByteUtil.reverseEndian(resp);
|
176 |
} else {
|
177 |
return resp;
|
178 |
} |
179 |
} |
180 |
|
181 |
public void get(byte[] out) { |
182 |
|
183 |
|
184 |
byte[] aux = new byte[out.length]; |
185 |
try {
|
186 |
raf.read(aux); |
187 |
} catch (IOException e) { |
188 |
logger.error("While reading byte array: " + e.getMessage());
|
189 |
} |
190 |
|
191 |
if (needsReverse()) {
|
192 |
byte[] resp = ByteUtil.reverse(aux); |
193 |
System.arraycopy(resp, 0, out, 0, out.length); |
194 |
} else {
|
195 |
System.arraycopy(aux, 0, out, 0, out.length); |
196 |
} |
197 |
|
198 |
|
199 |
} |
200 |
|
201 |
public double getDouble() { |
202 |
|
203 |
double resp = 0; |
204 |
|
205 |
try {
|
206 |
|
207 |
long lb = 0; |
208 |
byte[] aux = new byte[8]; |
209 |
raf.read(aux); |
210 |
if (needsReverse()) {
|
211 |
byte[] aux2 = ByteUtil.reverse(aux); |
212 |
lb = ByteUtil.arrayToLongBits(aux2, 8);
|
213 |
} else {
|
214 |
lb = ByteUtil.arrayToLongBits(aux, 8);
|
215 |
} |
216 |
resp = Double.longBitsToDouble(lb);
|
217 |
|
218 |
} catch (IOException e) { |
219 |
logger.error("While reading double: " + e.getMessage());
|
220 |
} |
221 |
|
222 |
return resp;
|
223 |
} |
224 |
|
225 |
public void order(ByteOrder o) { |
226 |
byteOrder = o; |
227 |
} |
228 |
|
229 |
private boolean needsReverse() { |
230 |
return (byteOrder == ByteOrder.LITTLE_ENDIAN); |
231 |
} |
232 |
|
233 |
public void getAsIs(byte[] aux) { |
234 |
try {
|
235 |
raf.read(aux); |
236 |
} catch (IOException e) { |
237 |
logger.error("While getting byte array: " + e.getMessage());
|
238 |
} |
239 |
} |
240 |
|
241 |
} |