mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2025-01-09 00:14:10 -08:00
Refactor ShortBuffer and IntBuffer. Implement ByteBuffer.asIntBuffer and
.asShortBuffer
This commit is contained in:
parent
8dd5661dfa
commit
cfcd25b159
|
@ -200,4 +200,8 @@ public abstract class TByteBuffer extends TBuffer implements TComparable<TByteBu
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract TCharBuffer asCharBuffer();
|
public abstract TCharBuffer asCharBuffer();
|
||||||
|
|
||||||
|
public abstract TShortBuffer asShortBuffer();
|
||||||
|
|
||||||
|
public abstract TIntBuffer asIntBuffer();
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,6 +106,27 @@ class TByteBufferImpl extends TByteBuffer {
|
||||||
@Override
|
@Override
|
||||||
public TCharBuffer asCharBuffer() {
|
public TCharBuffer asCharBuffer() {
|
||||||
int sz = remaining() / 2;
|
int sz = remaining() / 2;
|
||||||
return new TCharBufferOverByteBuffer(start + position, sz, this, 0, sz, isReadOnly());
|
TCharBufferOverByteBuffer result = new TCharBufferOverByteBuffer(start + position, sz, this, 0, sz,
|
||||||
|
isReadOnly());
|
||||||
|
result.byteOrder = order;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TShortBuffer asShortBuffer() {
|
||||||
|
int sz = remaining() / 2;
|
||||||
|
TShortBufferOverByteBuffer result = new TShortBufferOverByteBuffer(start + position, sz, this, 0, sz,
|
||||||
|
isReadOnly());
|
||||||
|
result.byteOrder = order;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TIntBuffer asIntBuffer() {
|
||||||
|
int sz = remaining() / 4;
|
||||||
|
TIntBufferOverByteBuffer result = new TIntBufferOverByteBuffer(start + position, sz, this, 0, sz,
|
||||||
|
isReadOnly());
|
||||||
|
result.byteOrder = order;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ package org.teavm.classlib.java.nio;
|
||||||
*/
|
*/
|
||||||
class TCharBufferOverByteBuffer extends TCharBufferImpl {
|
class TCharBufferOverByteBuffer extends TCharBufferImpl {
|
||||||
private TByteBufferImpl byteByffer;
|
private TByteBufferImpl byteByffer;
|
||||||
private TByteOrder byteOrder = TByteOrder.BIG_ENDIAN;
|
TByteOrder byteOrder = TByteOrder.BIG_ENDIAN;
|
||||||
boolean readOnly;
|
boolean readOnly;
|
||||||
private int start;
|
private int start;
|
||||||
|
|
||||||
|
|
|
@ -20,13 +20,8 @@ package org.teavm.classlib.java.nio;
|
||||||
* @author Alexey Andreev <konsoletyper@gmail.com>
|
* @author Alexey Andreev <konsoletyper@gmail.com>
|
||||||
*/
|
*/
|
||||||
public abstract class TIntBuffer extends TBuffer implements Comparable<TIntBuffer> {
|
public abstract class TIntBuffer extends TBuffer implements Comparable<TIntBuffer> {
|
||||||
int start;
|
TIntBuffer(int capacity, int position, int limit) {
|
||||||
int[] array;
|
|
||||||
|
|
||||||
TIntBuffer(int start, int capacity, int[] array, int position, int limit) {
|
|
||||||
super(capacity);
|
super(capacity);
|
||||||
this.start = start;
|
|
||||||
this.array = array;
|
|
||||||
this.position = position;
|
this.position = position;
|
||||||
this.limit = limit;
|
this.limit = limit;
|
||||||
}
|
}
|
||||||
|
@ -35,11 +30,11 @@ public abstract class TIntBuffer extends TBuffer implements Comparable<TIntBuffe
|
||||||
if (capacity < 0) {
|
if (capacity < 0) {
|
||||||
throw new IllegalArgumentException("Capacity is negative: " + capacity);
|
throw new IllegalArgumentException("Capacity is negative: " + capacity);
|
||||||
}
|
}
|
||||||
return new TIntBufferImpl(capacity);
|
return new TIntBufferOverArray(capacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TIntBuffer wrap(int[] array, int offset, int length) {
|
public static TIntBuffer wrap(int[] array, int offset, int length) {
|
||||||
return new TIntBufferImpl(0, array.length, array, offset, offset + length, false);
|
return new TIntBufferOverArray(0, array.length, array, offset, offset + length, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TIntBuffer wrap(int[] array) {
|
public static TIntBuffer wrap(int[] array) {
|
||||||
|
@ -60,6 +55,10 @@ public abstract class TIntBuffer extends TBuffer implements Comparable<TIntBuffe
|
||||||
|
|
||||||
public abstract TIntBuffer put(int index, int b);
|
public abstract TIntBuffer put(int index, int b);
|
||||||
|
|
||||||
|
abstract int getElement(int index);
|
||||||
|
|
||||||
|
abstract void putElement(int index, int value);
|
||||||
|
|
||||||
public TIntBuffer get(int[] dst, int offset, int length) {
|
public TIntBuffer get(int[] dst, int offset, int length) {
|
||||||
if (offset < 0 || offset >= dst.length) {
|
if (offset < 0 || offset >= dst.length) {
|
||||||
throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + dst.length + ")");
|
throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + dst.length + ")");
|
||||||
|
@ -74,9 +73,9 @@ public abstract class TIntBuffer extends TBuffer implements Comparable<TIntBuffe
|
||||||
if (length < 0) {
|
if (length < 0) {
|
||||||
throw new IndexOutOfBoundsException("Length " + length + " must be non-negative");
|
throw new IndexOutOfBoundsException("Length " + length + " must be non-negative");
|
||||||
}
|
}
|
||||||
int pos = position + start;
|
int pos = position;
|
||||||
for (int i = 0; i < length; ++i) {
|
for (int i = 0; i < length; ++i) {
|
||||||
dst[offset++] = array[pos++];
|
dst[offset++] = getElement(pos++);
|
||||||
}
|
}
|
||||||
position += length;
|
position += length;
|
||||||
return this;
|
return this;
|
||||||
|
@ -87,7 +86,20 @@ public abstract class TIntBuffer extends TBuffer implements Comparable<TIntBuffe
|
||||||
}
|
}
|
||||||
|
|
||||||
public TIntBuffer put(TIntBuffer src) {
|
public TIntBuffer put(TIntBuffer src) {
|
||||||
return put(src.array, src.start + src.position, src.remaining());
|
if (isReadOnly()) {
|
||||||
|
throw new TReadOnlyBufferException();
|
||||||
|
}
|
||||||
|
if (remaining() < src.remaining()) {
|
||||||
|
throw new TBufferOverflowException();
|
||||||
|
}
|
||||||
|
int length = src.remaining();
|
||||||
|
int pos = position;
|
||||||
|
int offset = src.position;
|
||||||
|
for (int i = 0; i < length; ++i) {
|
||||||
|
putElement(pos++, src.getElement(offset++));
|
||||||
|
}
|
||||||
|
position += length;
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TIntBuffer put(int[] src, int offset, int length) {
|
public TIntBuffer put(int[] src, int offset, int length) {
|
||||||
|
@ -107,9 +119,9 @@ public abstract class TIntBuffer extends TBuffer implements Comparable<TIntBuffe
|
||||||
if (length < 0) {
|
if (length < 0) {
|
||||||
throw new IndexOutOfBoundsException("Length " + length + " must be non-negative");
|
throw new IndexOutOfBoundsException("Length " + length + " must be non-negative");
|
||||||
}
|
}
|
||||||
int pos = position + start;
|
int pos = position;
|
||||||
for (int i = 0; i < length; ++i) {
|
for (int i = 0; i < length; ++i) {
|
||||||
array[pos++] = src[offset++];
|
putElement(pos++, src[offset++]);
|
||||||
}
|
}
|
||||||
position += length;
|
position += length;
|
||||||
return this;
|
return this;
|
||||||
|
@ -120,20 +132,26 @@ public abstract class TIntBuffer extends TBuffer implements Comparable<TIntBuffe
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasArray() {
|
public final boolean hasArray() {
|
||||||
return true;
|
return isArrayPresent();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final int[] array() {
|
public final int[] array() {
|
||||||
return array;
|
return getArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int arrayOffset() {
|
public final int arrayOffset() {
|
||||||
return start;
|
return getArrayOffset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abstract boolean isArrayPresent();
|
||||||
|
|
||||||
|
abstract int[] getArray();
|
||||||
|
|
||||||
|
abstract int getArrayOffset();
|
||||||
|
|
||||||
public abstract TIntBuffer compact();
|
public abstract TIntBuffer compact();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -148,9 +166,9 @@ public abstract class TIntBuffer extends TBuffer implements Comparable<TIntBuffe
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int hashCode = 0;
|
int hashCode = 0;
|
||||||
int pos = position + start;
|
int pos = position;
|
||||||
for (int i = position; i < limit; ++i) {
|
for (int i = position; i < limit; ++i) {
|
||||||
hashCode = 31 * hashCode + array[pos++];
|
hashCode = 31 * hashCode + getElement(pos++);
|
||||||
}
|
}
|
||||||
return hashCode;
|
return hashCode;
|
||||||
}
|
}
|
||||||
|
@ -168,10 +186,10 @@ public abstract class TIntBuffer extends TBuffer implements Comparable<TIntBuffe
|
||||||
if (sz != other.remaining()) {
|
if (sz != other.remaining()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
int a = position + start;
|
int a = position;
|
||||||
int b = other.position + other.start;
|
int b = other.position;
|
||||||
for (int i = 0; i < sz; ++i) {
|
for (int i = 0; i < sz; ++i) {
|
||||||
if (array[a++] != other.array[b++]) {
|
if (getElement(a++) != other.getElement(b++)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -184,14 +202,16 @@ public abstract class TIntBuffer extends TBuffer implements Comparable<TIntBuffe
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int sz = Math.min(remaining(), other.remaining());
|
int sz = Math.min(remaining(), other.remaining());
|
||||||
int a = position + start;
|
int a = position;
|
||||||
int b = other.position + other.start;
|
int b = other.position;
|
||||||
for (int i = 0; i < sz; ++i) {
|
for (int i = 0; i < sz; ++i) {
|
||||||
int r = Integer.compare(array[a++], other.array[b++]);
|
int r = Integer.compare(getElement(a++), other.getElement(b++));
|
||||||
if (r != 0) {
|
if (r != 0) {
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Integer.compare(remaining(), other.remaining());
|
return Integer.compare(remaining(), other.remaining());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract TByteOrder order();
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,50 +19,45 @@ package org.teavm.classlib.java.nio;
|
||||||
*
|
*
|
||||||
* @author Alexey Andreev <konsoletyper@gmail.com>
|
* @author Alexey Andreev <konsoletyper@gmail.com>
|
||||||
*/
|
*/
|
||||||
class TIntBufferImpl extends TIntBuffer {
|
abstract class TIntBufferImpl extends TIntBuffer {
|
||||||
private boolean readOnly;
|
public TIntBufferImpl(int capacity, int position, int limit) {
|
||||||
|
super(capacity, position, limit);
|
||||||
public TIntBufferImpl(int capacity) {
|
|
||||||
this(0, capacity, new int[capacity], 0, capacity, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public TIntBufferImpl(int start, int capacity, int[] array, int position, int limit, boolean readOnly) {
|
|
||||||
super(start, capacity, array, position, limit);
|
|
||||||
this.readOnly = readOnly;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TIntBuffer slice() {
|
public TIntBuffer slice() {
|
||||||
return new TIntBufferImpl(position, limit - position, array, 0, limit - position, readOnly);
|
return duplicate(position, limit - position, 0, limit - position, isReadOnly());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TIntBuffer duplicate() {
|
public TIntBuffer duplicate() {
|
||||||
return new TIntBufferImpl(start, capacity, array, position, limit, readOnly);
|
return duplicate(0, capacity, position, limit, isReadOnly());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TIntBuffer asReadOnlyBuffer() {
|
public TIntBuffer asReadOnlyBuffer() {
|
||||||
return new TIntBufferImpl(start, capacity, array, position, limit, true);
|
return duplicate(0, capacity, position, limit, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abstract TIntBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int get() {
|
public int get() {
|
||||||
if (position >= limit) {
|
if (position >= limit) {
|
||||||
throw new TBufferUnderflowException();
|
throw new TBufferUnderflowException();
|
||||||
}
|
}
|
||||||
return array[start + position++];
|
return getElement(position++);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TIntBuffer put(int b) {
|
public TIntBuffer put(int b) {
|
||||||
if (readOnly) {
|
if (isReadOnly()) {
|
||||||
throw new TReadOnlyBufferException();
|
throw new TReadOnlyBufferException();
|
||||||
}
|
}
|
||||||
if (position >= limit) {
|
if (position >= limit) {
|
||||||
throw new TBufferOverflowException();
|
throw new TBufferOverflowException();
|
||||||
}
|
}
|
||||||
array[start + position++] = b;
|
putElement(position++, b);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,32 +66,31 @@ class TIntBufferImpl extends TIntBuffer {
|
||||||
if (index < 0 || index >= limit) {
|
if (index < 0 || index >= limit) {
|
||||||
throw new IndexOutOfBoundsException("Index " + index + " is outside of range [0;" + limit + ")");
|
throw new IndexOutOfBoundsException("Index " + index + " is outside of range [0;" + limit + ")");
|
||||||
}
|
}
|
||||||
return array[start + index];
|
return getElement(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TIntBuffer put(int index, int b) {
|
public TIntBuffer put(int index, int b) {
|
||||||
if (readOnly) {
|
if (isReadOnly()) {
|
||||||
throw new TReadOnlyBufferException();
|
throw new TReadOnlyBufferException();
|
||||||
}
|
}
|
||||||
if (index < 0 || index >= limit) {
|
if (index < 0 || index >= limit) {
|
||||||
throw new IndexOutOfBoundsException("Index " + index + " is outside of range [0;" + limit + ")");
|
throw new IndexOutOfBoundsException("Index " + index + " is outside of range [0;" + limit + ")");
|
||||||
}
|
}
|
||||||
array[start + index] = b;
|
putElement(index, b);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TIntBuffer compact() {
|
public TIntBuffer compact() {
|
||||||
if (readOnly) {
|
if (isReadOnly()) {
|
||||||
throw new TReadOnlyBufferException();
|
throw new TReadOnlyBufferException();
|
||||||
}
|
}
|
||||||
if (position > 0) {
|
if (position > 0) {
|
||||||
int sz = remaining();
|
int sz = remaining();
|
||||||
int dst = start;
|
int src = position;
|
||||||
int src = start + position;
|
|
||||||
for (int i = 0; i < sz; ++i) {
|
for (int i = 0; i < sz; ++i) {
|
||||||
array[dst++] = array[src++];
|
putElement(i, getElement(src++));
|
||||||
}
|
}
|
||||||
position = sz;
|
position = sz;
|
||||||
}
|
}
|
||||||
|
@ -112,6 +106,8 @@ class TIntBufferImpl extends TIntBuffer {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isReadOnly() {
|
public boolean isReadOnly() {
|
||||||
return readOnly;
|
return readOnly();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abstract boolean readOnly();
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,77 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2014 Alexey Andreev.
|
||||||
|
*
|
||||||
|
* 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 org.teavm.classlib.java.nio;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Alexey Andreev <konsoletyper@gmail.com>
|
||||||
|
*/
|
||||||
|
class TIntBufferOverArray extends TIntBufferImpl {
|
||||||
|
boolean readOnly;
|
||||||
|
int start;
|
||||||
|
int[] array;
|
||||||
|
|
||||||
|
public TIntBufferOverArray(int capacity) {
|
||||||
|
this(0, capacity, new int[capacity], 0, capacity, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TIntBufferOverArray(int start, int capacity, int[] array, int position, int limit, boolean readOnly) {
|
||||||
|
super(capacity, position, limit);
|
||||||
|
this.start = start;
|
||||||
|
this.readOnly = readOnly;
|
||||||
|
this.array = array;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
TIntBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) {
|
||||||
|
return new TIntBufferOverArray(this.start + start, capacity, array, position, limit, readOnly);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
int getElement(int index) {
|
||||||
|
return array[index + start];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void putElement(int index, int value) {
|
||||||
|
array[index + start] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
boolean isArrayPresent() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
int[] getArray() {
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
int getArrayOffset() {
|
||||||
|
return start;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
boolean readOnly() {
|
||||||
|
return readOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TByteOrder order() {
|
||||||
|
return TByteOrder.BIG_ENDIAN;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,100 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2014 Alexey Andreev.
|
||||||
|
*
|
||||||
|
* 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 org.teavm.classlib.java.nio;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Alexey Andreev <konsoletyper@gmail.com>
|
||||||
|
*/
|
||||||
|
public class TIntBufferOverByteBuffer extends TIntBufferImpl {
|
||||||
|
private TByteBufferImpl byteByffer;
|
||||||
|
TByteOrder byteOrder = TByteOrder.BIG_ENDIAN;
|
||||||
|
boolean readOnly;
|
||||||
|
private int start;
|
||||||
|
|
||||||
|
public TIntBufferOverByteBuffer(int start, int capacity, TByteBufferImpl byteBuffer, int position, int limit,
|
||||||
|
boolean readOnly) {
|
||||||
|
super(capacity, position, limit);
|
||||||
|
this.start = start;
|
||||||
|
this.byteByffer = byteBuffer;
|
||||||
|
this.readOnly = readOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
TIntBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) {
|
||||||
|
TIntBufferOverByteBuffer result = new TIntBufferOverByteBuffer(this.start + start * 2, capacity,
|
||||||
|
byteByffer, position, limit, readOnly);
|
||||||
|
result.byteOrder = byteOrder;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
int getElement(int index) {
|
||||||
|
int value;
|
||||||
|
if (byteOrder == TByteOrder.BIG_ENDIAN) {
|
||||||
|
value = (byteByffer.array[start + index * 4] << 24) |
|
||||||
|
(byteByffer.array[start + index * 4 + 1] << 16) |
|
||||||
|
(byteByffer.array[start + index * 4 + 2] << 8) |
|
||||||
|
(byteByffer.array[start + index * 4 + 3]);
|
||||||
|
} else {
|
||||||
|
value = (byteByffer.array[start + index * 4]) |
|
||||||
|
(byteByffer.array[start + index * 4 + 1] << 8) |
|
||||||
|
(byteByffer.array[start + index * 4 + 2] << 16) |
|
||||||
|
(byteByffer.array[start + index * 4 + 3] << 24);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void putElement(int index, int value) {
|
||||||
|
if (byteOrder == TByteOrder.BIG_ENDIAN) {
|
||||||
|
byteByffer.array[start + index * 4] = (byte)(value >> 24);
|
||||||
|
byteByffer.array[start + index * 4 + 1] = (byte)(value >> 16);
|
||||||
|
byteByffer.array[start + index * 4 + 2] = (byte)(value >> 8);
|
||||||
|
byteByffer.array[start + index * 4 + 3] = (byte)value;
|
||||||
|
} else {
|
||||||
|
byteByffer.array[start + index * 4] = (byte)value;
|
||||||
|
byteByffer.array[start + index * 4 + 1] = (byte)(value >> 8);
|
||||||
|
byteByffer.array[start + index * 4 + 2] = (byte)(value >> 16);
|
||||||
|
byteByffer.array[start + index * 4 + 3] = (byte)(value >> 24);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
boolean isArrayPresent() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
int[] getArray() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
int getArrayOffset() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
boolean readOnly() {
|
||||||
|
return readOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TByteOrder order() {
|
||||||
|
return byteOrder;
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,13 +20,8 @@ package org.teavm.classlib.java.nio;
|
||||||
* @author Alexey Andreev <konsoletyper@gmail.com>
|
* @author Alexey Andreev <konsoletyper@gmail.com>
|
||||||
*/
|
*/
|
||||||
public abstract class TShortBuffer extends TBuffer implements Comparable<TShortBuffer> {
|
public abstract class TShortBuffer extends TBuffer implements Comparable<TShortBuffer> {
|
||||||
int start;
|
TShortBuffer(int capacity, int position, int limit) {
|
||||||
short[] array;
|
|
||||||
|
|
||||||
TShortBuffer(int start, int capacity, short[] array, int position, int limit) {
|
|
||||||
super(capacity);
|
super(capacity);
|
||||||
this.start = start;
|
|
||||||
this.array = array;
|
|
||||||
this.position = position;
|
this.position = position;
|
||||||
this.limit = limit;
|
this.limit = limit;
|
||||||
}
|
}
|
||||||
|
@ -35,11 +30,11 @@ public abstract class TShortBuffer extends TBuffer implements Comparable<TShortB
|
||||||
if (capacity < 0) {
|
if (capacity < 0) {
|
||||||
throw new IllegalArgumentException("Capacity is negative: " + capacity);
|
throw new IllegalArgumentException("Capacity is negative: " + capacity);
|
||||||
}
|
}
|
||||||
return new TShortBufferImpl(capacity);
|
return new TShortBufferOverArray(capacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TShortBuffer wrap(short[] array, int offset, int length) {
|
public static TShortBuffer wrap(short[] array, int offset, int length) {
|
||||||
return new TShortBufferImpl(0, array.length, array, offset, offset + length, false);
|
return new TShortBufferOverArray(0, array.length, array, offset, offset + length, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TShortBuffer wrap(short[] array) {
|
public static TShortBuffer wrap(short[] array) {
|
||||||
|
@ -60,6 +55,10 @@ public abstract class TShortBuffer extends TBuffer implements Comparable<TShortB
|
||||||
|
|
||||||
public abstract TShortBuffer put(int index, short b);
|
public abstract TShortBuffer put(int index, short b);
|
||||||
|
|
||||||
|
abstract short getElement(int index);
|
||||||
|
|
||||||
|
abstract void putElement(int index, short value);
|
||||||
|
|
||||||
public TShortBuffer get(short[] dst, int offset, int length) {
|
public TShortBuffer get(short[] dst, int offset, int length) {
|
||||||
if (offset < 0 || offset >= dst.length) {
|
if (offset < 0 || offset >= dst.length) {
|
||||||
throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + dst.length + ")");
|
throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + dst.length + ")");
|
||||||
|
@ -74,9 +73,9 @@ public abstract class TShortBuffer extends TBuffer implements Comparable<TShortB
|
||||||
if (length < 0) {
|
if (length < 0) {
|
||||||
throw new IndexOutOfBoundsException("Length " + length + " must be non-negative");
|
throw new IndexOutOfBoundsException("Length " + length + " must be non-negative");
|
||||||
}
|
}
|
||||||
int pos = position + start;
|
int pos = position;
|
||||||
for (int i = 0; i < length; ++i) {
|
for (int i = 0; i < length; ++i) {
|
||||||
dst[offset++] = array[pos++];
|
dst[offset++] = getElement(pos++);
|
||||||
}
|
}
|
||||||
position += length;
|
position += length;
|
||||||
return this;
|
return this;
|
||||||
|
@ -87,7 +86,20 @@ public abstract class TShortBuffer extends TBuffer implements Comparable<TShortB
|
||||||
}
|
}
|
||||||
|
|
||||||
public TShortBuffer put(TShortBuffer src) {
|
public TShortBuffer put(TShortBuffer src) {
|
||||||
return put(src.array, src.start + src.position, src.remaining());
|
if (isReadOnly()) {
|
||||||
|
throw new TReadOnlyBufferException();
|
||||||
|
}
|
||||||
|
if (remaining() < src.remaining()) {
|
||||||
|
throw new TBufferOverflowException();
|
||||||
|
}
|
||||||
|
int length = src.remaining();
|
||||||
|
int pos = position;
|
||||||
|
int offset = src.position;
|
||||||
|
for (int i = 0; i < length; ++i) {
|
||||||
|
putElement(pos++, src.getElement(offset++));
|
||||||
|
}
|
||||||
|
position += length;
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TShortBuffer put(short[] src, int offset, int length) {
|
public TShortBuffer put(short[] src, int offset, int length) {
|
||||||
|
@ -107,9 +119,9 @@ public abstract class TShortBuffer extends TBuffer implements Comparable<TShortB
|
||||||
if (length < 0) {
|
if (length < 0) {
|
||||||
throw new IndexOutOfBoundsException("Length " + length + " must be non-negative");
|
throw new IndexOutOfBoundsException("Length " + length + " must be non-negative");
|
||||||
}
|
}
|
||||||
int pos = position + start;
|
int pos = position;
|
||||||
for (int i = 0; i < length; ++i) {
|
for (int i = 0; i < length; ++i) {
|
||||||
array[pos++] = src[offset++];
|
putElement(pos++, src[offset++]);
|
||||||
}
|
}
|
||||||
position += length;
|
position += length;
|
||||||
return this;
|
return this;
|
||||||
|
@ -120,20 +132,26 @@ public abstract class TShortBuffer extends TBuffer implements Comparable<TShortB
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasArray() {
|
public final boolean hasArray() {
|
||||||
return true;
|
return isArrayPresent();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final short[] array() {
|
public final short[] array() {
|
||||||
return array;
|
return getArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int arrayOffset() {
|
public final int arrayOffset() {
|
||||||
return start;
|
return getArrayOffset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abstract boolean isArrayPresent();
|
||||||
|
|
||||||
|
abstract short[] getArray();
|
||||||
|
|
||||||
|
abstract int getArrayOffset();
|
||||||
|
|
||||||
public abstract TShortBuffer compact();
|
public abstract TShortBuffer compact();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -148,9 +166,9 @@ public abstract class TShortBuffer extends TBuffer implements Comparable<TShortB
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int hashCode = 0;
|
int hashCode = 0;
|
||||||
int pos = position + start;
|
int pos = position;
|
||||||
for (int i = position; i < limit; ++i) {
|
for (int i = position; i < limit; ++i) {
|
||||||
hashCode = 31 * hashCode + array[pos++];
|
hashCode = 31 * hashCode + getElement(pos++);
|
||||||
}
|
}
|
||||||
return hashCode;
|
return hashCode;
|
||||||
}
|
}
|
||||||
|
@ -168,10 +186,10 @@ public abstract class TShortBuffer extends TBuffer implements Comparable<TShortB
|
||||||
if (sz != other.remaining()) {
|
if (sz != other.remaining()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
int a = position + start;
|
int a = position;
|
||||||
int b = other.position + other.start;
|
int b = other.position;
|
||||||
for (int i = 0; i < sz; ++i) {
|
for (int i = 0; i < sz; ++i) {
|
||||||
if (array[a++] != other.array[b++]) {
|
if (getElement(a++) != other.getElement(b++)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -184,14 +202,16 @@ public abstract class TShortBuffer extends TBuffer implements Comparable<TShortB
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int sz = Math.min(remaining(), other.remaining());
|
int sz = Math.min(remaining(), other.remaining());
|
||||||
int a = position + start;
|
int a = position;
|
||||||
int b = other.position + other.start;
|
int b = other.position;
|
||||||
for (int i = 0; i < sz; ++i) {
|
for (int i = 0; i < sz; ++i) {
|
||||||
int r = Short.compare(array[a++], other.array[b++]);
|
int r = Short.compare(getElement(a++), other.getElement(b++));
|
||||||
if (r != 0) {
|
if (r != 0) {
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Integer.compare(remaining(), other.remaining());
|
return Integer.compare(remaining(), other.remaining());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract TByteOrder order();
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,50 +19,45 @@ package org.teavm.classlib.java.nio;
|
||||||
*
|
*
|
||||||
* @author Alexey Andreev <konsoletyper@gmail.com>
|
* @author Alexey Andreev <konsoletyper@gmail.com>
|
||||||
*/
|
*/
|
||||||
class TShortBufferImpl extends TShortBuffer {
|
abstract class TShortBufferImpl extends TShortBuffer {
|
||||||
private boolean readOnly;
|
public TShortBufferImpl(int capacity, int position, int limit) {
|
||||||
|
super(capacity, position, limit);
|
||||||
public TShortBufferImpl(int capacity) {
|
|
||||||
this(0, capacity, new short[capacity], 0, capacity, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public TShortBufferImpl(int start, int capacity, short[] array, int position, int limit, boolean readOnly) {
|
|
||||||
super(start, capacity, array, position, limit);
|
|
||||||
this.readOnly = readOnly;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TShortBuffer slice() {
|
public TShortBuffer slice() {
|
||||||
return new TShortBufferImpl(position, limit - position, array, 0, limit - position, readOnly);
|
return duplicate(position, limit - position, 0, limit - position, isReadOnly());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TShortBuffer duplicate() {
|
public TShortBuffer duplicate() {
|
||||||
return new TShortBufferImpl(start, capacity, array, position, limit, readOnly);
|
return duplicate(0, capacity, position, limit, isReadOnly());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TShortBuffer asReadOnlyBuffer() {
|
public TShortBuffer asReadOnlyBuffer() {
|
||||||
return new TShortBufferImpl(start, capacity, array, position, limit, true);
|
return duplicate(0, capacity, position, limit, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abstract TShortBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public short get() {
|
public short get() {
|
||||||
if (position >= limit) {
|
if (position >= limit) {
|
||||||
throw new TBufferUnderflowException();
|
throw new TBufferUnderflowException();
|
||||||
}
|
}
|
||||||
return array[start + position++];
|
return getElement(position++);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TShortBuffer put(short b) {
|
public TShortBuffer put(short b) {
|
||||||
if (readOnly) {
|
if (isReadOnly()) {
|
||||||
throw new TReadOnlyBufferException();
|
throw new TReadOnlyBufferException();
|
||||||
}
|
}
|
||||||
if (position >= limit) {
|
if (position >= limit) {
|
||||||
throw new TBufferOverflowException();
|
throw new TBufferOverflowException();
|
||||||
}
|
}
|
||||||
array[start + position++] = b;
|
putElement(position++, b);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,32 +66,31 @@ class TShortBufferImpl extends TShortBuffer {
|
||||||
if (index < 0 || index >= limit) {
|
if (index < 0 || index >= limit) {
|
||||||
throw new IndexOutOfBoundsException("Index " + index + " is outside of range [0;" + limit + ")");
|
throw new IndexOutOfBoundsException("Index " + index + " is outside of range [0;" + limit + ")");
|
||||||
}
|
}
|
||||||
return array[start + index];
|
return getElement(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TShortBuffer put(int index, short b) {
|
public TShortBuffer put(int index, short b) {
|
||||||
if (readOnly) {
|
if (isReadOnly()) {
|
||||||
throw new TReadOnlyBufferException();
|
throw new TReadOnlyBufferException();
|
||||||
}
|
}
|
||||||
if (index < 0 || index >= limit) {
|
if (index < 0 || index >= limit) {
|
||||||
throw new IndexOutOfBoundsException("Index " + index + " is outside of range [0;" + limit + ")");
|
throw new IndexOutOfBoundsException("Index " + index + " is outside of range [0;" + limit + ")");
|
||||||
}
|
}
|
||||||
array[start + index] = b;
|
putElement(index, b);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TShortBuffer compact() {
|
public TShortBuffer compact() {
|
||||||
if (readOnly) {
|
if (isReadOnly()) {
|
||||||
throw new TReadOnlyBufferException();
|
throw new TReadOnlyBufferException();
|
||||||
}
|
}
|
||||||
if (position > 0) {
|
if (position > 0) {
|
||||||
int sz = remaining();
|
int sz = remaining();
|
||||||
int dst = start;
|
int src = position;
|
||||||
int src = start + position;
|
|
||||||
for (int i = 0; i < sz; ++i) {
|
for (int i = 0; i < sz; ++i) {
|
||||||
array[dst++] = array[src++];
|
putElement(i, getElement(src++));
|
||||||
}
|
}
|
||||||
position = sz;
|
position = sz;
|
||||||
}
|
}
|
||||||
|
@ -112,6 +106,8 @@ class TShortBufferImpl extends TShortBuffer {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isReadOnly() {
|
public boolean isReadOnly() {
|
||||||
return readOnly;
|
return readOnly();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abstract boolean readOnly();
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,77 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2014 Alexey Andreev.
|
||||||
|
*
|
||||||
|
* 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 org.teavm.classlib.java.nio;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Alexey Andreev <konsoletyper@gmail.com>
|
||||||
|
*/
|
||||||
|
class TShortBufferOverArray extends TShortBufferImpl {
|
||||||
|
boolean readOnly;
|
||||||
|
int start;
|
||||||
|
short[] array;
|
||||||
|
|
||||||
|
public TShortBufferOverArray(int capacity) {
|
||||||
|
this(0, capacity, new short[capacity], 0, capacity, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TShortBufferOverArray(int start, int capacity, short[] array, int position, int limit, boolean readOnly) {
|
||||||
|
super(capacity, position, limit);
|
||||||
|
this.start = start;
|
||||||
|
this.readOnly = readOnly;
|
||||||
|
this.array = array;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
TShortBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) {
|
||||||
|
return new TShortBufferOverArray(this.start + start, capacity, array, position, limit, readOnly);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
short getElement(int index) {
|
||||||
|
return array[index + start];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void putElement(int index, short value) {
|
||||||
|
array[index + start] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
boolean isArrayPresent() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
short[] getArray() {
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
int getArrayOffset() {
|
||||||
|
return start;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
boolean readOnly() {
|
||||||
|
return readOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TByteOrder order() {
|
||||||
|
return TByteOrder.BIG_ENDIAN;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,90 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2014 Alexey Andreev.
|
||||||
|
*
|
||||||
|
* 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 org.teavm.classlib.java.nio;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Alexey Andreev <konsoletyper@gmail.com>
|
||||||
|
*/
|
||||||
|
class TShortBufferOverByteBuffer extends TShortBufferImpl {
|
||||||
|
private TByteBufferImpl byteByffer;
|
||||||
|
TByteOrder byteOrder = TByteOrder.BIG_ENDIAN;
|
||||||
|
boolean readOnly;
|
||||||
|
private int start;
|
||||||
|
|
||||||
|
public TShortBufferOverByteBuffer(int start, int capacity, TByteBufferImpl byteBuffer, int position, int limit,
|
||||||
|
boolean readOnly) {
|
||||||
|
super(capacity, position, limit);
|
||||||
|
this.start = start;
|
||||||
|
this.byteByffer = byteBuffer;
|
||||||
|
this.readOnly = readOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
TShortBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) {
|
||||||
|
TShortBufferOverByteBuffer result = new TShortBufferOverByteBuffer(this.start + start * 2, capacity,
|
||||||
|
byteByffer, position, limit, readOnly);
|
||||||
|
result.byteOrder = byteOrder;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
short getElement(int index) {
|
||||||
|
int value;
|
||||||
|
if (byteOrder == TByteOrder.BIG_ENDIAN) {
|
||||||
|
value = (byteByffer.array[start + index * 2] << 8) | (byteByffer.array[start + index * 2 + 1]);
|
||||||
|
} else {
|
||||||
|
value = (byteByffer.array[start + index * 2 + 1] << 8) | (byteByffer.array[start + index * 2]);
|
||||||
|
}
|
||||||
|
return (short)value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void putElement(int index, short value) {
|
||||||
|
if (byteOrder == TByteOrder.BIG_ENDIAN) {
|
||||||
|
byteByffer.array[start + index * 2] = (byte)(value >> 8);
|
||||||
|
byteByffer.array[start + index * 2 + 1] = (byte)value;
|
||||||
|
} else {
|
||||||
|
byteByffer.array[start + index * 2] = (byte)value;
|
||||||
|
byteByffer.array[start + index * 2 + 1] = (byte)(value >> 8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
boolean isArrayPresent() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
short[] getArray() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
int getArrayOffset() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
boolean readOnly() {
|
||||||
|
return readOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TByteOrder order() {
|
||||||
|
return byteOrder;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,93 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2014 Alexey Andreev.
|
||||||
|
*
|
||||||
|
* 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 org.teavm.classlib.java.nio;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.ByteOrder;
|
||||||
|
import java.nio.IntBuffer;
|
||||||
|
import java.nio.ShortBuffer;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Alexey Andreev <konsoletyper@gmail.com>
|
||||||
|
*/
|
||||||
|
public class ByteBufferWrapperTest {
|
||||||
|
@Test
|
||||||
|
public void wrapsIntoShortBuffer() {
|
||||||
|
byte[] array = new byte[100];
|
||||||
|
ByteBuffer buffer = ByteBuffer.wrap(array);
|
||||||
|
buffer.limit(80);
|
||||||
|
buffer.get(new byte[10]);
|
||||||
|
buffer = buffer.slice();
|
||||||
|
buffer.put(0, (byte)0x23);
|
||||||
|
buffer.put(1, (byte)0x24);
|
||||||
|
ShortBuffer wrapper = buffer.asShortBuffer();
|
||||||
|
assertThat(wrapper.capacity(), is(35));
|
||||||
|
assertThat(wrapper.position(), is(0));
|
||||||
|
assertThat(wrapper.limit(), is(35));
|
||||||
|
assertThat(wrapper.get(0), is((short)0x2324));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void wrapsIntoIntBuffer() {
|
||||||
|
byte[] array = new byte[100];
|
||||||
|
ByteBuffer buffer = ByteBuffer.wrap(array);
|
||||||
|
buffer.limit(70);
|
||||||
|
buffer.get(new byte[10]);
|
||||||
|
buffer = buffer.slice();
|
||||||
|
buffer.put(0, (byte)0x23);
|
||||||
|
buffer.put(1, (byte)0x24);
|
||||||
|
buffer.put(2, (byte)0x25);
|
||||||
|
IntBuffer wrapper = buffer.asIntBuffer();
|
||||||
|
assertThat(wrapper.capacity(), is(15));
|
||||||
|
assertThat(wrapper.position(), is(0));
|
||||||
|
assertThat(wrapper.limit(), is(15));
|
||||||
|
assertThat(wrapper.get(0), is(0x23242500));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void endiannesWorks() {
|
||||||
|
byte[] array = new byte[100];
|
||||||
|
ByteBuffer buffer = ByteBuffer.wrap(array);
|
||||||
|
buffer.order(ByteOrder.LITTLE_ENDIAN);
|
||||||
|
buffer.put(0, (byte)0x23);
|
||||||
|
buffer.put(1, (byte)0x24);
|
||||||
|
ShortBuffer wrapper = buffer.asShortBuffer();
|
||||||
|
assertThat(wrapper.get(0), is((short)0x2423));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void changesInWrapperSeenInBuffer() {
|
||||||
|
byte[] array = new byte[100];
|
||||||
|
ByteBuffer buffer = ByteBuffer.wrap(array);
|
||||||
|
ShortBuffer wrapper = buffer.asShortBuffer();
|
||||||
|
wrapper.put(0, (short)0x2324);
|
||||||
|
assertThat(buffer.get(0), is((byte)0x23));
|
||||||
|
assertThat(buffer.get(1), is((byte)0x24));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void changesInBufferSeenInWrapper() {
|
||||||
|
byte[] array = new byte[100];
|
||||||
|
ByteBuffer buffer = ByteBuffer.wrap(array);
|
||||||
|
ShortBuffer wrapper = buffer.asShortBuffer();
|
||||||
|
buffer.put(1, (byte)0x24);
|
||||||
|
assertThat(wrapper.get(0), is((short)0x0024));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user