Java Code Examples for java.nio.ByteBuffer#asDoubleBuffer()

The following examples show how to use java.nio.ByteBuffer#asDoubleBuffer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChannelHelper.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
	}
}
 
Example 2
Source File: ArrayUtils.java    From icafe with Eclipse Public License 1.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: Frame.java    From VideoAndroid with Apache License 2.0 6 votes vote down vote up
/** 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);
    }
}
 
Example 4
Source File: ArrayComplex.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
@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;
}
 
Example 5
Source File: StreamingGeometryGenerator.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
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());
}
 
Example 6
Source File: OfflineGeometryGenerator.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
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());
}
 
Example 7
Source File: GeometryUtils.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: ThriftBmiBridge.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
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;
	}
}
 
Example 9
Source File: CdmrfWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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;
}
 
Example 10
Source File: BinUtils.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: LittleEndianBinUtils.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 12
Source File: TypedArrayFunctions.java    From es6draft with MIT License 5 votes vote down vote up
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;
}
 
Example 13
Source File: GeometryUtils.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
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();
}
 
Example 14
Source File: BufferTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 15
Source File: ArrayDouble.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
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;
}
 
Example 16
Source File: NativeFloat64Array.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Float64ArrayData createArrayData(final ByteBuffer nb, final int start, final int length) {
    return new Float64ArrayData(nb.asDoubleBuffer(), start, length);
}
 
Example 17
Source File: NativeFloat64Array.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Float64ArrayData createArrayData(final ByteBuffer nb, final int start, final int length) {
    return new Float64ArrayData(nb.asDoubleBuffer(), start, length);
}
 
Example 18
Source File: WflowPythonToJavaAdapter.java    From OpenDA with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static DoubleBuffer createNativeDoubleBuffer(int numberOfElements) {
    ByteBuffer res = ByteBuffer.allocateDirect(numberOfElements * 8);
    res.order(ByteOrder.nativeOrder());
    return res.asDoubleBuffer();
}
 
Example 19
Source File: NativeFloat64Array.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Float64ArrayData createArrayData(final ByteBuffer nb, final int start, final int length) {
    return new Float64ArrayData(nb.asDoubleBuffer(), start, length);
}
 
Example 20
Source File: NativeFloat64Array.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Float64ArrayData createArrayData(final ByteBuffer nb, final int start, final int length) {
    return new Float64ArrayData(nb.asDoubleBuffer(), start, length);
}