use of com.jmatio.types.MLNumericArray in project org.csstudio.display.builder by kasemir.
the class MatFileIncrementalWriter method writeMatrix.
/**
* Writes MATRIX into <code>OutputStream</code>.
*
* @param os - <code>OutputStream</code>
* @param array - a <code>MLArray</code>
* @throws IOException
*/
private void writeMatrix(DataOutputStream output, MLArray array) throws IOException {
OSArrayTag tag;
ByteArrayOutputStream buffer;
DataOutputStream bufferDOS;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
// flags
writeFlags(dos, array);
// dimensions
writeDimensions(dos, array);
// array name
writeName(dos, array);
switch(array.getType()) {
case MLArray.mxCHAR_CLASS:
// write char data
buffer = new ByteArrayOutputStream();
bufferDOS = new DataOutputStream(buffer);
Character[] ac = ((MLChar) array).exportChar();
for (int i = 0; i < ac.length; i++) {
bufferDOS.writeByte((byte) ac[i].charValue());
}
tag = new OSArrayTag(MatDataTypes.miUTF8, buffer.toByteArray());
tag.writeTo(dos);
break;
case MLArray.mxDOUBLE_CLASS:
tag = new OSArrayTag(MatDataTypes.miDOUBLE, ((MLNumericArray<?>) array).getRealByteBuffer());
tag.writeTo(dos);
// write real imaginary
if (array.isComplex()) {
tag = new OSArrayTag(MatDataTypes.miDOUBLE, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
tag.writeTo(dos);
}
break;
case MLArray.mxUINT8_CLASS:
tag = new OSArrayTag(MatDataTypes.miUINT8, ((MLNumericArray<?>) array).getRealByteBuffer());
tag.writeTo(dos);
// write real imaginary
if (array.isComplex()) {
tag = new OSArrayTag(MatDataTypes.miUINT8, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
tag.writeTo(dos);
}
break;
case MLArray.mxINT8_CLASS:
tag = new OSArrayTag(MatDataTypes.miINT8, ((MLNumericArray<?>) array).getRealByteBuffer());
tag.writeTo(dos);
// write real imaginary
if (array.isComplex()) {
tag = new OSArrayTag(MatDataTypes.miINT8, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
tag.writeTo(dos);
}
break;
case MLArray.mxINT64_CLASS:
tag = new OSArrayTag(MatDataTypes.miINT64, ((MLNumericArray<?>) array).getRealByteBuffer());
tag.writeTo(dos);
// write real imaginary
if (array.isComplex()) {
tag = new OSArrayTag(MatDataTypes.miINT64, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
tag.writeTo(dos);
}
break;
case MLArray.mxUINT64_CLASS:
tag = new OSArrayTag(MatDataTypes.miUINT64, ((MLNumericArray<?>) array).getRealByteBuffer());
tag.writeTo(dos);
// write real imaginary
if (array.isComplex()) {
tag = new OSArrayTag(MatDataTypes.miUINT64, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
tag.writeTo(dos);
}
break;
case MLArray.mxSTRUCT_CLASS:
// field name length
int itag = 4 << 16 | MatDataTypes.miINT32 & 0xffff;
dos.writeInt(itag);
dos.writeInt(((MLStructure) array).getMaxFieldLenth());
// get field names
tag = new OSArrayTag(MatDataTypes.miINT8, ((MLStructure) array).getKeySetToByteArray());
tag.writeTo(dos);
for (MLArray a : ((MLStructure) array).getAllFields()) {
writeMatrix(dos, a);
}
break;
case MLArray.mxCELL_CLASS:
for (MLArray a : ((MLCell) array).cells()) {
writeMatrix(dos, a);
}
break;
case MLArray.mxSPARSE_CLASS:
int[] ai;
// write ir
buffer = new ByteArrayOutputStream();
bufferDOS = new DataOutputStream(buffer);
ai = ((MLSparse) array).getIR();
for (int i : ai) {
bufferDOS.writeInt(i);
}
tag = new OSArrayTag(MatDataTypes.miINT32, buffer.toByteArray());
tag.writeTo(dos);
// write jc
buffer = new ByteArrayOutputStream();
bufferDOS = new DataOutputStream(buffer);
ai = ((MLSparse) array).getJC();
for (int i : ai) {
bufferDOS.writeInt(i);
}
tag = new OSArrayTag(MatDataTypes.miINT32, buffer.toByteArray());
tag.writeTo(dos);
// write real
buffer = new ByteArrayOutputStream();
bufferDOS = new DataOutputStream(buffer);
Double[] ad = ((MLSparse) array).exportReal();
for (int i = 0; i < ad.length; i++) {
bufferDOS.writeDouble(ad[i].doubleValue());
}
tag = new OSArrayTag(MatDataTypes.miDOUBLE, buffer.toByteArray());
tag.writeTo(dos);
// write real imaginary
if (array.isComplex()) {
buffer = new ByteArrayOutputStream();
bufferDOS = new DataOutputStream(buffer);
ad = ((MLSparse) array).exportImaginary();
for (int i = 0; i < ad.length; i++) {
bufferDOS.writeDouble(ad[i].doubleValue());
}
tag = new OSArrayTag(MatDataTypes.miDOUBLE, buffer.toByteArray());
tag.writeTo(dos);
}
break;
default:
throw new MatlabIOException("Cannot write matrix of type: " + MLArray.typeToString(array.getType()));
}
// write matrix
// matrix tag
output.writeInt(MatDataTypes.miMATRIX);
// size of matrix
output.writeInt(baos.size());
// matrix data
output.write(baos.toByteArray());
}
use of com.jmatio.types.MLNumericArray in project org.csstudio.display.builder by kasemir.
the class MatFileWriter method writeMatrix.
/**
* Writes MATRIX into <code>OutputStream</code>.
*
* @param os - <code>OutputStream</code>
* @param array - a <code>MLArray</code>
* @throws IOException
*/
private void writeMatrix(DataOutputStream output, MLArray array) throws IOException {
OSArrayTag tag;
ByteArrayOutputStream buffer;
DataOutputStream bufferDOS;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
// flags
writeFlags(dos, array);
// dimensions
writeDimensions(dos, array);
// array name
writeName(dos, array);
switch(array.getType()) {
case MLArray.mxCHAR_CLASS:
// write char data
buffer = new ByteArrayOutputStream();
bufferDOS = new DataOutputStream(buffer);
Character[] ac = ((MLChar) array).exportChar();
for (int i = 0; i < ac.length; i++) {
bufferDOS.writeByte((byte) ac[i].charValue());
}
tag = new OSArrayTag(MatDataTypes.miUTF8, buffer.toByteArray());
tag.writeTo(dos);
break;
case MLArray.mxDOUBLE_CLASS:
tag = new OSArrayTag(MatDataTypes.miDOUBLE, ((MLNumericArray<?>) array).getRealByteBuffer());
tag.writeTo(dos);
// write real imaginary
if (array.isComplex()) {
tag = new OSArrayTag(MatDataTypes.miDOUBLE, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
tag.writeTo(dos);
}
break;
case MLArray.mxSINGLE_CLASS:
tag = new OSArrayTag(MatDataTypes.miSINGLE, ((MLNumericArray<?>) array).getRealByteBuffer());
tag.writeTo(dos);
// write real imaginary
if (array.isComplex()) {
tag = new OSArrayTag(MatDataTypes.miSINGLE, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
tag.writeTo(dos);
}
break;
case MLArray.mxUINT8_CLASS:
tag = new OSArrayTag(MatDataTypes.miUINT8, ((MLNumericArray<?>) array).getRealByteBuffer());
tag.writeTo(dos);
// write real imaginary
if (array.isComplex()) {
tag = new OSArrayTag(MatDataTypes.miUINT8, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
tag.writeTo(dos);
}
break;
case MLArray.mxINT8_CLASS:
tag = new OSArrayTag(MatDataTypes.miINT8, ((MLNumericArray<?>) array).getRealByteBuffer());
tag.writeTo(dos);
// write real imaginary
if (array.isComplex()) {
tag = new OSArrayTag(MatDataTypes.miINT8, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
tag.writeTo(dos);
}
break;
case MLArray.mxINT64_CLASS:
tag = new OSArrayTag(MatDataTypes.miINT64, ((MLNumericArray<?>) array).getRealByteBuffer());
tag.writeTo(dos);
// write real imaginary
if (array.isComplex()) {
tag = new OSArrayTag(MatDataTypes.miINT64, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
tag.writeTo(dos);
}
break;
case MLArray.mxUINT64_CLASS:
tag = new OSArrayTag(MatDataTypes.miUINT64, ((MLNumericArray<?>) array).getRealByteBuffer());
tag.writeTo(dos);
// write real imaginary
if (array.isComplex()) {
tag = new OSArrayTag(MatDataTypes.miUINT64, ((MLNumericArray<?>) array).getImaginaryByteBuffer());
tag.writeTo(dos);
}
break;
case MLArray.mxSTRUCT_CLASS:
// field name length
int itag = 4 << 16 | MatDataTypes.miINT32 & 0xffff;
dos.writeInt(itag);
dos.writeInt(((MLStructure) array).getMaxFieldLenth());
// get field names
tag = new OSArrayTag(MatDataTypes.miINT8, ((MLStructure) array).getKeySetToByteArray());
tag.writeTo(dos);
for (MLArray a : ((MLStructure) array).getAllFields()) {
writeMatrix(dos, a);
}
break;
case MLArray.mxCELL_CLASS:
for (MLArray a : ((MLCell) array).cells()) {
writeMatrix(dos, a);
}
break;
case MLArray.mxSPARSE_CLASS:
int[] ai;
// write ir
buffer = new ByteArrayOutputStream();
bufferDOS = new DataOutputStream(buffer);
ai = ((MLSparse) array).getIR();
for (int i : ai) {
bufferDOS.writeInt(i);
}
tag = new OSArrayTag(MatDataTypes.miINT32, buffer.toByteArray());
tag.writeTo(dos);
// write jc
buffer = new ByteArrayOutputStream();
bufferDOS = new DataOutputStream(buffer);
ai = ((MLSparse) array).getJC();
for (int i : ai) {
bufferDOS.writeInt(i);
}
tag = new OSArrayTag(MatDataTypes.miINT32, buffer.toByteArray());
tag.writeTo(dos);
// write real
buffer = new ByteArrayOutputStream();
bufferDOS = new DataOutputStream(buffer);
Double[] ad = ((MLSparse) array).exportReal();
for (int i = 0; i < ad.length; i++) {
bufferDOS.writeDouble(ad[i].doubleValue());
}
tag = new OSArrayTag(MatDataTypes.miDOUBLE, buffer.toByteArray());
tag.writeTo(dos);
// write real imaginary
if (array.isComplex()) {
buffer = new ByteArrayOutputStream();
bufferDOS = new DataOutputStream(buffer);
ad = ((MLSparse) array).exportImaginary();
for (int i = 0; i < ad.length; i++) {
bufferDOS.writeDouble(ad[i].doubleValue());
}
tag = new OSArrayTag(MatDataTypes.miDOUBLE, buffer.toByteArray());
tag.writeTo(dos);
}
break;
default:
throw new MatlabIOException("Cannot write matrix of type: " + MLArray.typeToString(array.getType()));
}
// write matrix
// matrix tag
output.writeInt(MatDataTypes.miMATRIX);
// size of matrix
output.writeInt(baos.size());
// matrix data
output.write(baos.toByteArray());
}
Aggregations