Fix signed byte to int conversion in ByteBuffer wrappers. Refactor

DoubleBuffer
This commit is contained in:
konsoletyper 2014-11-04 14:32:57 +03:00
parent 999e02d316
commit 3a0db006bb
11 changed files with 385 additions and 87 deletions

View File

@ -199,6 +199,14 @@ public abstract class TByteBuffer extends TBuffer implements TComparable<TByteBu
return this; return this;
} }
public abstract char getChar();
public abstract TByteBuffer putChar(char value);
public abstract char getChar(int index);
public abstract TByteBuffer putChar(int index, char value);
public abstract TCharBuffer asCharBuffer(); public abstract TCharBuffer asCharBuffer();
public abstract TShortBuffer asShortBuffer(); public abstract TShortBuffer asShortBuffer();
@ -208,4 +216,6 @@ public abstract class TByteBuffer extends TBuffer implements TComparable<TByteBu
public abstract TLongBuffer asLongBuffer(); public abstract TLongBuffer asLongBuffer();
public abstract TFloatBuffer asFloatBuffer(); public abstract TFloatBuffer asFloatBuffer();
public abstract TDoubleBuffer asDoubleBuffer();
} }

View File

@ -103,6 +103,71 @@ class TByteBufferImpl extends TByteBuffer {
return readOnly; return readOnly;
} }
@Override
public char getChar() {
if (position + 1 >= limit) {
throw new TBufferUnderflowException();
}
int a = array[start + position] & 0xFF;
int b = array[start + position + 1] & 0xFF;
position += 2;
if (order == TByteOrder.BIG_ENDIAN) {
return (char)(a << 8 | b);
} else {
return (char)(b << 8 | a);
}
}
@Override
public TByteBuffer putChar(char value) {
if (readOnly) {
throw new TReadOnlyBufferException();
}
if (position + 1 >= limit) {
throw new TBufferOverflowException();
}
if (order == TByteOrder.BIG_ENDIAN) {
array[start + position++] = (byte)(value >> 8);
array[start + position++] = (byte)value;
} else {
array[start + position++] = (byte)value;
array[start + position++] = (byte)(value >> 8);
}
return this;
}
@Override
public char getChar(int index) {
if (index < 0 || index + 1 >= limit) {
throw new IndexOutOfBoundsException("Index " + index + " is outside of range [0;" + (limit - 1) + ")");
}
int a = array[start + index] & 0xFF;
int b = array[start + index + 1] & 0xFF;
if (order == TByteOrder.BIG_ENDIAN) {
return (char)(a << 8 | b);
} else {
return (char)(b << 8 | a);
}
}
@Override
public TByteBuffer putChar(int index, char value) {
if (readOnly) {
throw new TReadOnlyBufferException();
}
if (index < 0 || index + 1 >= limit) {
throw new IndexOutOfBoundsException("Index " + index + " is outside of range [0;" + (limit - 1) + ")");
}
if (order == TByteOrder.BIG_ENDIAN) {
array[start + index] = (byte)(value >> 8);
array[start + index + 1] = (byte)value;
} else {
array[start + index] = (byte)value;
array[start + index + 1] = (byte)(value >> 8);
}
return this;
}
@Override @Override
public TCharBuffer asCharBuffer() { public TCharBuffer asCharBuffer() {
int sz = remaining() / 2; int sz = remaining() / 2;
@ -147,4 +212,13 @@ class TByteBufferImpl extends TByteBuffer {
result.byteOrder = order; result.byteOrder = order;
return result; return result;
} }
@Override
public TDoubleBuffer asDoubleBuffer() {
int sz = remaining() / 8;
TDoubleBufferOverByteBuffer result = new TDoubleBufferOverByteBuffer(start + position, sz, this, 0, sz,
isReadOnly());
result.byteOrder = order;
return result;
}
} }

View File

