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

The following examples show how to use java.nio.ByteBuffer#asDoubleBuffer() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: VideoAndroid   File: Frame.java    License: 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 2
Source Project: icafe   File: ArrayUtils.java    License: 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 Project: libcommon   File: ChannelHelper.java    License: 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 4
Source Project: j2objc   File: BufferTest.java    License: 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 5
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 6
@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 7
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 8
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 9
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 10
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 11
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 12
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 13
Source Project: es6draft   File: TypedArrayFunctions.java    License: 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 14
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 15
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
@Override
public Float64ArrayData createArrayData(final ByteBuffer nb, final int start, final int length) {
    return new Float64ArrayData(nb.asDoubleBuffer(), start, length);
}
 
Example 17
@Override
public Float64ArrayData createArrayData(final ByteBuffer nb, final int start, final int length) {
    return new Float64ArrayData(nb.asDoubleBuffer(), start, length);
}
 
Example 18
public static DoubleBuffer createNativeDoubleBuffer(int numberOfElements) {
    ByteBuffer res = ByteBuffer.allocateDirect(numberOfElements * 8);
    res.order(ByteOrder.nativeOrder());
    return res.asDoubleBuffer();
}
 
Example 19
@Override
public Float64ArrayData createArrayData(final ByteBuffer nb, final int start, final int length) {
    return new Float64ArrayData(nb.asDoubleBuffer(), start, length);
}
 
Example 20
@Override
public Float64ArrayData createArrayData(final ByteBuffer nb, final int start, final int length) {
    return new Float64ArrayData(nb.asDoubleBuffer(), start, length);
}