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 {
23 public static ByteBuffer allocate(int i) {
25 throw new IllegalArgumentException();
27 ByteBuffer newBuffer = new ByteBuffer(i);
31 public static ByteBuffer allocateDirect(int capacity) {
32 return allocate(capacity);
35 protected ByteBuffer(int i) {
41 public ByteBuffer duplicate() {
42 ByteBuffer copy = new ByteBuffer(capacity);
47 public ByteBuffer asReadOnlyBuffer() {
51 public ByteBuffer slice() {
52 int remaining = limit - position;
53 ByteBuffer copy = new ByteBuffer(remaining);
58 public ByteBuffer put(byte b) {
59 if (position >= limit) {
60 throw new BufferOverflowException();
67 public ByteBuffer put(int i, byte b) {
68 if ((i < 0) || (i >= limit)) {
69 throw new IndexOutOfBoundsException();
75 public ByteBuffer put(ByteBuffer src) {
77 throw new IllegalArgumentException();
80 int srcRemaining = src.remaining();
81 if (srcRemaining > remaining()) {
82 throw new BufferOverflowException();
85 System.arraycopy(src.array, src.position(), array, position, srcRemaining);
87 src.position(src.position() + srcRemaining);
88 position(position + srcRemaining);
93 public ByteBuffer put(byte[] bytes, int offset, int length) {
94 if ((offset | length | (offset + length) | (bytes.length - (offset + length))) < 0) {
95 throw new IndexOutOfBoundsException();
98 if (length > remaining()) {
99 throw new BufferOverflowException();
102 System.arraycopy(bytes, offset, array, position, length);
103 position(position + length);
108 public ByteBuffer put(byte[] bytes) {
109 return put(bytes, 0, bytes.length);
113 if (position >= limit) {
114 throw new BufferUnderflowException();
117 return array[position-1];
120 public byte get(int i) {
121 if ((i < 0) || (i >= limit)) {
122 throw new IndexOutOfBoundsException();
127 public ByteBuffer get(byte[] bytes) {
128 return get(bytes, 0, bytes.length);
131 public ByteBuffer get(byte[] bytes, int offset, int length) {
132 if ((offset | length | (offset + length) | (bytes.length - (offset + length))) < 0) {
133 throw new IndexOutOfBoundsException();
136 if (length > remaining()) {
137 throw new BufferUnderflowException();
140 int end = offset + length;
141 for (int i = offset; i < end; i++) {
147 public ByteBuffer order(ByteOrder order) {
151 /***************************************************************
152 * public char getChar()
153 * @return 16 bit (UTF-16) char of ByteBuffer at this.position
154 * Caution: 8 or 32 bit character encodings are not supported.
156 public char getChar() {
157 char res=getChar(this.position);
162 /***************************************************************
163 * public char getChar(int pos)
164 * @return 16 bit (UTF-16) char of ByteBuffer at int pos
165 * Caution: 8 or 32 bit character encodings are not supported.
167 public char getChar(int pos) {
168 if (limit - pos < 2) {
169 throw new BufferUnderflowException();
171 int x1 = (array[pos] & 0xff) << 8;
172 int x0 = (array[pos+1] & 0xff);
174 return (char) (x1 | x0);
177 /***************************************************************
178 * public ByteBuffer putChar(char c)
179 * @return insert 16 bit (UTF-16) char c at this.position
180 * Caution: 8 or 32 bit character encodings are not supported.
182 public ByteBuffer putChar(char c) {
183 if (limit - position < 2) {
184 throw new BufferOverflowException();
186 array[position] = (byte)(c >> 8);
187 array[position+1] = (byte)(c );
193 public int getInt() {
194 if (limit - position < 4) {
195 throw new BufferUnderflowException();
198 int x3 = (array[position ] ) << 24;
199 int x2 = (array[position+1] & 0xff) << 16;
200 int x1 = (array[position+2] & 0xff) << 8;
201 int x0 = (array[position+3] & 0xff);
204 return (x3 | x2 | x1 | x0);
207 public ByteBuffer putInt(int x) {
208 if (limit - position < 4) {
209 throw new BufferOverflowException();
212 array[position ] = (byte)(x >> 24);
213 array[position+1] = (byte)(x >> 16);
214 array[position+2] = (byte)(x >> 8);
215 array[position+3] = (byte)(x );
221 public long getLong() {
222 if (limit - position < 8) {
223 throw new BufferUnderflowException();
226 long x7 = ((long)(array[position ] ) << 56);
227 long x6 = ((long)(array[position+1] & 0xff) << 48);
228 long x5 = ((long)(array[position+2] & 0xff) << 40);
229 long x4 = ((long)(array[position+3] & 0xff) << 32);
230 long x3 = ((long)(array[position+4] & 0xff) << 24);
231 long x2 = ((long)(array[position+5] & 0xff) << 16);
232 long x1 = ((long)(array[position+6] & 0xff) << 8);
233 long x0 = (array[position+7] & 0xff );
236 return (x7 | x6 | x5 | x4 | x3 | x2 | x1 | x0);
239 public ByteBuffer putLong(long x) {
240 if (limit - position < 8) {
241 throw new BufferOverflowException();
244 array[position ] = (byte)((x >> 56) );
245 array[position+1] = (byte)((x >> 48) & 0xff);
246 array[position+2] = (byte)((x >> 40) & 0xff);
247 array[position+3] = (byte)((x >> 32) & 0xff);
248 array[position+4] = (byte)((x >> 24) & 0xff);
249 array[position+5] = (byte)((x >> 16) & 0xff);
250 array[position+6] = (byte)((x >> 8) & 0xff);
251 array[position+7] = (byte)((x ) & 0xff);
258 public byte[] array() {
263 public boolean hasArray() {
267 public ByteBuffer compact() {
268 int pos = position();
270 int cap = capacity();
273 byte[] newArray = new byte[cap];
274 System.arraycopy(array, pos, newArray, 0, rem);
282 public static ByteBuffer wrap(byte[] outMess) {
283 ByteBuffer byteBuffer = new ByteBuffer(outMess.length);
285 System.arraycopy(outMess, 0 , byteBuffer.array, 0, outMess.length);