@ -45,9 +45,11 @@ class TCharBufferOverByteBuffer extends TCharBufferImpl {
char getChar(int index) { char getChar(int index) {
int value; int value;
if (byteOrder == TByteOrder.BIG_ENDIAN) { if (byteOrder == TByteOrder.BIG_ENDIAN) {
value = (byteByffer.array[start + index * 2] << 8) | (byteByffer.array[start + index * 2 + 1]); value = ((byteByffer.array[start + index * 2] & 0xFF) << 8) |
(byteByffer.array[start + index * 2 + 1] & 0xFF);
} else { } else {
value = (byteByffer.array[start + index * 2 + 1] << 8) | (byteByffer.array[start + index * 2]); value = ((byteByffer.array[start + index * 2 + 1] & 0xFF) << 8) |
(byteByffer.array[start + index * 2] & 0xFF);
} }
return (char)value; return (char)value;
} }

View File

@ -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 TDoubleBuffer extends TBuffer implements Comparable<TDoubleBuffer> { public abstract class TDoubleBuffer extends TBuffer implements Comparable<TDoubleBuffer> {
int start; TDoubleBuffer(int capacity, int position, int limit) {
double[] array;
TDoubleBuffer(int start, int capacity, double[] 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 TDoubleBuffer extends TBuffer implements Comparable<TDoubl
if (capacity < 0) { if (capacity < 0) {
throw new IllegalArgumentException("Capacity is negative: " + capacity); throw new IllegalArgumentException("Capacity is negative: " + capacity);
} }
return new TDoubleBufferImpl(capacity); return new TDoubleBufferOverArray(capacity);
} }
public static TDoubleBuffer wrap(double[] array, int offset, int length) { public static TDoubleBuffer wrap(double[] array, int offset, int length) {
return new TDoubleBufferImpl(0, array.length, array, offset, offset + length, false); return new TDoubleBufferOverArray(0, array.length, array, offset, offset + length, false);
} }
public static TDoubleBuffer wrap(double[] array) { public static TDoubleBuffer wrap(double[] array) {
@ -60,6 +55,10 @@ public abstract class TDoubleBuffer extends TBuffer implements Comparable<TDoubl
public abstract TDoubleBuffer put(int index, double b); public abstract TDoubleBuffer put(int index, double b);
abstract double getElement(int index);
abstract void putElement(int index, double value);
public TDoubleBuffer get(double[] dst, int offset, int length) { public TDoubleBuffer get(double[] 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 TDoubleBuffer extends TBuffer implements Comparable<TDoubl
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 TDoubleBuffer extends TBuffer implements Comparable<TDoubl
} }
public TDoubleBuffer put(TDoubleBuffer src) { public TDoubleBuffer put(TDoubleBuffer 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 TDoubleBuffer put(double[] src, int offset, int length) { public TDoubleBuffer put(double[] src, int offset, int length) {
@ -107,9 +119,9 @@ public abstract class TDoubleBuffer extends TBuffer implements Comparable<TDoubl
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 TDoubleBuffer extends TBuffer implements Comparable<TDoubl
} }
@Override @Override
public boolean hasArray() { public final boolean hasArray() {
return true; return isArrayPresent();
} }
@Override @Override
public final double[] array() { public final double[] array() {
return array; return getArray();
} }
@Override @Override
public int arrayOffset() { public final int arrayOffset() {
return start; return getArrayOffset();
} }
abstract boolean isArrayPresent();
abstract double[] getArray();
abstract int getArrayOffset();
public abstract TDoubleBuffer compact(); public abstract TDoubleBuffer compact();
@Override @Override
@ -148,10 +166,10 @@ public abstract class TDoubleBuffer extends TBuffer implements Comparable<TDoubl
@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) {
long e = Double.doubleToLongBits(array[pos++]); long elem = Double.doubleToLongBits(getElement(pos++));
hashCode = 31 * hashCode + (int)e + (int)(e >>> 32); hashCode = 31 * hashCode + (int)elem + (int)(elem >>> 32);
} }
return hashCode; return hashCode;
} }
@ -169,10 +187,10 @@ public abstract class TDoubleBuffer extends TBuffer implements Comparable<TDoubl
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;
} }
} }
@ -185,14 +203,16 @@ public abstract class TDoubleBuffer extends TBuffer implements Comparable<TDoubl
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 = Double.compare(array[a++], other.array[b++]); int r = Double.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();
} }

