1 /* |
1 /* |
2 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
58 public ByteBuffer(int initialSize) { |
58 public ByteBuffer(int initialSize) { |
59 elems = new byte[initialSize]; |
59 elems = new byte[initialSize]; |
60 length = 0; |
60 length = 0; |
61 } |
61 } |
62 |
62 |
63 private void copy(int size) { |
|
64 byte[] newelems = new byte[size]; |
|
65 System.arraycopy(elems, 0, newelems, 0, elems.length); |
|
66 elems = newelems; |
|
67 } |
|
68 |
|
69 /** Append byte to this buffer. |
63 /** Append byte to this buffer. |
70 */ |
64 */ |
71 public void appendByte(int b) { |
65 public void appendByte(int b) { |
72 if (length >= elems.length) copy(elems.length * 2); |
66 elems = ArrayUtils.ensureCapacity(elems, length); |
73 elems[length++] = (byte)b; |
67 elems[length++] = (byte)b; |
74 } |
68 } |
75 |
69 |
76 /** Append `len' bytes from byte array, |
70 /** Append `len' bytes from byte array, |
77 * starting at given `start' offset. |
71 * starting at given `start' offset. |
78 */ |
72 */ |
79 public void appendBytes(byte[] bs, int start, int len) { |
73 public void appendBytes(byte[] bs, int start, int len) { |
80 while (length + len > elems.length) copy(elems.length * 2); |
74 elems = ArrayUtils.ensureCapacity(elems, length + len); |
81 System.arraycopy(bs, start, elems, length, len); |
75 System.arraycopy(bs, start, elems, length, len); |
82 length += len; |
76 length += len; |
83 } |
77 } |
84 |
78 |
85 /** Append all bytes from given byte array. |
79 /** Append all bytes from given byte array. |
89 } |
83 } |
90 |
84 |
91 /** Append a character as a two byte number. |
85 /** Append a character as a two byte number. |
92 */ |
86 */ |
93 public void appendChar(int x) { |
87 public void appendChar(int x) { |
94 while (length + 1 >= elems.length) copy(elems.length * 2); |
88 elems = ArrayUtils.ensureCapacity(elems, length + 1); |
95 elems[length ] = (byte)((x >> 8) & 0xFF); |
89 elems[length ] = (byte)((x >> 8) & 0xFF); |
96 elems[length+1] = (byte)((x ) & 0xFF); |
90 elems[length+1] = (byte)((x ) & 0xFF); |
97 length = length + 2; |
91 length = length + 2; |
98 } |
92 } |
99 |
93 |
100 /** Append an integer as a four byte number. |
94 /** Append an integer as a four byte number. |
101 */ |
95 */ |
102 public void appendInt(int x) { |
96 public void appendInt(int x) { |
103 while (length + 3 >= elems.length) copy(elems.length * 2); |
97 elems = ArrayUtils.ensureCapacity(elems, length + 3); |
104 elems[length ] = (byte)((x >> 24) & 0xFF); |
98 elems[length ] = (byte)((x >> 24) & 0xFF); |
105 elems[length+1] = (byte)((x >> 16) & 0xFF); |
99 elems[length+1] = (byte)((x >> 16) & 0xFF); |
106 elems[length+2] = (byte)((x >> 8) & 0xFF); |
100 elems[length+2] = (byte)((x >> 8) & 0xFF); |
107 elems[length+3] = (byte)((x ) & 0xFF); |
101 elems[length+3] = (byte)((x ) & 0xFF); |
108 length = length + 4; |
102 length = length + 4; |