summaryrefslogtreecommitdiffstats
path: root/obex/javax/obex/ObexByteBuffer.java
blob: 1bf68b6943104e529317a6ecb37ef2c27a9f513d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package javax.obex;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

class ObexByteBuffer {
    private static final int REALLOC_EXTRA_SPACE = 24;

    private byte[] mBuffer;

    private int mIndex;

    private int mLength;

    public ObexByteBuffer(int initialSize) {
        mBuffer = new byte[initialSize];
        mIndex = 0;
        mLength = 0;
    }

    /**
     * Mark bytes at beginning or valid data as invalid.
     * @param numBytes Number of bytes to consume.
     */
    private void consume(int numBytes) {
        mLength -= numBytes;
        if (mLength > 0) {
            mIndex += numBytes;
        } else {
            mIndex = 0;
        }
    }

    /**
     * Make room in for new data (if needed).
     * @param numBytes Number of bytes to make room for.
     */
    private void aquire(int numBytes) {
        int remainingSpace = mBuffer.length - (mIndex + mLength);

        // Do we need to grow or shuffle?
        if (remainingSpace < numBytes) {
            int availableSpace = mBuffer.length - mLength;
            if (availableSpace < numBytes) {
                // Need to grow. Add some extra space to avoid small growth.
                byte[] newbuf = new byte[mLength + numBytes + REALLOC_EXTRA_SPACE];
                System.arraycopy(mBuffer, mIndex, newbuf, 0, mLength);
                mBuffer = newbuf;
            } else {
                // Need to shuffle
                System.arraycopy(mBuffer, mIndex, mBuffer, 0, mLength);
            }
            mIndex = 0;
        }
    }

    /**
     * Get the internal byte array. Use with care.
     * @return the internal byte array
     */
    public byte[] getBytes() {
        return mBuffer;
    }

    /**
     * Get number of written but not consumed bytes.
     * @return number of bytes
     */
    public int getLength() {
        return mLength;
    }

    /**
     * Discard all unconsumed bytes.
     */
    public void reset() {
        mIndex = 0;
        mLength = 0;
    }

    /**
     * Read and consume one byte.
     * @return Next unconsumed byte.
     */
    public byte read() {
        if (mLength == 0) {
            throw new ArrayIndexOutOfBoundsException();
        }
        mLength--;
        return mBuffer[mIndex++];
    }

    /**
     * Read and consume bytes, and write them into a byte array.
     * Will read (dest.length - destOffset) bytes.
     * @param dest Array to copy data into.
     * @param destOffset Where to start writing in dest.
     * @return number of read bytes.
     */
    public int read(byte[] dest, int destOffset) {
        return read(dest, destOffset, mLength);
    }

    /**
     * Read and consume bytes, and write them into a byte array.
     * Will read (length - destOffset) bytes.
     * @param dest Array to copy data into.
     * @param destOffset Where to start writing in dest.
     * @param length Number of bytes to read.
     * @return number of read bytes.
     */
    public int read(byte[] dest, int destOffset, int length) {
        peek(0, dest, destOffset, length);
        consume(length);
        return length;
    }

    /**
     * Read and consume bytes, and write them into another ObexByteBuffer.
     * @param dest ObexByteBuffer to copy data into.
     * @param length Number of bytes to read.
     * @return number of read bytes.
     */
    public int read(ObexByteBuffer dest, int length) {
        if (length > mLength) {
            throw new ArrayIndexOutOfBoundsException();
        }
        dest.write(mBuffer, mIndex, length);
        consume(length);

        return length;
    }

    /**
     * Read and consume all unconsumed bytes, and write them into an OutputStream.
     * @param dest OutputStream to copy data into.
     * @return number of read bytes.
     */
    public int read(OutputStream stream) throws IOException {
        return read(stream, mLength);
    }

    /**
     * Read and consume bytes, and write them into an OutputStream.
     * @param dest OutputStream to copy data into.
     * @param length Number of bytes to read.
     * @return number of read bytes.
     */
    public int read(OutputStream destStream, int length) throws IOException {
        peek(destStream, length);
        consume(length);
        return length;
    }

    /**
     * Read (but don't consume) one byte.
     * @param offset Offset into unconsumed bytes.
     * @return Requested unconsumed byte.
     */
    public byte peek(int offset) {
        if (offset > mLength) {
            throw new ArrayIndexOutOfBoundsException();
        }
        return mBuffer[mIndex + offset];
    }

    /**
     * Read (but don't consume) bytes and write them into a byte array.
     * Will read dest.length bytes.
     * @param offset Offset into unconsumed bytes.
     * @param dest Array to copy data into.
     */
    public void peek(int offset, byte[] dest) {
        peek(offset, dest, 0, dest.length);
    }