View File

@ -19,50 +19,45 @@ package org.teavm.classlib.java.nio;
* *
* @author Alexey Andreev <konsoletyper@gmail.com> * @author Alexey Andreev <konsoletyper@gmail.com>
*/ */
class TDoubleBufferImpl extends TDoubleBuffer { abstract class TDoubleBufferImpl extends TDoubleBuffer {
private boolean readOnly; public TDoubleBufferImpl(int capacity, int position, int limit) {
super(capacity, position, limit);
public TDoubleBufferImpl(int capacity) {
this(0, capacity, new double[capacity], 0, capacity, false);
}
public TDoubleBufferImpl(int start, int capacity, double[] array, int position, int limit, boolean readOnly) {
super(start, capacity, array, position, limit);
this.readOnly = readOnly;
} }
@Override @Override
public TDoubleBuffer slice() { public TDoubleBuffer slice() {
return new TDoubleBufferImpl(position, limit - position, array, 0, limit - position, readOnly); return duplicate(position, limit - position, 0, limit - position, isReadOnly());
} }
@Override @Override
public TDoubleBuffer duplicate() { public TDoubleBuffer duplicate() {
return new TDoubleBufferImpl(start, capacity, array, position, limit, readOnly); return duplicate(0, capacity, position, limit, isReadOnly());
} }
@Override @Override
public TDoubleBuffer asReadOnlyBuffer() { public TDoubleBuffer asReadOnlyBuffer() {
return new TDoubleBufferImpl(start, capacity, array, position, limit, true); return duplicate(0, capacity, position, limit, true);
} }
abstract TDoubleBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly);
@Override @Override
public double get() { public double get() {
if (position >= limit) { if (position >= limit) {
throw new TBufferUnderflowException(); throw new TBufferUnderflowException();
} }
return array[start + position++]; return getElement(position++);
} }
@Override @Override
public TDoubleBuffer put(double b) { public TDoubleBuffer put(double 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 TDoubleBufferImpl extends TDoubleBuffer {
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 TDoubleBuffer put(int index, double b) { public TDoubleBuffer put(int index, double 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 TDoubleBuffer compact() { public TDoubleBuffer 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 TDoubleBufferImpl extends TDoubleBuffer {
@Override @Override
public boolean isReadOnly() { public boolean isReadOnly() {
return readOnly; return readOnly();
} }
abstract boolean readOnly();
} }

View File

@ -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 TDoubleBufferOverArray extends TDoubleBufferImpl {
boolean readOnly;
int start;
double[] array;
public TDoubleBufferOverArray(int capacity) {
this(0, capacity, new double[capacity], 0, capacity, false);
}
public TDoubleBufferOverArray(int start, int capacity, double[] array, int position, int limit, boolean readOnly) {
super(capacity, position, limit);
this.start = start;
this.readOnly = readOnly;
this.array = array;
}
@Override
TDoubleBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) {
return new TDoubleBufferOverArray(this.start + start, capacity, array, position, limit, readOnly);
}
@Override
double getElement(int index) {
return array[index + start];
}
@Override
void putElement(int index, double value) {
array[index + start] = value;
}
@Override
boolean isArrayPresent() {
return true;
}
@Override
double[] getArray() {
return array;
}
@Override
int getArrayOffset() {
return start;
}
@Override
boolean readOnly() {
return readOnly;
}
@Override
public TByteOrder order() {
return TByteOrder.BIG_ENDIAN;
}
}

View File

@ -0,0 +1,117 @@
/*
* 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 TDoubleBufferOverByteBuffer extends TDoubleBufferImpl {
private TByteBufferImpl byteByffer;
TByteOrder byteOrder = TByteOrder.BIG_ENDIAN;
boolean readOnly;
private int start;
public TDoubleBufferOverByteBuffer(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
TDoubleBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) {
TDoubleBufferOverByteBuffer result = new TDoubleBufferOverByteBuffer(this.start + start * 2, capacity,
byteByffer, position, limit, readOnly);
result.byteOrder = byteOrder;
return result;
}
@Override
double 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 Double.longBitsToDouble(value);
}
@Override
void putElement(int index, double d) {
long value = Double.doubleToLongBits(d);
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;
}
@Override
double[] getArray() {
throw new UnsupportedOperationException();
}
@Override
int getArrayOffset() {
throw new UnsupportedOperationException();
}
@Override
boolean readOnly() {
return readOnly;
}
@Override
public TByteOrder order() {
return byteOrder;
}
}

View File

@ -45,15 +45,15 @@ class TFloatBufferOverByteBuffer extends TFloatBufferImpl {
float getElement(int index) { float getElement(int index) {
int value; int value;
if (byteOrder == TByteOrder.BIG_ENDIAN) { if (byteOrder == TByteOrder.BIG_ENDIAN) {
value = (byteByffer.array[start + index * 4] << 24) | value = ((byteByffer.array[start + index * 4] & 0xFF) << 24) |
(byteByffer.array[start + index * 4 + 1] << 16) | ((byteByffer.array[start + index * 4 + 1] & 0xFF) << 16) |
(byteByffer.array[start + index * 4 + 2] << 8) | ((byteByffer.array[start + index * 4 + 2] & 0xFF) << 8) |
(byteByffer.array[start + index * 4 + 3]); (byteByffer.array[start + index * 4 + 3] & 0xFF);
} else { } else {
value = (byteByffer.array[start + index * 4]) | value = (byteByffer.array[start + index * 4] & 0xFF) |
(byteByffer.array[start + index * 4 + 1] << 8) | ((byteByffer.array[start + index * 4 + 1] & 0xFF) << 8) |
(byteByffer.array[start + index * 4 + 2] << 16) | ((byteByffer.array[start + index * 4 + 2] & 0xFF) << 16) |
(byteByffer.array[start + index * 4 + 3] << 24); ((byteByffer.array[start + index * 4 + 3] & 0xFF) << 24);
} }
return Float.intBitsToFloat(value); return Float.intBitsToFloat(value);
} }

View File

@ -45,15 +45,15 @@ public class TIntBufferOverByteBuffer extends TIntBufferImpl {
int getElement(int index) { int getElement(int index) {
int value; int value;
if (byteOrder == TByteOrder.BIG_ENDIAN) { if (byteOrder == TByteOrder.BIG_ENDIAN) {
value = (byteByffer.array[start + index * 4] << 24) | value = ((byteByffer.array[start + index * 4] & 0xFF) << 24) |
(byteByffer.array[start + index * 4 + 1] << 16) | ((byteByffer.array[start + index * 4 + 1] & 0xFF) << 16) |
(byteByffer.array[start + index * 4 + 2] << 8) | ((byteByffer.array[start + index * 4 + 2] & 0xFF) << 8) |
(byteByffer.array[start + index * 4 + 3]); (byteByffer.array[start + index * 4 + 3] & 0xFF);
} else { } else {
value = (byteByffer.array[start + index * 4]) | value = (byteByffer.array[start + index * 4] & 0xFF) |
(byteByffer.array[start + index * 4 + 1] << 8) | ((byteByffer.array[start + index * 4 + 1] & 0xFF) << 8) |
(byteByffer.array[start + index * 4 + 2] << 16) | ((byteByffer.array[start + index * 4 + 2] & 0xFF) << 16) |
(byteByffer.array[start + index * 4 + 3] << 24); ((byteByffer.array[start + index * 4 + 3] & 0xFF) << 24);
} }
return value; return value;
} }

View File

@ -45,23 +45,23 @@ class TLongBufferOverByteBuffer extends TLongBufferImpl {
long getElement(int index) { long getElement(int index) {
long value; long value;
if (byteOrder == TByteOrder.BIG_ENDIAN) { if (byteOrder == TByteOrder.BIG_ENDIAN) {
value = ((long)byteByffer.array[start + index * 8] << 56) | value = (((long)byteByffer.array[start + index * 8] & 0xFF) << 56) |
((long)byteByffer.array[start + index * 8 + 1] << 48) | (((long)byteByffer.array[start + index * 8 + 1] & 0xFF) << 48) |
((long)byteByffer.array[start + index * 8 + 2] << 40) | (((long)byteByffer.array[start + index * 8 + 2] & 0xFF) << 40) |
((long)byteByffer.array[start + index * 8 + 3] << 32) | (((long)byteByffer.array[start + index * 8 + 3] & 0xFF) << 32) |
((long)byteByffer.array[start + index * 8 + 4] << 24) | (((long)byteByffer.array[start + index * 8 + 4] & 0xFF) << 24) |
((long)byteByffer.array[start + index * 8 + 5] << 16) | (((long)byteByffer.array[start + index * 8 + 5] & 0xFF) << 16) |
((long)byteByffer.array[start + index * 8 + 6] << 8) | (((long)byteByffer.array[start + index * 8 + 6] & 0xFF) << 8) |
byteByffer.array[start + index * 8 + 7]; (byteByffer.array[start + index * 8 + 7] & 0xFF);
} else { } else {
value = byteByffer.array[start + index * 8] | value = (byteByffer.array[start + index * 8] & 0xFF) |
((long)byteByffer.array[start + index * 8 + 1] << 8) | (((long)byteByffer.array[start + index * 8 + 1] & 0xFF) << 8) |
((long)byteByffer.array[start + index * 8 + 2] << 16) | (((long)byteByffer.array[start + index * 8 + 2] & 0xFF) << 16) |
((long)byteByffer.array[start + index * 8 + 3] << 24) | (((long)byteByffer.array[start + index * 8 + 3] & 0xFF) << 24) |
((long)byteByffer.array[start + index * 8 + 4] << 32) | (((long)byteByffer.array[start + index * 8 + 4] & 0xFF) << 32) |
((long)byteByffer.array[start + index * 8 + 5] << 40) | (((long)byteByffer.array[start + index * 8 + 5] & 0xFF) << 40) |
((long)byteByffer.array[start + index * 8 + 6] << 48) | (((long)byteByffer.array[start + index * 8 + 6] & 0xFF) << 48) |
((long)byteByffer.array[start + index * 8 + 7] << 56); (((long)byteByffer.array[start + index * 8 + 7] & 0xFF) << 56);
} }
return value; return value;
} }

View File

@ -45,9 +45,11 @@ class TShortBufferOverByteBuffer extends TShortBufferImpl {
short getElement(int index) { short getElement(int index) {
int value; int value;
if (byteOrder == TByteOrder.BIG_ENDIAN) { if (byteOrder == TByteOrder.BIG_ENDIAN) {
value = (byteByffer.array[start + index * 2] << 8) | (byteByffer.array[start + index * 2 + 1]); value = ((byteByffer.array[start + index * 2] & 0xFF) << 8) |
(byteByffer.array[start + index * 2 + 1] & 0xFF);
} else { } else {
value = (byteByffer.array[start + index * 2 + 1] << 8) | (byteByffer.array[start + index * 2]); value = ((byteByffer.array[start + index * 2 + 1] & 0xFF) << 8) |
(byteByffer.array[start + index * 2] & 0xFF);
} }
return (short)value; return (short)value;
} }