Statistics
| Revision:

svn-gvsig-desktop / branches / Mobile_Compatible_Hito_1 / libFMap_mobile_shp_driver / src-comp / org / gvsig / datasources / impljme / ByteBufferJME.java @ 21865

History | View | Annotate | Download (6.42 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Prodevelop and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *   Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *   +34 963862235
28
 *   gvsig@gva.es
29
 *   http://www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Prodevelop Integraci?n de Tecnolog?as SL
34
 *   Conde Salvatierra de ?lava , 34-10
35
 *   46004 Valencia
36
 *   Spain
37
 *
38
 *   +34 963 510 612
39
 *   +34 963 510 968
40
 *   gis@prodevelop.es
41
 *   http://www.prodevelop.es
42
 *
43
 *    or
44
 *
45
 *   Instituto de Rob?tica
46
 *   Apartado de correos 2085
47
 *   46071 Valencia
48
 *   (Spain)
49
 *   
50
 *   +34 963 543 577
51
 *   jjordan@robotica.uv.es
52
 *   http://robotica.uv.es
53
 *   
54
 */
55

    
56
package org.gvsig.datasources.impljme;
57

    
58
import java.nio.ByteOrder;
59

    
60
import org.apache.log4j.Logger;
61
import org.gvsig.datasources.common.IByteBuffer;
62

    
63

    
64

    
65
/**
66
 * This class replaces the byte buffer used in J2SE to store a file in memory.
67
 * 
68
 * @see java.nio.ByteOrder
69
 * 
70
 * @author jldominguez
71
 *
72
 */
73
public class ByteBufferJME implements IByteBuffer {
74
        
75
        private static Logger logger = Logger.getLogger(ByteBufferJME.class);
76
        
77
        private byte[] bytes;
78
        private ByteOrder byteOrder = ByteOrder.LITTLE_ENDIAN;
79
        private int pos = 0;
80
        
81
        public ByteBufferJME(int size) {
82
                bytes = new byte[size];
83
        }
84
        
85
        public ByteBufferJME(byte[] bb) {
86
                bytes = bb;
87
        }
88

    
89
        public void get(byte[] out) {
90
                
91
                int canread = bytes.length - pos;
92
                canread = Math.min(canread, out.length);
93
                System.arraycopy(bytes, pos, out, 0, canread);
94
                pos = pos + canread;
95
        }
96

    
97
        public void put(byte[] input) {
98
                
99
                int canwrite = bytes.length - pos;
100
                if (canwrite < input.length) {
101
                        logger.error("Byte buffer too short to write " + input.length + " bytes. Nothing done.");
102
                        return;
103
                }
104

    
105
                System.arraycopy(input, 0, bytes, pos, input.length);
106
                pos = pos + input.length;
107
        }
108

    
109
        public IByteBuffer flip() {
110
                
111
                byte[] newbytes = new byte[pos];
112
                System.arraycopy(bytes, 0, newbytes, 0, pos);
113
                bytes = newbytes;
114
                pos = 0;
115
                return this;
116
        }
117

    
118
        public void order(ByteOrder o) {
119
                byteOrder = o;
120
        }
121

    
122
        public void put(byte b) {
123
                
124
                if (pos == bytes.length) {
125
                        logger.error("Cannot put one byte, we are at the end. Nothing done.");
126
                } else {
127
                        bytes[pos] = b;
128
                        pos++;
129
                }
130
        }
131

    
132
        public int position() {
133
                return pos;
134
        }
135

    
136
        public int remaining() {
137
                return bytes.length - pos;
138
        }
139

    
140
        public void rewind() {
141
                pos = 0;
142
        }
143

    
144
        public ByteOrder order() {
145
                return byteOrder;
146
        }
147

    
148
        public byte get() {
149
                
150
                if (pos == bytes.length) {
151
                        logger.error("Cannot get one byte, we are at the end. Returned 0.");
152
                        return 0;
153
                } else {
154
                        pos++;
155
                        return bytes[pos-1]; 
156
                }
157
        }
158

    
159
        public int capacity() {
160
                return bytes.length;
161
        }
162

    
163
        public IByteBuffer limit(int newlimit) {
164
                
165
                if ((newlimit <= 0) || (newlimit > bytes.length)) {
166
                        logger.error("Invalid new limit: " + newlimit + ". Nothing done.");
167
                        return this;
168
                }
169
                
170
                byte[] newbytes = new byte[newlimit];
171
                System.arraycopy(bytes, 0, newbytes, 0, newlimit);
172
                bytes = newbytes;
173
                if (pos > newlimit) {
174
                        pos = newlimit;
175
                }
176
                return this;
177
        }
178

    
179
        public int limit() {
180
                return bytes.length;
181
        }
182

    
183
        public void position(int newp) {
184
                pos = newp;
185
        }
186

    
187
        public double getDouble() {
188
                double resp = getDouble(bytes, pos);
189
                pos = pos + 8;
190
                return resp;
191
        }
192

    
193
        public int getInt() {
194
                int resp = getInt(bytes, pos);
195
                pos = pos + 4;
196
                return resp;
197
        }
198
        
199
        // ==============================
200
        
201
        private long getNumber(final byte[] data, final int offset, final int size) {
202
                
203
        long result = 0;
204
        
205
        if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
206
                
207
            for (int j = offset + size - 1; j >= offset; j--) {
208
                result <<= 8;
209
                result |= 0xff & data[j];
210
            }
211
            
212
        } else {
213

    
214
            for (int j = offset; j <= offset + size - 1; j++) {
215
                result <<= 8;
216
                result |= 0xff & data[j];
217
            }
218

    
219
        }
220

    
221
        return result;
222
    }
223
        
224
        private void putNumber(final byte[] number) {
225
                
226
        if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
227
                
228
                byte[] reversed = reverse(number);
229
                put(reversed);
230
                
231
        } else {
232
                
233
                put(number);
234
        }
235
    }
