下面列出了java.nio.DoubleBuffer#array ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* ByteChannelからdouble配列を読み込む
* @param channel
* @return
* @throws IOException
*/
public static double[] readDoubleArray(@NonNull final ByteChannel channel)
throws IOException {
final int n = readInt(channel);
final ByteBuffer buf = ByteBuffer.allocate(n * 8).order(ByteOrder.BIG_ENDIAN);
final int readBytes = channel.read(buf);
if (readBytes != n * 8) throw new IOException();
buf.clear();
final DoubleBuffer result = buf.asDoubleBuffer();
if (result.hasArray()) {
return result.array();
} else {
final double[] b = new double[n];
result.get(b);
return b;
}
}
private static double[] bufferToDoubleArray(ByteBuffer buffer) {
buffer.order(ByteOrder.nativeOrder());
DoubleBuffer doubles = buffer.asDoubleBuffer();
if (doubles.hasArray()) {
return doubles.array();
} else {
double[] resultArray = new double[doubles.capacity()];
doubles.get(resultArray);
return resultArray;
}
}
@Override
public double[] toArray() {
DoubleBuffer buffer = DoubleBuffer.allocate(metadata.getIndex().getPointCount());
for (int i = 0; i < metadata.getIndex().getPointCount(); i++) {
buffer.put(i, Double.NaN);
}
fillBuffer(buffer, 0);
return buffer.array();
}
static
public double[] toDoubleArray(Tensor tensor){
DoubleBuffer doubleBuffer = DoubleBuffer.allocate(tensor.numElements());
tensor.writeTo(doubleBuffer);
return doubleBuffer.array();
}
/**
* For direct buffers, calls {@link Pointer#Pointer(Buffer)}, while for buffers
* backed with an array, allocates enough memory for the array and copies it.
*
* @param buffer the Buffer to reference or copy
* @see #put(double[])
*/
public DoublePointer(DoubleBuffer buffer) {
super(buffer);
if (buffer != null && buffer.hasArray()) {
double[] array = buffer.array();
allocateArray(array.length);
put(array);
position(buffer.position());
limit(buffer.limit());
}
}
@Override
public List<Field> createListField(Tensor<Double> tensor, DoubleBuffer doubleBuffer) {
List<Field> fields = new ArrayList<>();
tensor.writeTo(doubleBuffer);
double[] doubles = doubleBuffer.array();
for (double aDouble : doubles) {
fields.add(Field.create(aDouble));
}
return fields;
}
/**
* Create a new {@link JTensor} instance by extracting data from the underlying {@link Tensor} and
* closing it afterwards.
*/
public static JTensor create(final Tensor<?> tensor) {
final JTensor jt;
try {
switch (tensor.dataType()) {
case STRING:
if (tensor.numDimensions() == 0) {
final String value = new String(tensor.bytesValue(), UTF_8);
jt =
new AutoValue_JTensor(
tensor.dataType(), tensor.numDimensions(), tensor.shape(), value);
} else {
final int[] dimensions = toIntExact(tensor.shape());
final Object byteArray =
tensor.copyTo(Array.newInstance(byte[].class, toIntExact(tensor.shape())));
jt =
new AutoValue_JTensor(
tensor.dataType(),
tensor.numDimensions(),
tensor.shape(),
toStringArray(byteArray, tensor.numElements(), dimensions));
}
break;
case INT32:
final IntBuffer intBuf = IntBuffer.allocate(tensor.numElements());
tensor.writeTo(intBuf);
jt =
new AutoValue_JTensor(
tensor.dataType(), tensor.numDimensions(), tensor.shape(), intBuf.array());
break;
case INT64:
final LongBuffer longBuf = LongBuffer.allocate(tensor.numElements());
tensor.writeTo(longBuf);
jt =
new AutoValue_JTensor(
tensor.dataType(), tensor.numDimensions(), tensor.shape(), longBuf.array());
break;
case FLOAT:
final FloatBuffer floatBuf = FloatBuffer.allocate(tensor.numElements());
tensor.writeTo(floatBuf);
jt =
new AutoValue_JTensor(
tensor.dataType(), tensor.numDimensions(), tensor.shape(), floatBuf.array());
break;
case DOUBLE:
final DoubleBuffer doubleBuf = DoubleBuffer.allocate(tensor.numElements());
tensor.writeTo(doubleBuf);
jt =
new AutoValue_JTensor(
tensor.dataType(), tensor.numDimensions(), tensor.shape(), doubleBuf.array());
break;
case BOOL:
final boolean[] array = new boolean[tensor.numElements()];
tensor.copyTo(array);
jt =
new AutoValue_JTensor(
tensor.dataType(), tensor.numDimensions(), tensor.shape(), array);
break;
default:
throw new IllegalStateException("Unsupported data type " + tensor.dataType());
}
} finally {
tensor.close();
}
return jt;
}
@Override
public double[] toArray() {
DoubleBuffer buffer = DoubleBuffer.allocate(metadata.getIndex().getPointCount());
fillBuffer(buffer, 0);
return buffer.array();
}