Initial import
[jpf-core.git] / src / classes / java / nio / ByteBuffer.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
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
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
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.
17  */
18 package java.nio;
19
20 public class ByteBuffer extends Buffer {
21         byte[] array;
22
23         public static ByteBuffer allocate(int i) {
24                 if (i < 0) {
25                         throw new IllegalArgumentException();
26                 }
27                 ByteBuffer newBuffer = new ByteBuffer(i);
28                 return newBuffer;
29         }
30
31         public static ByteBuffer allocateDirect(int capacity) {
32                 return allocate(capacity);
33         }
34
35         protected ByteBuffer(int i) {
36                 capacity = i;
37                 this.clear();
38                 array = new byte[i];
39         }
40
41         public ByteBuffer duplicate() {
42                 ByteBuffer copy = new ByteBuffer(capacity);
43                 copy.array = array;
44                 return copy;
45         }
46
47         public ByteBuffer asReadOnlyBuffer() {
48                 return duplicate();
49         }
50
51         public ByteBuffer slice() {
52                 int remaining = limit - position;
53                 ByteBuffer copy = new ByteBuffer(remaining);
54                 copy.array = array;
55                 return copy;
56         }
57
58         public ByteBuffer put(byte b) {
59                 if (position >= limit) {
60                         throw new BufferOverflowException();
61                 }
62                 array[position] = b;
63                 position++;
64                 return this;
65         }
66
67         public ByteBuffer put(int i, byte b) {
68                 if ((i < 0) || (i >= limit)) {
69                         throw new IndexOutOfBoundsException();
70                 }
71                 array[i] = b;
72                 return this;
73         }
74
75         public ByteBuffer put(ByteBuffer src) {
76                 if (src == this) {
77                         throw new IllegalArgumentException();
78                 }
79
80                 int srcRemaining = src.remaining();
81                 if (srcRemaining > remaining()) {
82                         throw new BufferOverflowException();
83                 }
84
85                 System.arraycopy(src.array, src.position(), array, position, srcRemaining);
86
87                 src.position(src.position() + srcRemaining);
88                 position(position + srcRemaining);
89
90                 return this;
91         }
92
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();
96                 }
97
98                 if (length > remaining()) {
99                         throw new BufferOverflowException();
100                 }
101
102                 System.arraycopy(bytes, offset, array, position, length);
103                 position(position + length);
104
105                 return this;
106         }
107
108         public ByteBuffer put(byte[] bytes) {
109                 return put(bytes, 0, bytes.length);
110         }
111
112         public byte get() {
113                 if (position >= limit) {
114                         throw new BufferUnderflowException();
115                 }
116                 position++;
117                 return array[position-1];
118         }
119
120         public byte get(int i) {
121                 if ((i < 0) || (i >= limit)) {
122                         throw new IndexOutOfBoundsException();
123                 }
124                 return array[i];
125         }
126
127         public ByteBuffer get(byte[] bytes) {
128                 return get(bytes, 0, bytes.length);
129         }
130
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();
134                 }
135
136                 if (length > remaining()) {
137                         throw new BufferUnderflowException();
138                 }
139
140                 int end = offset + length;
141                 for (int i = offset; i < end; i++) {
142                         bytes[i] = get();
143                 }
144                 return this;
145         }
146
147         public ByteBuffer order(ByteOrder order) {
148                 return this;
149         }
150
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.
155          */
156         public char getChar() {
157         char res=getChar(this.position);
158         this.position+=2;
159         return res;
160         }
161
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.
166          */
167         public char getChar(int pos) {
168                 if (limit - pos < 2) {
169                         throw new BufferUnderflowException();
170                 }
171                 int x1 = (array[pos]   & 0xff) << 8;
172                 int x0 = (array[pos+1] & 0xff);
173
174                 return (char) (x1 | x0);
175         }
176
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.
181          */
182         public ByteBuffer putChar(char c) {
183                 if (limit - position < 2) {
184                         throw new BufferOverflowException();
185                 }
186                 array[position]   = (byte)(c >> 8);
187                 array[position+1] = (byte)(c     );
188                 position += 2;
189
190                 return this;
191         }
192
193         public int getInt() {
194                 if (limit - position < 4) {
195                         throw new BufferUnderflowException();
196                 }
197
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);
202                 position += 4;
203
204                 return (x3 | x2 | x1 | x0);
205         }
206
207         public ByteBuffer putInt(int x) {
208                 if (limit - position < 4) {
209                         throw new BufferOverflowException();
210                 }
211
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      );
216                 position += 4;
217
218                 return this;
219         }
220
221         public long getLong() {
222                 if (limit - position < 8) {
223                         throw new BufferUnderflowException();
224                 }
225
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      );
234                 position += 8;
235
236                 return (x7 | x6 | x5 | x4 | x3 | x2 | x1 | x0);
237         }
238
239         public ByteBuffer putLong(long x) {
240                 if (limit - position < 8) {
241                         throw new BufferOverflowException();
242                 }
243
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);
252                 position += 8;
253
254                 return this;
255         }
256
257         @Override
258         public byte[] array() {
259                 return array;
260         }
261
262         @Override
263         public boolean hasArray() {
264                 return true;
265         }
266
267         public ByteBuffer compact() {
268                 int pos = position();
269                 int lim = limit();
270                 int cap = capacity();
271                 int rem = lim - pos;
272
273                 byte[] newArray = new byte[cap];
274                 System.arraycopy(array, pos, newArray, 0, rem);
275                 array = newArray;
276
277                 position(rem);
278                 limit(cap);
279                 return this;
280         }
281
282         public static ByteBuffer wrap(byte[] outMess) {
283                 ByteBuffer byteBuffer = new ByteBuffer(outMess.length);
284                 byteBuffer.clear();
285                 System.arraycopy(outMess, 0 , byteBuffer.array, 0, outMess.length);
286                 return byteBuffer;
287         }
288 }