com.xxdb.data.BasicFloatMatrix Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dolphindb-javaapi Show documentation
Show all versions of dolphindb-javaapi Show documentation
The messaging and data conversion protocol between Java and DolphinDB server
package com.xxdb.data;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.xxdb.io.ExtendedDataInput;
import com.xxdb.io.ExtendedDataOutput;
/**
*
* Corresponds to DolphinDB float matrix
*
*/
public class BasicFloatMatrix extends AbstractMatrix{
private float[] values;
public BasicFloatMatrix(int rows, int columns){
super(rows, columns);
values = new float[rows * columns];
}
public BasicFloatMatrix(int rows, int columns, List listOfArrays) throws Exception {
super(rows,columns);
values = new float[rows*columns];
if (listOfArrays == null || listOfArrays.size() != columns)
throw new Exception("input list of arrays does not have " + columns + " columns");
for (int i=0; i getElementClass(){
return BasicFloat.class;
}
@Override
protected void readMatrixFromInputStream(int rows, int columns, ExtendedDataInput in) throws IOException{
int size = rows * columns;
values =new float[size];
long totalBytes = (long)size * 4, off = 0;
ByteOrder bo = in.isLittleEndian() ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN;
while (off < totalBytes) {
int len = (int)Math.min(BUF_SIZE, totalBytes - off);
in.readFully(buf, 0, len);
int start = (int)(off / 4), end = len / 4;
ByteBuffer byteBuffer = ByteBuffer.wrap(buf, 0, len).order(bo);
for (int i = 0; i < end; i++)
values[i + start] = byteBuffer.getFloat(i * 4);
off += len;
}
}
protected void writeVectorToOutputStream(ExtendedDataOutput out) throws IOException{
for(float value : values)
out.writeFloat(value);
}
@JsonIgnore
@Override
public int getScale() {
return super.getScale();
}
}