236
        
237
        
238
        private static byte[] reverse(byte[] b) {
239
                int l = b.length;
240
                byte[] resp = new byte[l];
241
                for (int i=0; i<l; i++) {
242
                        resp[i] = b[l - 1 - i]; 
243
                }
244
                return resp;
245
        }
246

    
247
        private double getDouble(final byte[] data, final int offset) {
248
                
249
        return Double.longBitsToDouble(getNumber(data, offset, 8));
250
    }
251
        
252
        private int getInt(final byte[] data, final int offset) {
253
                
254
        return (int) getNumber(data, offset, 4);
255
    }
256
        
257
        private short getShort(final byte[] data, final int offset) {
258
        return (short) getNumber(data, offset, 2);
259
    }
260

    
261
        public void putDouble(double v) {
262
                long vlong = Double.doubleToLongBits(v);
263
                byte[] vbytes = longToByteArray(vlong, 8);
264
                putNumber(vbytes);
265
    }
266
        
267
        public void putInt(int v) {
268
                byte[] vbytes = longToByteArray(v, 4);
269
                putNumber(vbytes);
270
    }
271
        
272
        public void putShort(short v) {
273
                byte[] vbytes = longToByteArray(v, 2);
274
                putNumber(vbytes);
275
    }
276
        
277
        
278
        private static byte[] longToByteArray(long v, int size) {
279
                byte[] resp = new byte[size];
280
                long aux = v;
281
                for (int i=0; i<size; i++) {
282
                        resp[size - 1 - i] = (byte) (0xff & aux);
283
                        aux >>= 8;
284
                }
285
                return resp;
286
        }
287

    
288
        public byte get(int ind) {
289
                
290
                if (ind >= bytes.length) {
291
                        logger.error("Index too large: " + ind + ". Returned zero.");
292
                        return 0;
293
                } else {
294
                        return bytes[ind];
295
                }
296
        }
297

    
298
        public short getShort() {
299
                
300
                short resp = getShort(bytes, pos);
301
                pos = pos + 2;
302
                return resp;
303
        }
304

    
305
        public int getInt(int index) {
306
                
307
                int resp = getInt(bytes, index);
308
                return resp;
309
        }
310

    
311
        public byte[] getAllBytes() {
312
                return bytes;
313
        }
314
}