From 3489f3c82c8ad23f85321265c489b12afdf2cd83 Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Tue, 4 Nov 2014 20:02:36 +0300 Subject: [PATCH] Speed-up buffers that wrap ByteBuffer --- .../classlib/java/nio/TByteBufferImpl.java | 45 ++++++------ .../java/nio/TCharBufferOverByteBuffer.java | 44 +----------- .../TCharBufferOverByteBufferBigEndian.java | 51 ++++++++++++++ ...TCharBufferOverByteBufferLittleEndian.java | 51 ++++++++++++++ .../java/nio/TFloatBufferOverByteBuffer.java | 53 +-------------- .../TFloatBufferOverByteBufferBigEndian.java | 56 +++++++++++++++ ...FloatBufferOverByteBufferLittleEndian.java | 56 +++++++++++++++ .../java/nio/TIntBufferOverByteBuffer.java | 52 +------------- .../TIntBufferOverByteBufferBigEndian.java | 54 +++++++++++++++ .../TIntBufferOverByteBufferLittleEndian.java | 54 +++++++++++++++ .../java/nio/TLongBufferOverByteBuffer.java | 68 +------------------ .../TLongBufferOverByteBufferBigEndian.java | 62 +++++++++++++++++ ...TLongBufferOverByteBufferLittleEndian.java | 62 +++++++++++++++++ .../java/nio/TShortBufferOverByteBuffer.java | 44 +----------- .../TShortBufferOverByteBufferBigEndian.java | 51 ++++++++++++++ ...ShortBufferOverByteBufferLittleEndian.java | 51 ++++++++++++++ 16 files changed, 588 insertions(+), 266 deletions(-) create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TCharBufferOverByteBufferBigEndian.java create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TCharBufferOverByteBufferLittleEndian.java create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TFloatBufferOverByteBufferBigEndian.java create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TFloatBufferOverByteBufferLittleEndian.java create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TIntBufferOverByteBufferBigEndian.java create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TIntBufferOverByteBufferLittleEndian.java create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TLongBufferOverByteBufferBigEndian.java create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TLongBufferOverByteBufferLittleEndian.java create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TShortBufferOverByteBufferBigEndian.java create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TShortBufferOverByteBufferLittleEndian.java diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TByteBufferImpl.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TByteBufferImpl.java index 7fd548170..50691afca 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TByteBufferImpl.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TByteBufferImpl.java @@ -171,46 +171,51 @@ class TByteBufferImpl extends TByteBuffer { @Override public TCharBuffer asCharBuffer() { int sz = remaining() / 2; - TCharBufferOverByteBuffer result = new TCharBufferOverByteBuffer(start + position, sz, this, 0, sz, - isReadOnly()); - result.byteOrder = order; - return result; + if (order == TByteOrder.BIG_ENDIAN) { + return new TCharBufferOverByteBufferBigEndian(start + position, sz, this, 0, sz, isReadOnly()); + } else { + return new TCharBufferOverByteBufferLittleEndian(start + position, sz, this, 0, sz, isReadOnly()); + } } @Override public TShortBuffer asShortBuffer() { int sz = remaining() / 2; - TShortBufferOverByteBuffer result = new TShortBufferOverByteBuffer(start + position, sz, this, 0, sz, - isReadOnly()); - result.byteOrder = order; - return result; + if (order == TByteOrder.BIG_ENDIAN) { + return new TShortBufferOverByteBufferBigEndian(start + position, sz, this, 0, sz, isReadOnly()); + } else { + return new TShortBufferOverByteBufferLittleEndian(start + position, sz, this, 0, sz, isReadOnly()); + } } @Override public TIntBuffer asIntBuffer() { int sz = remaining() / 4; - TIntBufferOverByteBuffer result = new TIntBufferOverByteBuffer(start + position, sz, this, 0, sz, - isReadOnly()); - result.byteOrder = order; - return result; + if (order == TByteOrder.BIG_ENDIAN) { + return new TIntBufferOverByteBufferBigEndian(start + position, sz, this, 0, sz, isReadOnly()); + } else { + return new TIntBufferOverByteBufferLittleEndian(start + position, sz, this, 0, sz, isReadOnly()); + } } @Override public TLongBuffer asLongBuffer() { int sz = remaining() / 8; - TLongBufferOverByteBuffer result = new TLongBufferOverByteBuffer(start + position, sz, this, 0, sz, - isReadOnly()); - result.byteOrder = order; - return result; + if (order == TByteOrder.BIG_ENDIAN) { + return new TLongBufferOverByteBufferBigEndian(start + position, sz, this, 0, sz, isReadOnly()); + } else { + return new TLongBufferOverByteBufferLittleEndian(start + position, sz, this, 0, sz, isReadOnly()); + } } @Override public TFloatBuffer asFloatBuffer() { int sz = remaining() / 4; - TFloatBufferOverByteBuffer result = new TFloatBufferOverByteBuffer(start + position, sz, this, 0, sz, - isReadOnly()); - result.byteOrder = order; - return result; + if (order == TByteOrder.LITTLE_ENDIAN) { + return new TFloatBufferOverByteBufferBigEndian(start + position, sz, this, 0, sz, isReadOnly()); + } else { + return new TFloatBufferOverByteBufferLittleEndian(start + position, sz, this, 0, sz, isReadOnly()); + } } @Override diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TCharBufferOverByteBuffer.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TCharBufferOverByteBuffer.java index 695c44980..aa48b251f 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TCharBufferOverByteBuffer.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TCharBufferOverByteBuffer.java @@ -19,11 +19,10 @@ package org.teavm.classlib.java.nio; * * @author Alexey Andreev */ -class TCharBufferOverByteBuffer extends TCharBufferImpl { - private TByteBufferImpl byteByffer; - TByteOrder byteOrder = TByteOrder.BIG_ENDIAN; +abstract class TCharBufferOverByteBuffer extends TCharBufferImpl { + TByteBufferImpl byteByffer; boolean readOnly; - private int start; + int start; public TCharBufferOverByteBuffer(int start, int capacity, TByteBufferImpl byteBuffer, int position, int limit, boolean readOnly) { @@ -33,38 +32,6 @@ class TCharBufferOverByteBuffer extends TCharBufferImpl { this.readOnly = readOnly; } - @Override - TCharBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) { - TCharBufferOverByteBuffer result = new TCharBufferOverByteBuffer(this.start + start * 2, capacity, byteByffer, - position, limit, readOnly); - result.byteOrder = byteOrder; - return result; - } - - @Override - char getChar(int index) { - int value; - if (byteOrder == TByteOrder.BIG_ENDIAN) { - value = ((byteByffer.array[start + index * 2] & 0xFF) << 8) | - (byteByffer.array[start + index * 2 + 1] & 0xFF); - } else { - value = ((byteByffer.array[start + index * 2 + 1] & 0xFF) << 8) | - (byteByffer.array[start + index * 2] & 0xFF); - } - return (char)value; - } - - @Override - void putChar(int index, char 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; @@ -84,9 +51,4 @@ class TCharBufferOverByteBuffer extends TCharBufferImpl { boolean readOnly() { return readOnly; } - - @Override - public TByteOrder order() { - return byteOrder; - } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TCharBufferOverByteBufferBigEndian.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TCharBufferOverByteBufferBigEndian.java new file mode 100644 index 000000000..b0ea539f5 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TCharBufferOverByteBufferBigEndian.java @@ -0,0 +1,51 @@ +/* + * 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 + */ +class TCharBufferOverByteBufferBigEndian extends TCharBufferOverByteBuffer { + public TCharBufferOverByteBufferBigEndian(int start, int capacity, TByteBufferImpl byteBuffer, int position, + int limit, boolean readOnly) { + super(start, capacity, byteBuffer, position, limit, readOnly); + } + + @Override + char getChar(int index) { + int value = ((byteByffer.array[start + index * 2] & 0xFF) << 8) | + (byteByffer.array[start + index * 2 + 1] & 0xFF); + return (char)value; + } + + @Override + void putChar(int index, char value) { + byteByffer.array[start + index * 2] = (byte)(value >> 8); + byteByffer.array[start + index * 2 + 1] = (byte)value; + } + + @Override + TCharBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) { + return new TCharBufferOverByteBufferBigEndian(this.start + start * 2, capacity, byteByffer, + position, limit, readOnly); + } + + @Override + public TByteOrder order() { + return TByteOrder.LITTLE_ENDIAN; + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TCharBufferOverByteBufferLittleEndian.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TCharBufferOverByteBufferLittleEndian.java new file mode 100644 index 000000000..0d18355d9 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TCharBufferOverByteBufferLittleEndian.java @@ -0,0 +1,51 @@ +/* + * 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 + */ +class TCharBufferOverByteBufferLittleEndian extends TCharBufferOverByteBuffer { + public TCharBufferOverByteBufferLittleEndian(int start, int capacity, TByteBufferImpl byteBuffer, int position, + int limit, boolean readOnly) { + super(start, capacity, byteBuffer, position, limit, readOnly); + } + + @Override + char getChar(int index) { + int value = ((byteByffer.array[start + index * 2 + 1] & 0xFF) << 8) | + (byteByffer.array[start + index * 2] & 0xFF); + return (char)value; + } + + @Override + void putChar(int index, char value) { + byteByffer.array[start + index * 2] = (byte)value; + byteByffer.array[start + index * 2 + 1] = (byte)(value >> 8); + } + + @Override + public TByteOrder order() { + return TByteOrder.BIG_ENDIAN; + } + + @Override + TCharBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) { + return new TCharBufferOverByteBufferLittleEndian(this.start + start * 2, capacity, byteByffer, + position, limit, readOnly); + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TFloatBufferOverByteBuffer.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TFloatBufferOverByteBuffer.java index d46635e51..8815fd7e9 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TFloatBufferOverByteBuffer.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TFloatBufferOverByteBuffer.java @@ -19,11 +19,10 @@ package org.teavm.classlib.java.nio; * * @author Alexey Andreev */ -class TFloatBufferOverByteBuffer extends TFloatBufferImpl { - private TByteBufferImpl byteByffer; - TByteOrder byteOrder = TByteOrder.BIG_ENDIAN; +abstract class TFloatBufferOverByteBuffer extends TFloatBufferImpl { + TByteBufferImpl byteByffer; boolean readOnly; - private int start; + int start; public TFloatBufferOverByteBuffer(int start, int capacity, TByteBufferImpl byteBuffer, int position, int limit, boolean readOnly) { @@ -33,47 +32,6 @@ class TFloatBufferOverByteBuffer extends TFloatBufferImpl { this.readOnly = readOnly; } - @Override - TFloatBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) { - TFloatBufferOverByteBuffer result = new TFloatBufferOverByteBuffer(this.start + start * 2, capacity, - byteByffer, position, limit, readOnly); - result.byteOrder = byteOrder; - return result; - } - - @Override - float getElement(int index) { - int value; - if (byteOrder == TByteOrder.BIG_ENDIAN) { - value = ((byteByffer.array[start + index * 4] & 0xFF) << 24) | - ((byteByffer.array[start + index * 4 + 1] & 0xFF) << 16) | - ((byteByffer.array[start + index * 4 + 2] & 0xFF) << 8) | - (byteByffer.array[start + index * 4 + 3] & 0xFF); - } else { - value = (byteByffer.array[start + index * 4] & 0xFF) | - ((byteByffer.array[start + index * 4 + 1] & 0xFF) << 8) | - ((byteByffer.array[start + index * 4 + 2] & 0xFF) << 16) | - ((byteByffer.array[start + index * 4 + 3] & 0xFF) << 24); - } - return Float.intBitsToFloat(value); - } - - @Override - void putElement(int index, float f) { - int value = Float.floatToIntBits(f); - 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; @@ -93,9 +51,4 @@ class TFloatBufferOverByteBuffer extends TFloatBufferImpl { boolean readOnly() { return readOnly; } - - @Override - public TByteOrder order() { - return byteOrder; - } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TFloatBufferOverByteBufferBigEndian.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TFloatBufferOverByteBufferBigEndian.java new file mode 100644 index 000000000..5e66eefd5 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TFloatBufferOverByteBufferBigEndian.java @@ -0,0 +1,56 @@ +/* + * 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 + */ +class TFloatBufferOverByteBufferBigEndian extends TFloatBufferOverByteBuffer { + public TFloatBufferOverByteBufferBigEndian(int start, int capacity, TByteBufferImpl byteBuffer, int position, + int limit, boolean readOnly) { + super(start, capacity, byteBuffer, position, limit, readOnly); + } + + @Override + TFloatBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) { + return new TFloatBufferOverByteBufferBigEndian(this.start + start * 4, capacity, + byteByffer, position, limit, readOnly); + } + + @Override + float getElement(int index) { + int value = (byteByffer.array[start + index * 4] & 0xFF) | + ((byteByffer.array[start + index * 4 + 1] & 0xFF) << 8) | + ((byteByffer.array[start + index * 4 + 2] & 0xFF) << 16) | + ((byteByffer.array[start + index * 4 + 3] & 0xFF) << 24); + return Float.intBitsToFloat(value); + } + + @Override + void putElement(int index, float f) { + int value = Float.floatToIntBits(f); + 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 + public TByteOrder order() { + return TByteOrder.BIG_ENDIAN; + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TFloatBufferOverByteBufferLittleEndian.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TFloatBufferOverByteBufferLittleEndian.java new file mode 100644 index 000000000..cee658002 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TFloatBufferOverByteBufferLittleEndian.java @@ -0,0 +1,56 @@ +/* + * 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 + */ +class TFloatBufferOverByteBufferLittleEndian extends TFloatBufferOverByteBuffer { + public TFloatBufferOverByteBufferLittleEndian(int start, int capacity, TByteBufferImpl byteBuffer, int position, + int limit, boolean readOnly) { + super(start, capacity, byteBuffer, position, limit, readOnly); + } + + @Override + TFloatBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) { + return new TFloatBufferOverByteBufferLittleEndian(this.start + start * 4, capacity, + byteByffer, position, limit, readOnly); + } + + @Override + float getElement(int index) { + int value = ((byteByffer.array[start + index * 4] & 0xFF) << 24) | + ((byteByffer.array[start + index * 4 + 1] & 0xFF) << 16) | + ((byteByffer.array[start + index * 4 + 2] & 0xFF) << 8) | + (byteByffer.array[start + index * 4 + 3] & 0xFF); + return Float.intBitsToFloat(value); + } + + @Override + void putElement(int index, float f) { + int value = Float.floatToIntBits(f); + 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; + } + + @Override + public TByteOrder order() { + return TByteOrder.LITTLE_ENDIAN; + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TIntBufferOverByteBuffer.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TIntBufferOverByteBuffer.java index 733d89068..25e0c787b 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TIntBufferOverByteBuffer.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TIntBufferOverByteBuffer.java @@ -19,11 +19,10 @@ package org.teavm.classlib.java.nio; * * @author Alexey Andreev */ -public class TIntBufferOverByteBuffer extends TIntBufferImpl { - private TByteBufferImpl byteByffer; - TByteOrder byteOrder = TByteOrder.BIG_ENDIAN; +abstract class TIntBufferOverByteBuffer extends TIntBufferImpl { + TByteBufferImpl byteByffer; boolean readOnly; - private int start; + int start; public TIntBufferOverByteBuffer(int start, int capacity, TByteBufferImpl byteBuffer, int position, int limit, boolean readOnly) { @@ -33,46 +32,6 @@ public class TIntBufferOverByteBuffer extends TIntBufferImpl { 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] & 0xFF) << 24) | - ((byteByffer.array[start + index * 4 + 1] & 0xFF) << 16) | - ((byteByffer.array[start + index * 4 + 2] & 0xFF) << 8) | - (byteByffer.array[start + index * 4 + 3] & 0xFF); - } else { - value = (byteByffer.array[start + index * 4] & 0xFF) | - ((byteByffer.array[start + index * 4 + 1] & 0xFF) << 8) | - ((byteByffer.array[start + index * 4 + 2] & 0xFF) << 16) | - ((byteByffer.array[start + index * 4 + 3] & 0xFF) << 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; @@ -92,9 +51,4 @@ public class TIntBufferOverByteBuffer extends TIntBufferImpl { boolean readOnly() { return readOnly; } - - @Override - public TByteOrder order() { - return byteOrder; - } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TIntBufferOverByteBufferBigEndian.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TIntBufferOverByteBufferBigEndian.java new file mode 100644 index 000000000..56943f0e1 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TIntBufferOverByteBufferBigEndian.java @@ -0,0 +1,54 @@ +/* + * 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 + */ +class TIntBufferOverByteBufferBigEndian extends TIntBufferOverByteBuffer { + public TIntBufferOverByteBufferBigEndian(int start, int capacity, TByteBufferImpl byteBuffer, int position, + int limit, boolean readOnly) { + super(start, capacity, byteBuffer, position, limit, readOnly); + } + + @Override + TIntBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) { + return new TIntBufferOverByteBufferBigEndian(this.start + start * 4, capacity, + byteByffer, position, limit, readOnly); + } + + @Override + int getElement(int index) { + return ((byteByffer.array[start + index * 4] & 0xFF) << 24) | + ((byteByffer.array[start + index * 4 + 1] & 0xFF) << 16) | + ((byteByffer.array[start + index * 4 + 2] & 0xFF) << 8) | + (byteByffer.array[start + index * 4 + 3] & 0xFF); + } + + @Override + void putElement(int index, int value) { + 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; + } + + @Override + public TByteOrder order() { + return TByteOrder.BIG_ENDIAN; + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TIntBufferOverByteBufferLittleEndian.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TIntBufferOverByteBufferLittleEndian.java new file mode 100644 index 000000000..618dab9d5 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TIntBufferOverByteBufferLittleEndian.java @@ -0,0 +1,54 @@ +/* + * 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 + */ +class TIntBufferOverByteBufferLittleEndian extends TIntBufferOverByteBuffer { + public TIntBufferOverByteBufferLittleEndian(int start, int capacity, TByteBufferImpl byteBuffer, int position, + int limit, boolean readOnly) { + super(start, capacity, byteBuffer, position, limit, readOnly); + } + + @Override + TIntBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) { + return new TIntBufferOverByteBufferLittleEndian(this.start + start * 4, capacity, + byteByffer, position, limit, readOnly); + } + + @Override + int getElement(int index) { + return (byteByffer.array[start + index * 4] & 0xFF) | + ((byteByffer.array[start + index * 4 + 1] & 0xFF) << 8) | + ((byteByffer.array[start + index * 4 + 2] & 0xFF) << 16) | + ((byteByffer.array[start + index * 4 + 3] & 0xFF) << 24); + } + + @Override + void putElement(int index, int value) { + 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 + public TByteOrder order() { + return TByteOrder.LITTLE_ENDIAN; + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TLongBufferOverByteBuffer.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TLongBufferOverByteBuffer.java index eedc34c66..7fb4d3efb 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TLongBufferOverByteBuffer.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TLongBufferOverByteBuffer.java @@ -19,11 +19,10 @@ package org.teavm.classlib.java.nio; * * @author Alexey Andreev */ -class TLongBufferOverByteBuffer extends TLongBufferImpl { - private TByteBufferImpl byteByffer; - TByteOrder byteOrder = TByteOrder.BIG_ENDIAN; +abstract class TLongBufferOverByteBuffer extends TLongBufferImpl { + TByteBufferImpl byteByffer; boolean readOnly; - private int start; + int start; public TLongBufferOverByteBuffer(int start, int capacity, TByteBufferImpl byteBuffer, int position, int limit, boolean readOnly) { @@ -33,62 +32,6 @@ class TLongBufferOverByteBuffer extends TLongBufferImpl { this.readOnly = readOnly; } - @Override - TLongBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) { - TLongBufferOverByteBuffer result = new TLongBufferOverByteBuffer(this.start + start * 2, capacity, - byteByffer, position, limit, readOnly); - result.byteOrder = byteOrder; - return result; - } - - @Override - long getElement(int index) { - long value; - if (byteOrder == TByteOrder.BIG_ENDIAN) { - value = (((long)byteByffer.array[start + index * 8] & 0xFF) << 56) | - (((long)byteByffer.array[start + index * 8 + 1] & 0xFF) << 48) | - (((long)byteByffer.array[start + index * 8 + 2] & 0xFF) << 40) | - (((long)byteByffer.array[start + index * 8 + 3] & 0xFF) << 32) | - (((long)byteByffer.array[start + index * 8 + 4] & 0xFF) << 24) | - (((long)byteByffer.array[start + index * 8 + 5] & 0xFF) << 16) | - (((long)byteByffer.array[start + index * 8 + 6] & 0xFF) << 8) | - (byteByffer.array[start + index * 8 + 7] & 0xFF); - } else { - value = (byteByffer.array[start + index * 8] & 0xFF) | - (((long)byteByffer.array[start + index * 8 + 1] & 0xFF) << 8) | - (((long)byteByffer.array[start + index * 8 + 2] & 0xFF) << 16) | - (((long)byteByffer.array[start + index * 8 + 3] & 0xFF) << 24) | - (((long)byteByffer.array[start + index * 8 + 4] & 0xFF) << 32) | - (((long)byteByffer.array[start + index * 8 + 5] & 0xFF) << 40) | - (((long)byteByffer.array[start + index * 8 + 6] & 0xFF) << 48) | - (((long)byteByffer.array[start + index * 8 + 7] & 0xFF) << 56); - } - return value; - } - - @Override - void putElement(int index, long value) { - if (byteOrder == TByteOrder.BIG_ENDIAN) { - byteByffer.array[start + index * 8] = (byte)(value >> 56); - byteByffer.array[start + index * 8 + 1] = (byte)(value >> 48); - byteByffer.array[start + index * 8 + 2] = (byte)(value >> 40); - byteByffer.array[start + index * 8 + 3] = (byte)(value >> 32); - byteByffer.array[start + index * 8 + 4] = (byte)(value >> 24); - byteByffer.array[start + index * 8 + 5] = (byte)(value >> 16); - byteByffer.array[start + index * 8 + 6] = (byte)(value >> 8); - byteByffer.array[start + index * 8 + 7] = (byte)value; - } else { - byteByffer.array[start + index * 8] = (byte)value; - byteByffer.array[start + index * 8 + 1] = (byte)(value >> 8); - byteByffer.array[start + index * 8 + 2] = (byte)(value >> 16); - byteByffer.array[start + index * 8 + 3] = (byte)(value >> 24); - byteByffer.array[start + index * 8 + 4] = (byte)(value >> 32); - byteByffer.array[start + index * 8 + 5] = (byte)(value >> 40); - byteByffer.array[start + index * 8 + 6] = (byte)(value >> 48); - byteByffer.array[start + index * 8 + 7] = (byte)(value >> 56); - } - } - @Override boolean isArrayPresent() { return false; @@ -108,9 +51,4 @@ class TLongBufferOverByteBuffer extends TLongBufferImpl { boolean readOnly() { return readOnly; } - - @Override - public TByteOrder order() { - return byteOrder; - } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TLongBufferOverByteBufferBigEndian.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TLongBufferOverByteBufferBigEndian.java new file mode 100644 index 000000000..1a9df0517 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TLongBufferOverByteBufferBigEndian.java @@ -0,0 +1,62 @@ +/* + * 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 + */ +class TLongBufferOverByteBufferBigEndian extends TLongBufferOverByteBuffer { + public TLongBufferOverByteBufferBigEndian(int start, int capacity, TByteBufferImpl byteBuffer, int position, + int limit, boolean readOnly) { + super(start, capacity, byteBuffer, position, limit, readOnly); + } + + @Override + TLongBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) { + return new TLongBufferOverByteBufferBigEndian(this.start + start * 8, capacity, + byteByffer, position, limit, readOnly); + } + + @Override + long getElement(int index) { + return (((long)byteByffer.array[start + index * 8] & 0xFF) << 56) | + (((long)byteByffer.array[start + index * 8 + 1] & 0xFF) << 48) | + (((long)byteByffer.array[start + index * 8 + 2] & 0xFF) << 40) | + (((long)byteByffer.array[start + index * 8 + 3] & 0xFF) << 32) | + (((long)byteByffer.array[start + index * 8 + 4] & 0xFF) << 24) | + (((long)byteByffer.array[start + index * 8 + 5] & 0xFF) << 16) | + (((long)byteByffer.array[start + index * 8 + 6] & 0xFF) << 8) | + (byteByffer.array[start + index * 8 + 7] & 0xFF); + } + + @Override + void putElement(int index, long value) { + byteByffer.array[start + index * 8] = (byte)(value >> 56); + byteByffer.array[start + index * 8 + 1] = (byte)(value >> 48); + byteByffer.array[start + index * 8 + 2] = (byte)(value >> 40); + byteByffer.array[start + index * 8 + 3] = (byte)(value >> 32); + byteByffer.array[start + index * 8 + 4] = (byte)(value >> 24); + byteByffer.array[start + index * 8 + 5] = (byte)(value >> 16); + byteByffer.array[start + index * 8 + 6] = (byte)(value >> 8); + byteByffer.array[start + index * 8 + 7] = (byte)value; + } + + @Override + public TByteOrder order() { + return TByteOrder.BIG_ENDIAN; + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TLongBufferOverByteBufferLittleEndian.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TLongBufferOverByteBufferLittleEndian.java new file mode 100644 index 000000000..50d3329e1 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TLongBufferOverByteBufferLittleEndian.java @@ -0,0 +1,62 @@ +/* + * 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 + */ +class TLongBufferOverByteBufferLittleEndian extends TLongBufferOverByteBuffer { + public TLongBufferOverByteBufferLittleEndian(int start, int capacity, TByteBufferImpl byteBuffer, int position, + int limit, boolean readOnly) { + super(start, capacity, byteBuffer, position, limit, readOnly); + } + + @Override + TLongBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) { + return new TLongBufferOverByteBufferLittleEndian(this.start + start * 8, capacity, + byteByffer, position, limit, readOnly); + } + + @Override + long getElement(int index) { + return (byteByffer.array[start + index * 8] & 0xFF) | + (((long)byteByffer.array[start + index * 8 + 1] & 0xFF) << 8) | + (((long)byteByffer.array[start + index * 8 + 2] & 0xFF) << 16) | + (((long)byteByffer.array[start + index * 8 + 3] & 0xFF) << 24) | + (((long)byteByffer.array[start + index * 8 + 4] & 0xFF) << 32) | + (((long)byteByffer.array[start + index * 8 + 5] & 0xFF) << 40) | + (((long)byteByffer.array[start + index * 8 + 6] & 0xFF) << 48) | + (((long)byteByffer.array[start + index * 8 + 7] & 0xFF) << 56); + } + + @Override + void putElement(int index, long value) { + byteByffer.array[start + index * 8] = (byte)value; + byteByffer.array[start + index * 8 + 1] = (byte)(value >> 8); + byteByffer.array[start + index * 8 + 2] = (byte)(value >> 16); + byteByffer.array[start + index * 8 + 3] = (byte)(value >> 24); + byteByffer.array[start + index * 8 + 4] = (byte)(value >> 32); + byteByffer.array[start + index * 8 + 5] = (byte)(value >> 40); + byteByffer.array[start + index * 8 + 6] = (byte)(value >> 48); + byteByffer.array[start + index * 8 + 7] = (byte)(value >> 56); + } + + @Override + public TByteOrder order() { + return TByteOrder.LITTLE_ENDIAN; + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TShortBufferOverByteBuffer.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TShortBufferOverByteBuffer.java index 516e1ceb8..9433bbf16 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TShortBufferOverByteBuffer.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TShortBufferOverByteBuffer.java @@ -19,11 +19,10 @@ package org.teavm.classlib.java.nio; * * @author Alexey Andreev */ -class TShortBufferOverByteBuffer extends TShortBufferImpl { - private TByteBufferImpl byteByffer; - TByteOrder byteOrder = TByteOrder.BIG_ENDIAN; +abstract class TShortBufferOverByteBuffer extends TShortBufferImpl { + TByteBufferImpl byteByffer; boolean readOnly; - private int start; + int start; public TShortBufferOverByteBuffer(int start, int capacity, TByteBufferImpl byteBuffer, int position, int limit, boolean readOnly) { @@ -33,38 +32,6 @@ class TShortBufferOverByteBuffer extends TShortBufferImpl { 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] & 0xFF) << 8) | - (byteByffer.array[start + index * 2 + 1] & 0xFF); - } else { - value = ((byteByffer.array[start + index * 2 + 1] & 0xFF) << 8) | - (byteByffer.array[start + index * 2] & 0xFF); - } - 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; @@ -84,9 +51,4 @@ class TShortBufferOverByteBuffer extends TShortBufferImpl { boolean readOnly() { return readOnly; } - - @Override - public TByteOrder order() { - return byteOrder; - } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TShortBufferOverByteBufferBigEndian.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TShortBufferOverByteBufferBigEndian.java new file mode 100644 index 000000000..2130fd554 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TShortBufferOverByteBufferBigEndian.java @@ -0,0 +1,51 @@ +/* + * 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 + */ +class TShortBufferOverByteBufferBigEndian extends TShortBufferOverByteBuffer { + public TShortBufferOverByteBufferBigEndian(int start, int capacity, TByteBufferImpl byteBuffer, int position, + int limit, boolean readOnly) { + super(start, capacity, byteBuffer, position, limit, readOnly); + } + + @Override + TShortBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) { + return new TShortBufferOverByteBufferBigEndian(this.start + start * 2, capacity, + byteByffer, position, limit, readOnly); + } + + @Override + short getElement(int index) { + int value = ((byteByffer.array[start + index * 2] & 0xFF) << 8) | + (byteByffer.array[start + index * 2 + 1] & 0xFF); + return (short)value; + } + + @Override + void putElement(int index, short value) { + byteByffer.array[start + index * 2] = (byte)(value >> 8); + byteByffer.array[start + index * 2 + 1] = (byte)value; + } + + @Override + public TByteOrder order() { + return TByteOrder.BIG_ENDIAN; + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TShortBufferOverByteBufferLittleEndian.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TShortBufferOverByteBufferLittleEndian.java new file mode 100644 index 000000000..771723680 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TShortBufferOverByteBufferLittleEndian.java @@ -0,0 +1,51 @@ +/* + * 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 + */ +class TShortBufferOverByteBufferLittleEndian extends TShortBufferOverByteBuffer { + public TShortBufferOverByteBufferLittleEndian(int start, int capacity, TByteBufferImpl byteBuffer, int position, + int limit, boolean readOnly) { + super(start, capacity, byteBuffer, position, limit, readOnly); + } + + @Override + TShortBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) { + return new TShortBufferOverByteBufferLittleEndian(this.start + start * 2, capacity, + byteByffer, position, limit, readOnly); + } + + @Override + short getElement(int index) { + int value = (byteByffer.array[start + index * 2] & 0xFF) | + ((byteByffer.array[start + index * 2 + 1] & 0xFF) << 8); + return (short)value; + } + + @Override + void putElement(int index, short value) { + byteByffer.array[start + index * 2] = (byte)value; + byteByffer.array[start + index * 2 + 1] = (byte)(value >> 8); + } + + @Override + public TByteOrder order() { + return TByteOrder.LITTLE_ENDIAN; + } +}