Statistics
| Revision:

svn-gvsig-desktop / branches / Mobile_Compatible_Hito_1 / libFMap_mobile_shp_driver / src-file / org / gvsig / data / datastores / vectorial / file / FalseByteBuffer.java @ 21865

History | View | Annotate | Download (4.49 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.data.datastores.vectorial.file;
57

    
58
import java.nio.ByteOrder;
59

    
60
/**
61
 * This class replaces the byte buffer used in J2SE to store a file in memory.
62
 * 
63
 * @see java.nio.ByteOrder
64
 * 
65
 * @author jldominguez
66
 *
67
 */
68
public class FalseByteBuffer {
69
        
70
        private byte[] bytes;
71
        private ByteOrder byteOrder = ByteOrder.LITTLE_ENDIAN;
72
        private int pos = 0;
73
        
74
        /**
75
         * Constructor
76
         * @param bb array of bytes to be stored
77
         */
78
        public FalseByteBuffer(byte[] bb) {
79
                bytes = bb;
80
        }
81
        
82
        /**
83
         * Moves file cursor to position p 
84
         * @param p the new position
85
         */
86
        public void position(int p) {
87
                pos = p;
88
        }
89
        
90
        /**
91
         * 
92
         * @return current cursor position
93
         */
94
        public int getPosition() {
95
                return pos;
96
        }
97
        
98
        /**
99
         * @return byte pointed by cursor then increases cursor
100
         */
101
        public byte getByte() {
102
                byte resp = bytes[pos];
103
                pos++;
104
                return resp;
105
        }
106
        
107
        /**
108
         * Gets one byte
109
         * @param ind index of the byte of interest 
110
         * @return the byte of interest
111
         */
112
        public byte getByte(int ind) {
113
                return bytes[ind];
114
        }
115

    
116
        /**
117
         * @return integer pointed by cursor then increases cursor
118
         */
119
        public int getInt() {
120
                int resp = getInt(bytes, pos);
121
                pos = pos + 4;
122
                return resp;
123
        }
124
        
125
        /**
126
         * @return short pointed by cursor then increases cursor
127
         */
128
        public short getShort() {
129
                short resp = getShort(bytes, pos);
130
                pos = pos + 2;
131
                return resp;
132
        }
133
        
134
        /**
135
         * Gets the integer at a certain position
136
         * @param index the index of interest
137
         * @return the integer at a certain position
138
         */
139
        public int getInt(int index) {
140
                int resp = getInt(bytes, index);
141
                return resp;
142
        }
143
        
144
        /**
145
         * Gets some bytes.
146
         * @param out the array of bytes to be filled
147
         */
148
        public void getBytes(byte[] out) {
149
                for (int i=0; i<out.length; i++) {
150
                        out[i] = getByte(pos + i);
151
                }
152
                pos = pos + out.length;
153
        }
154
        
155
        /**
156
         * @return double pointed by cursor then increases cursor
157
         */
158
        public double getDouble() {
159
                double resp = getDouble(bytes, pos);
160
                pos = pos + 8;
161
                return resp;
162
        }
163
        
164
        private int getInt(final byte[] data, final int offset) {
165
        return (int) getNumber(data, offset, 4);
166
    }
167
        
168

    
169
        private short getShort(final byte[] data, final int offset) {
170
        return (short) getNumber(data, offset, 2);
171
    }
172

    
173
        
174
        private long getNumber(final byte[] data, final int offset, final int size) {
175
        long result = 0;
176
        
177
        if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
178
                
179
            for (int j = offset + size - 1; j >= offset; j--) {
180
                result <<= 8;
181
                result |= 0xff & data[j];
182
            }
183
            
184
        } else {
185

    
186
            for (int j = offset; j <= offset + size - 1; j++) {
187
                result <<= 8;
188
                result |= 0xff & data[j];
189
            }
190

    
191
        }
192

    
193
        return result;
194
    }
195
        
196
        private double getDouble(final byte[] data, final int offset) {
197
        return Double.longBitsToDouble(getNumber(data, offset, 8));
198
    }
199

    
200
        /**
201
         * Sets byte order (big endian, little endian)
202
         * @param o byte order (big endian, little endian)
203
         */
204
        public void setByteOrder(ByteOrder o) {
205
                byteOrder = o;
206
        }
207

    
208

    
209

    
210
}