Statistics
| Revision:

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 / ByteWriter.java @ 47456

History | View | Annotate | Download (4.22 KB)

1
package org.gvsig.sqlite.dal.geopackage;
2

    
3
import java.io.ByteArrayOutputStream;
4
import java.io.IOException;
5
import java.io.OutputStream;
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 ByteWriter {
18

    
19
        /**
20
         * Logger
21
         */
22
        private static final Logger logger = Logger
23
                        .getLogger(ByteWriter.class.getName());
24

    
25
        /**
26
         * Default write byte order
27
         * 
28
         * @since 2.0.3
29
         */
30
        public static final ByteOrder DEFAULT_BYTE_ORDER = ByteOrder.BIG_ENDIAN;
31

    
32
        /**
33
         * Output stream to write bytes to
34
         */
35
        private final OutputStream outputStream;
36

    
37
        /**
38
         * Byte order
39
         */
40
        private ByteOrder byteOrder = DEFAULT_BYTE_ORDER;
41

    
42
        /**
43
         * Constructor
44
         */
45
        public ByteWriter() {
46
                outputStream = new ByteArrayOutputStream();
47
        }
48

    
49
        /**
50
         * Constructor
51
         * 
52
         * @param outputStream
53
         *            output stream
54
         * @since 2.0.3
55
         */
56
        public ByteWriter(OutputStream outputStream) {
57
                this.outputStream = outputStream;
58
        }
59

    
60
        /**
61
         * Constructor
62
         * 
63
         * @param byteOrder
64
         *            byte order
65
         * @since 2.0.3
66
         */
67
        public ByteWriter(ByteOrder byteOrder) {
68
                this();
69
                this.byteOrder = byteOrder;
70
        }
71

    
72
        /**
73
         * Constructor
74
         * 
75
         * @param outputStream
76
         *            output stream
77
         * @param byteOrder
78
         *            byte order
79
         * @since 2.0.3
80
         */
81
        public ByteWriter(OutputStream outputStream, ByteOrder byteOrder) {
82
                this(outputStream);
83
                this.byteOrder = byteOrder;
84
        }
85

    
86
        /**
87
         * Get the output stream
88
         * 
89
         * @return output stream
90
         * @since 2.0.3
91
         */
92
        public OutputStream getOutputStream() {
93
                return outputStream;
94
        }
95

    
96
        /**
97
         * Get the output stream
98
         * 
99
         * @return output stream
100
         * @since 2.0.3
101
         */
102
        public ByteArrayOutputStream getByteArrayOutputStream() {
103
                if (!(outputStream instanceof ByteArrayOutputStream)) {
104
                        throw new RuntimeException(
105
                                        "Output Stream is not a ByteArrayOutputStream: "
106
                                                        + outputStream.getClass().getName());
107
                }
108
                return (ByteArrayOutputStream) outputStream;
109
        }
110

    
111
        /**
112
         * Close the byte writer
113
         */
114
        public void close() {
115
                try {
116
                        outputStream.close();
117
                } catch (IOException e) {
118
                        logger.log(Level.WARNING,
119
                                        "Failed to close byte writer output stream", e);
120
                }
121
        }
122

    
123
        /**
124
         * Get the byte order
125
         * 
126
         * @return byte order
127
         */
128
        public ByteOrder getByteOrder() {
129
                return byteOrder;
130
        }
131

    
132
        /**
133
         * Set the byte order
134
         * 
135
         * @param byteOrder
136
         *            byte order
137
         */
138
        public void setByteOrder(ByteOrder byteOrder) {
139
                this.byteOrder = byteOrder;
140
        }
141

    
142
        /**
143
         * Get the written bytes
144
         * 
145
         * @return written bytes
146
         */
147
        public byte[] getBytes() {
148
                return getByteArrayOutputStream().toByteArray();
149
        }
150

    
151
        /**
152
         * Get the current size in bytes written
153
         * 
154
         * @return bytes written
155
         */
156
        public int size() {
157
                return getByteArrayOutputStream().size();
158
        }
159

    
160
        /**
161
         * Write a String
162
         * 
163
         * @param value
164
         *            string value
165
         * @throws IOException
166
         *             upon error
167
         */
168
        public void writeString(String value) throws IOException {
169
                byte[] valueBytes = value.getBytes();
170
                outputStream.write(valueBytes);
171
        }
172

    
173
        /**
174
         * Write a byte
175
         * 
176
         * @param value
177
         *            byte
178
         * @throws IOException
179
         *             upon error
180
         */
181
        public void writeByte(byte value) throws IOException {
182
                outputStream.write(value);
183
        }
184

    
185
        /**
186
         * Write an integer
187
         * 
188
         * @param value
189
         *            int value
190
         * @throws IOException
191
         *             upon error
192
         */
193
        public void writeInt(int value) throws IOException {
194
                byte[] valueBytes = new byte[4];
195
                ByteBuffer byteBuffer = ByteBuffer.allocate(4).order(byteOrder)
196
                                .putInt(value);
197
                byteBuffer.flip();
198
                byteBuffer.get(valueBytes);
199
                outputStream.write(valueBytes);
200
        }
201

    
202
        /**
203
         * Write a double
204
         * 
205
         * @param value
206
         *            double
207
         * @throws IOException
208
         *             upon error
209
         */
210
        public void writeDouble(double value) throws IOException {
211
                byte[] valueBytes = new byte[8];
212
                ByteBuffer byteBuffer = ByteBuffer.allocate(8).order(byteOrder)
213
                                .putDouble(value);
214
                byteBuffer.flip();
215
                byteBuffer.get(valueBytes);
216
                outputStream.write(valueBytes);
217
        }
218

    
219
        public void writeBytes(byte[] value) throws IOException {
220
                outputStream.write(value, 0, value.length);
221
        }
222
}