2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 public class ByteBuffer extends Buffer {
24 public static ByteBuffer allocate(int i) {
26 throw new IllegalArgumentException();
28 ByteBuffer newBuffer = new ByteBuffer(-1, 0, i, i, new byte[i], 0);
32 public static ByteBuffer allocateDirect(int capacity) {
33 return allocate(capacity);
36 ByteBuffer(int mark, int pos, int lim, int cap, byte[] hb, int offset) {
37 super(mark, pos, lim, cap);
42 ByteBuffer(int mark, int pos, int lim, int cap) {
43 this(mark, pos, lim, cap, null, 0);
46 public ByteBuffer duplicate() {
47 ByteBuffer copy = new ByteBuffer(-1, 0, capacity, capacity, new byte[capacity], 0);
52 public ByteBuffer asReadOnlyBuffer() {
56 public ByteBuffer slice() {
57 int remaining = limit - position;
58 ByteBuffer copy = new ByteBuffer(-1, 0, remaining, remaining, new byte[remaining], 0);
63 public ByteBuffer put(byte b) {
64 if (position >= limit) {
65 throw new BufferOverflowException();
72 public ByteBuffer put(int i, byte b) {
73 if ((i < 0) || (i >= limit)) {
74 throw new IndexOutOfBoundsException();
80 public ByteBuffer put(ByteBuffer src) {
82 throw new IllegalArgumentException();
85 int srcRemaining = src.remaining();
86 if (srcRemaining > remaining()) {
87 throw new BufferOverflowException();
90 System.arraycopy(src.array, src.position(), array, position, srcRemaining);
92 src.position(src.position() + srcRemaining);
93 position(position + srcRemaining);
98 public ByteBuffer put(byte[] bytes, int offset, int length) {
99 if ((offset | length | (offset + length) | (bytes.length - (offset + length))) < 0) {
100 throw new IndexOutOfBoundsException();
103 if (length > remaining()) {
104 throw new BufferOverflowException();
107 System.arraycopy(bytes, offset, array, position, length);
108 position(position + length);
113 public ByteBuffer put(byte[] bytes) {
114 return put(bytes, 0, bytes.length);
118 if (position >= limit) {
119 throw new BufferUnderflowException();
122 return array[position-1];
125 public byte get(int i) {
126 if ((i < 0) || (i >= limit)) {
127 throw new IndexOutOfBoundsException();
132 public ByteBuffer get(byte[] bytes) {
133 return get(bytes, 0, bytes.length);
136 public ByteBuffer get(byte[] bytes, int offset, int length) {
137 if ((offset | length | (offset + length) | (bytes.length - (offset + length))) < 0) {
138 throw new IndexOutOfBoundsException();
141 if (length > remaining()) {
142 throw new BufferUnderflowException();
145 int end = offset + length;
146 for (int i = offset; i < end; i++) {
152 public ByteBuffer order(ByteOrder order) {
156 /***************************************************************
157 * public char getChar()
158 * @return 16 bit (UTF-16) char of ByteBuffer at this.position
159 * Caution: 8 or 32 bit character encodings are not supported.
161 public char getChar() {
162 char res=getChar(this.position);
167 /***************************************************************
168 * public char getChar(int pos)
169 * @return 16 bit (UTF-16) char of ByteBuffer at int pos
170 * Caution: 8 or 32 bit character encodings are not supported.
172 public char getChar(int pos) {
173 if (limit - pos < 2) {
174 throw new BufferUnderflowException();
176 int x1 = (array[pos] & 0xff) << 8;
177 int x0 = (array[pos+1] & 0xff);
179 return (char) (x1 | x0);
182 /***************************************************************
183 * public ByteBuffer putChar(char c)
184 * @return insert 16 bit (UTF-16) char c at this.position
185 * Caution: 8 or 32 bit character encodings are not supported.
187 public ByteBuffer putChar(char c) {
188 if (limit - position < 2) {
189 throw new BufferOverflowException();
191 array[position] = (byte)(c >> 8);
192 array[position+1] = (byte)(c );
198 public int getInt() {
199 if (limit - position < 4) {
200 throw new BufferUnderflowException();
203 int x3 = (array[position ] ) << 24;
204 int x2 = (array[position+1] & 0xff) << 16;
205 int x1 = (array[position+2] & 0xff) << 8;
206 int x0 = (array[position+3] & 0xff);
209 return (x3 | x2 | x1 | x0);
212 public ByteBuffer putInt(int x) {
213 if (limit - position < 4) {
214 throw new BufferOverflowException();
217 array[position ] = (byte)(x >> 24);
218 array[position+1] = (byte)(x >> 16);
219 array[position+2] = (byte)(x >> 8);
220 array[position+3] = (byte)(x );
226 public long getLong() {
227 if (limit - position < 8) {
228 throw new BufferUnderflowException();
231 long x7 = ((long)(array[position ] ) << 56);
232 long x6 = ((long)(array[position+1] & 0xff) << 48);
233 long x5 = ((long)(array[position+2] & 0xff) << 40);
234 long x4 = ((long)(array[position+3] & 0xff) << 32);
235 long x3 = ((long)(array[position+4] & 0xff) << 24);
236 long x2 = ((long)(array[position+5] & 0xff) << 16);
237 long x1 = ((long)(array[position+6] & 0xff) << 8);
238 long x0 = (array[position+7] & 0xff );
241 return (x7 | x6 | x5 | x4 | x3 | x2 | x1 | x0);
244 public ByteBuffer putLong(long x) {
245 if (limit - position < 8) {
246 throw new BufferOverflowException();
249 array[position ] = (byte)((x >> 56) );
250 array[position+1] = (byte)((x >> 48) & 0xff);
251 array[position+2] = (byte)((x >> 40) & 0xff);
252 array[position+3] = (byte)((x >> 32) & 0xff);
253 array[position+4] = (byte)((x >> 24) & 0xff);
254 array[position+5] = (byte)((x >> 16) & 0xff);
255 array[position+6] = (byte)((x >> 8) & 0xff);
256 array[position+7] = (byte)((x ) & 0xff);
263 public byte[] array() {
268 public boolean hasArray() {
272 public ByteBuffer compact() {
273 int pos = position();
275 int cap = capacity();
278 byte[] newArray = new byte[cap];
279 System.arraycopy(array, pos, newArray, 0, rem);
287 public static ByteBuffer wrap(byte[] outMess) {
288 ByteBuffer byteBuffer = new ByteBuffer(-1, 0, outMess.length, outMess.length, new byte[outMess.length], 0);
290 System.arraycopy(outMess, 0 , byteBuffer.array, 0, outMess.length);