svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.sqlite / org.gvsig.sqlite.provider / src / main / java / org / gvsig / sqlite / dal / geopackage / ByteReader.java @ 47456
History | View | Annotate | Download (4.78 KB)
1 |
package org.gvsig.sqlite.dal.geopackage; |
---|---|
2 |
|
3 |
import java.io.ByteArrayInputStream; |
4 |
import java.io.IOException; |
5 |
import java.io.InputStream; |
6 |
import java.nio.ByteBuffer; |
7 |
import java.nio.ByteOrder; |
8 |
import java.util.logging.Level; |
9 |
import java.util.logging.Logger; |
10 |
|
11 |
/*
|
12 |
* Based on portions of code from GeoPackage Libraries
|
13 |
* of National Geospatial-Intelligence Agency (NGA)
|
14 |
* https://github.com/ngageoint/geopackage-java
|
15 |
*/
|
16 |
|
17 |
/*friend*/ class ByteReader { |
18 |
|
19 |
/**
|
20 |
* Logger
|
21 |
*/
|
22 |
private static final Logger logger = Logger |
23 |
.getLogger(ByteReader.class.getName()); |
24 |
|
25 |
/**
|
26 |
* Default read byte order
|
27 |
*
|
28 |
* @since 2.0.3
|
29 |
*/
|
30 |
public static final ByteOrder DEFAULT_BYTE_ORDER = ByteOrder.BIG_ENDIAN; |
31 |
|
32 |
/**
|
33 |
* Character set
|
34 |
*/
|
35 |
private static final String CHAR_SET = "UTF-8"; |
36 |
|
37 |
/**
|
38 |
* Bytes to read from
|
39 |
*/
|
40 |
private final byte[] bytes; |
41 |
|
42 |
/**
|
43 |
* Input stream to read bytes from
|
44 |
*/
|
45 |
private final InputStream inputStream; |
46 |
|
47 |
/**
|
48 |
* Next byte index to read
|
49 |
*/
|
50 |
private int nextByte = 0; |
51 |
|
52 |
/**
|
53 |
* Byte order
|
54 |
*/
|
55 |
private ByteOrder byteOrder = DEFAULT_BYTE_ORDER; |
56 |
|
57 |
/**
|
58 |
* Constructor
|
59 |
*
|
60 |
* @param bytes
|
61 |
* bytes
|
62 |
*/
|
63 |
public ByteReader(byte[] bytes) { |
64 |
this.bytes = bytes;
|
65 |
inputStream = new ByteArrayInputStream(bytes); |
66 |
} |
67 |
|
68 |
/**
|
69 |
* Constructor
|
70 |
*
|
71 |
* @param inputStream
|
72 |
* input stream
|
73 |
* @since 2.0.3
|
74 |
*/
|
75 |
public ByteReader(InputStream inputStream) { |
76 |
this.bytes = null; |
77 |
this.inputStream = inputStream;
|
78 |
} |
79 |
|
80 |
/**
|
81 |
* Constructor
|
82 |
*
|
83 |
* @param bytes
|
84 |
* bytes
|
85 |
* @param byteOrder
|
86 |
* byte order
|
87 |
* @since 2.0.3
|
88 |
*/
|
89 |
public ByteReader(byte[] bytes, ByteOrder byteOrder) { |
90 |
this(bytes);
|
91 |
this.byteOrder = byteOrder;
|
92 |
} |
93 |
|
94 |
/**
|
95 |
* Constructor
|
96 |
*
|
97 |
* @param inputStream
|
98 |
* input stream
|
99 |
* @param byteOrder
|
100 |
* byte order
|
101 |
* @since 2.0.3
|
102 |
*/
|
103 |
public ByteReader(InputStream inputStream, ByteOrder byteOrder) { |
104 |
this(inputStream);
|
105 |
this.byteOrder = byteOrder;
|
106 |
} |
107 |
|
108 |
/**
|
109 |
* Get the bytes
|
110 |
*
|
111 |
* @return bytes
|
112 |
*
|
113 |
* @since 2.0.3
|
114 |
*/
|
115 |
public byte[] getBytes() { |
116 |
return bytes;
|
117 |
} |
118 |
|
119 |
/**
|
120 |
* Get the output stream
|
121 |
*
|
122 |
* @return output stream
|
123 |
*
|
124 |
* @since 2.0.3
|
125 |
*/
|
126 |
public InputStream getInputStream() { |
127 |
return inputStream;
|
128 |
} |
129 |
|
130 |
/**
|
131 |
* Close the byte reader
|
132 |
*
|
133 |
* @since 2.0.3
|
134 |
*/
|
135 |
public void close() { |
136 |
try {
|
137 |
inputStream.close(); |
138 |
} catch (IOException e) { |
139 |
logger.log(Level.WARNING,
|
140 |
"Failed to close byte reader intput stream", e);
|
141 |
} |
142 |
} |
143 |
|
144 |
/**
|
145 |
* Get the next byte to be read
|
146 |
*
|
147 |
* @return next byte to be read
|
148 |
*/
|
149 |
public int getNextByte() { |
150 |
return nextByte;
|
151 |
} |
152 |
|
153 |
/**
|
154 |
* Get the byte order
|
155 |
*
|
156 |
* @return byte order
|
157 |
*/
|
158 |
public ByteOrder getByteOrder() { |
159 |
return byteOrder;
|
160 |
} |
161 |
|
162 |
/**
|
163 |
* Set the byte order
|
164 |
*
|
165 |
* @param byteOrder
|
166 |
* byte order
|
167 |
*/
|
168 |
public void setByteOrder(ByteOrder byteOrder) { |
169 |
this.byteOrder = byteOrder;
|
170 |
} |
171 |
|
172 |
/**
|
173 |
* Read a String from the provided number of bytes
|
174 |
*
|
175 |
* @param num
|
176 |
* number of bytes
|
177 |
* @return String
|
178 |
* @throws IOException
|
179 |
* upon error
|
180 |
*/
|
181 |
public String readString(int num) throws IOException { |
182 |
return new String(readBytes(num), CHAR_SET); |
183 |
} |
184 |
|
185 |
/**
|
186 |
* Read the number of bytes
|
187 |
*
|
188 |
* @param num
|
189 |
* number of bytes
|
190 |
* @return bytes
|
191 |
* @throws IOException
|
192 |
* upon error
|
193 |
*/
|
194 |
public byte[] readBytes(int num) throws IOException { |
195 |
verifyRemainingBytes(num); |
196 |
byte[] bytes = new byte[num]; |
197 |
inputStream.read(bytes); |
198 |
nextByte += num; |
199 |
return bytes;
|
200 |
} |
201 |
|
202 |
/**
|
203 |
* Read a byte
|
204 |
*
|
205 |
* @return byte
|
206 |
* @throws IOException
|
207 |
* upon error
|
208 |
*/
|
209 |
public byte readByte() throws IOException { |
210 |
return readBytes(1)[0]; |
211 |
} |
212 |
|
213 |
/**
|
214 |
* Read an integer
|
215 |
*
|
216 |
* @return integer
|
217 |
* @throws IOException
|
218 |
* upon error
|
219 |
*/
|
220 |
public int readInt() throws IOException { |
221 |
return ByteBuffer.wrap(readBytes(4)).order(byteOrder).getInt(); |
222 |
} |
223 |
|
224 |
/**
|
225 |
* Read an unsigned integer
|
226 |
*
|
227 |
* @return unsigned integer
|
228 |
* @throws IOException
|
229 |
* upon error
|
230 |
*/
|
231 |
public long readUnsignedInt() throws IOException { |
232 |
int intValue = readInt();
|
233 |
long value = intValue & 0xffffffffL; |
234 |
return value;
|
235 |
} |
236 |
|
237 |
/**
|
238 |
* Read a double
|
239 |
*
|
240 |
* @return double
|
241 |
* @throws IOException
|
242 |
* upon error
|
243 |
*/
|
244 |
public double readDouble() throws IOException { |
245 |
return ByteBuffer.wrap(readBytes(8)).order(byteOrder).getDouble(); |
246 |
} |
247 |
|
248 |
/**
|
249 |
* Verify with the remaining bytes that there are enough remaining to read
|
250 |
* the provided amount
|
251 |
*
|
252 |
* @param bytesToRead
|
253 |
* number of bytes to read
|
254 |
*/
|
255 |
private void verifyRemainingBytes(int bytesToRead) { |
256 |
if (bytes != null && nextByte + bytesToRead > bytes.length) { |
257 |
throw new RuntimeException( |
258 |
"No more remaining bytes to read. Total Bytes: "
|
259 |
+ bytes.length + ", Bytes already read: " + nextByte
|
260 |
+ ", Attempted to read: " + bytesToRead);
|
261 |
} |
262 |
} |
263 |
|
264 |
} |