    /**
     * Read (but don't consume) bytes and write them into a byte array.
     * Will read (length - destOffset) bytes.
     * @param offset Offset into unconsumed bytes.
     * @param dest Array to copy data into.
     * @param destOffset Where to start writing in dest.
     * @param length Number of bytes to read.
     */
    public void peek(int offset, byte[] dest, int destOffset, int length) {
        if (offset > mLength || (offset + length) > mLength) {
            throw new ArrayIndexOutOfBoundsException();
        }
        System.arraycopy(mBuffer, mIndex + offset, dest, destOffset, length);
    }

    /**
     * Read (but don't consume) bytes, and write them into an OutputStream.
     * @param dest OutputStream to copy data into.
     * @param length Number of bytes to read.
     */
    public void peek(OutputStream stream, int length) throws IOException {
        if (length > mLength) {
            throw new ArrayIndexOutOfBoundsException();
        }
        stream.write(mBuffer, mIndex, length);
    }

    /**
     * Write a new byte.
     * @param src Byte to write.
     */
    public void write(byte src) {
        aquire(1);
        mBuffer[mIndex + mLength] = src;
        mLength++;
    }

    /**
     * Read bytes from a byte array and add to unconsumed bytes.
     * Will read/write src.length bytes.
     * @param src Array to read from.
     */
    public void write(byte[] src) {
        write(src, 0, src.length);
    }

    /**
     * Read bytes from a byte array and add to unconsumed bytes.
     * Will read/write (src.length - srcOffset) bytes.
     * @param src Array to read from.
     * @param srcOffset Offset into source array.
     */
    public void write(byte[] src, int srcOffset) {
        write(src, srcOffset, src.length - srcOffset);
    }

    /**
     * Read bytes from a byte array and add to unconsumed bytes.
     * Will read/write (srcLength - srcOffset) bytes.
     * @param src Array to read from.
     * @param srcOffset Offset into source array.
     * @param srcLength Number of bytes to read/write.
     */
    public void write(byte[] src, int srcOffset, int srcLength) {
        // Make sure we have space.
        aquire(srcLength);

        // Add the new data at the end
        System.arraycopy(src, srcOffset, mBuffer, mIndex + mLength, srcLength);
        mLength += srcLength;
    }

    /**
     * Read bytes from another ObexByteBuffer and add to unconsumed bytes.
     * Will read/write src.getLength() bytes. The bytes in src will not be consumed.
     * @param src ObexByteBuffer to read from.
     * @param srcOffset Offset into source array.
     */
    public void write(ObexByteBuffer src) {
        write(src.mBuffer, 0, src.getLength());
    }

    /**
     * Read bytes from another ObexByteBuffer and add to unconsumed bytes.
     * Will read/write (src.getLength() - srcOffset) bytes. The bytes in src will not
     * be consumed.
     * @param src ObexByteBuffer to read from.
     * @param srcOffset Offset into source array.
     */
    public void write(ObexByteBuffer src, int srcOffset) {
        if (srcOffset > src.mLength) {
            throw new ArrayIndexOutOfBoundsException();
        }
        write(src.mBuffer, src.mIndex + srcOffset, src.mLength - src.mIndex - srcOffset);
    }

    /**
     * Read bytes from another ObexByteBuffer and add to unconsumed bytes.
     * Will read/write (srcLength - srcOffset) bytes. The bytes in src will not be
     * consumed.
     * @param src ObexByteBuffer to read from.
     * @param srcOffset Offset into source array.
     * @param srcLength Number of bytes to read/write.
     */
    public void write(ObexByteBuffer src, int srcOffset, int srcLength) {
        if (srcOffset > src.mLength || (srcOffset + srcLength) > src.mLength) {
            throw new ArrayIndexOutOfBoundsException();
        }
        write(src.mBuffer, src.mIndex + srcOffset, srcLength);
    }

    /**
     * Read bytes from an InputStream and add to unconsumed bytes.
     * @param src InputStream to read from
     * @param srcLength Number of bytes to read
     * @throws IOException
     */
    public void write(InputStream src, int srcLength) throws IOException {
        // First make sure we have space.
        aquire(srcLength);

        // Read data until the requested number of bytes have been read.
        int numBytes = 0;
        do {
            int readBytes = src.read(mBuffer, mIndex + mLength + numBytes, srcLength - numBytes);
            if (readBytes == -1) {
                throw new IOException();
            }
            numBytes += readBytes;
        } while (numBytes != srcLength);
        mLength += numBytes;
    }
}