下面列出了java.nio.ByteBuffer#asDoubleBuffer ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/** Allocates a new packed image frame in native memory where rows are 8-byte aligned. */
public Frame(int width, int height, int depth, int channels) {
int pixelSize = Math.abs(depth) / 8;
this.imageWidth = width;
this.imageHeight = height;
this.imageDepth = depth;
this.imageChannels = channels;
this.imageStride = ((imageWidth * imageChannels * pixelSize + 7) & ~7) / pixelSize; // 8-byte aligned
this.image = new Buffer[1];
ByteBuffer buffer = ByteBuffer.allocateDirect(imageHeight * imageStride * pixelSize).order(ByteOrder.nativeOrder());
switch (imageDepth) {
case DEPTH_BYTE:
case DEPTH_UBYTE: image[0] = buffer; break;
case DEPTH_SHORT:
case DEPTH_USHORT: image[0] = buffer.asShortBuffer(); break;
case DEPTH_INT: image[0] = buffer.asIntBuffer(); break;
case DEPTH_LONG: image[0] = buffer.asLongBuffer(); break;
case DEPTH_FLOAT: image[0] = buffer.asFloatBuffer(); break;
case DEPTH_DOUBLE: image[0] = buffer.asDoubleBuffer(); break;
default: throw new UnsupportedOperationException("Unsupported depth value: " + imageDepth);
}
}
public static double[] toDoubleArray(byte[] data, int offset, int len, boolean bigEndian) {
ByteBuffer byteBuffer = ByteBuffer.wrap(data, offset, len);
if (bigEndian) {
byteBuffer.order(ByteOrder.BIG_ENDIAN);
} else {
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
}
DoubleBuffer doubleBuf = byteBuffer.asDoubleBuffer();
double[] array = new double[doubleBuf.remaining()];
doubleBuf.get(array);
return array;
}
/**
* 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 void testBuffersIndependentLimit(ByteBuffer b) {
CharBuffer c = b.asCharBuffer();
b.limit(b.capacity()); c.put(1, (char)1);
b.limit(0); c.put(1, (char)1);
b.limit(b.capacity());
ShortBuffer s = b.asShortBuffer();
b.limit(b.capacity()); s.put(1, (short)1);
b.limit(0); s.put(1, (short)1);
b.limit(b.capacity());
IntBuffer i = b.asIntBuffer();
i.put(1, (int)1);
b.limit(0); i.put(1, (int)1);
b.limit(b.capacity());
LongBuffer l = b.asLongBuffer();
l.put(1, (long)1);
b.limit(0); l.put(1, (long)1);
b.limit(b.capacity());
FloatBuffer f = b.asFloatBuffer();
f.put(1, (float)1);
b.limit(0); f.put(1, (float)1);
b.limit(b.capacity());
DoubleBuffer d = b.asDoubleBuffer();
d.put(1, (double)1);
b.limit(0); d.put(1, (double)1);
}
CdmrFeatureProto.CoordAxis.Builder encodeCoordAxis(CoverageCoordAxis axis) {
CdmrFeatureProto.CoordAxis.Builder builder = CdmrFeatureProto.CoordAxis.newBuilder();
builder.setName(axis.getName());
builder.setDataType(NcStream.convertDataType(axis.getDataType()));
builder.setAxisType(convertAxisType(axis.getAxisType()));
builder.setNvalues(axis.getNcoords());
if (axis.getUnits() != null)
builder.setUnits(axis.getUnits());
if (axis.getDescription() != null)
builder.setDescription(axis.getDescription());
builder.setDepend(convertDependenceType(axis.getDependenceType()));
for (String s : axis.getDependsOnList())
builder.addDependsOn(s);
if (axis instanceof LatLonAxis2D) {
LatLonAxis2D latlon2D = (LatLonAxis2D) axis;
for (int shape : latlon2D.getShape())
builder.addShape(shape);
}
for (Attribute att : axis.getAttributes())
builder.addAtts(NcStream.encodeAtt(att));
builder.setSpacing(convertSpacing(axis.getSpacing()));
builder.setStartValue(axis.getStartValue());
builder.setEndValue(axis.getEndValue());
builder.setResolution(axis.getResolution());
if (!axis.isRegular() && axis.getNcoords() < MAX_INLINE_NVALUES) {
double[] values = axis.getValues();
ByteBuffer bb = ByteBuffer.allocate(8 * values.length);
DoubleBuffer db = bb.asDoubleBuffer();
db.put(values);
builder.setValues(ByteString.copyFrom(bb.array()));
}
return builder;
}
@Override
public ByteBuffer getDataAsByteBuffer() {
ByteBuffer bb = ByteBuffer.allocate((int) (8 * getSize()));
DoubleBuffer ib = bb.asDoubleBuffer();
ib.put((double[]) get1DJavaArray(double.class)); // make sure its in canonical order
return bb;
}
void setTransformationMatrix(VirtualObject geometryInfo, double[] transformationMatrix) throws BimserverDatabaseException {
ByteBuffer byteBuffer = ByteBuffer.allocate(16 * 8);
byteBuffer.order(ByteOrder.nativeOrder());
DoubleBuffer asDoubleBuffer = byteBuffer.asDoubleBuffer();
for (double d : transformationMatrix) {
asDoubleBuffer.put(d);
}
geometryInfo.setAttribute(GeometryPackage.eINSTANCE.getGeometryInfo_Transformation(), byteBuffer.array());
}
private void setTransformationMatrix(GeometryInfo geometryInfo, double[] transformationMatrix) {
ByteBuffer byteBuffer = ByteBuffer.allocate(16 * 8);
byteBuffer.order(ByteOrder.nativeOrder());
DoubleBuffer asDoubleBuffer = byteBuffer.asDoubleBuffer();
for (double f : transformationMatrix) {
asDoubleBuffer.put(f);
}
geometryInfo.setTransformation(byteBuffer.array());
}
public static double[] toDoubleArray(byte[] data) {
ByteBuffer buffer = ByteBuffer.wrap(data);
buffer.order(ByteOrder.LITTLE_ENDIAN);
DoubleBuffer doubleBuffer = buffer.asDoubleBuffer();
double[] result = new double[data.length / 8];
for (int i=0; i<data.length / 8; i++) {
result[i] = doubleBuffer.get();
}
return result;
}
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;
}
}
public static byte[] doubleToByteArray(Double inDouble) {
byte[] bArray = new byte[8];
ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
DoubleBuffer lBuffer = bBuffer.asDoubleBuffer();
lBuffer.put(inDouble);
return bArray;
}
public static byte[] doubleToByteArray(Double inDouble) {
byte[] bArray = new byte[8];
ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
bBuffer.order(ByteOrder.LITTLE_ENDIAN);
DoubleBuffer lBuffer = bBuffer.asDoubleBuffer();
lBuffer.put(inDouble);
return bArray;
}
private static final DoubleBuffer asDoubleBuffer(TypedArrayObject typedArray) {
ByteBuffer data = byteBuffer(typedArray);
int byteOffset = byteOffset(typedArray);
int byteLength = byteLength(typedArray);
data.limit(byteOffset + byteLength).position(byteOffset);
DoubleBuffer view = data.asDoubleBuffer();
data.clear();
return view;
}
public static byte[] doubleArrayToByteArray(double[] vertices) {
if (vertices == null) {
return null;
}
ByteBuffer buffer = ByteBuffer.wrap(new byte[vertices.length * 4]);
buffer.order(ByteOrder.LITTLE_ENDIAN);
DoubleBuffer asDoubleBuffer = buffer.asDoubleBuffer();
for (double d : vertices) {
asDoubleBuffer.put(d);
}
return buffer.array();
}
public ByteBuffer getDataAsByteBuffer() {
ByteBuffer bb = ByteBuffer.allocate((int) (8 * getSize()));
DoubleBuffer ib = bb.asDoubleBuffer();
ib.put((double[]) get1DJavaArray(DataType.DOUBLE)); // make sure its in canonical order
return bb;
}
@Override
public Float64ArrayData createArrayData(final ByteBuffer nb, final int start, final int length) {
return new Float64ArrayData(nb.asDoubleBuffer(), start, length);
}
@Override
public Float64ArrayData createArrayData(final ByteBuffer nb, final int start, final int length) {
return new Float64ArrayData(nb.asDoubleBuffer(), start, length);
}
public static DoubleBuffer createNativeDoubleBuffer(int numberOfElements) {
ByteBuffer res = ByteBuffer.allocateDirect(numberOfElements * 8);
res.order(ByteOrder.nativeOrder());
return res.asDoubleBuffer();
}
@Override
public Float64ArrayData createArrayData(final ByteBuffer nb, final int start, final int length) {
return new Float64ArrayData(nb.asDoubleBuffer(), start, length);
}
@Override
public Float64ArrayData createArrayData(final ByteBuffer nb, final int start, final int length) {
return new Float64ArrayData(nb.asDoubleBuffer(), start, length);
}