use of org.bytedeco.javacpp.BytePointer in project nd4j by deeplearning4j.
the class Gzip method compress.
@Override
public DataBuffer compress(DataBuffer buffer) {
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(stream);
DataOutputStream dos = new DataOutputStream(gzip);
buffer.write(dos);
dos.flush();
dos.close();
byte[] bytes = stream.toByteArray();
// logger.info("Bytes: {}", Arrays.toString(bytes));
BytePointer pointer = new BytePointer(bytes);
CompressionDescriptor descriptor = new CompressionDescriptor(buffer, this);
descriptor.setCompressedLength(bytes.length);
CompressedDataBuffer result = new CompressedDataBuffer(pointer, descriptor);
return result;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.bytedeco.javacpp.BytePointer in project nd4j by deeplearning4j.
the class Float8 method compressPointer.
@Override
protected CompressedDataBuffer compressPointer(DataBuffer.TypeEx srcType, Pointer srcPointer, int length, int elementSize) {
BytePointer ptr = new BytePointer(length);
CompressionDescriptor descriptor = new CompressionDescriptor();
descriptor.setCompressedLength(length * 1);
descriptor.setOriginalLength(length * elementSize);
descriptor.setOriginalElementSize(elementSize);
descriptor.setNumberOfElements(length);
descriptor.setCompressionAlgorithm(getDescriptor());
descriptor.setCompressionType(getCompressionType());
CompressedDataBuffer buffer = new CompressedDataBuffer(ptr, descriptor);
Nd4j.getNDArrayFactory().convertDataEx(srcType, srcPointer, DataBuffer.TypeEx.FLOAT8, ptr, length);
return buffer;
}
use of org.bytedeco.javacpp.BytePointer in project nd4j by deeplearning4j.
the class Int16 method compressPointer.
@Override
protected CompressedDataBuffer compressPointer(DataBuffer.TypeEx srcType, Pointer srcPointer, int length, int elementSize) {
BytePointer ptr = new BytePointer(length * 2);
CompressionDescriptor descriptor = new CompressionDescriptor();
descriptor.setCompressedLength(length * 2);
descriptor.setOriginalLength(length * elementSize);
descriptor.setOriginalElementSize(elementSize);
descriptor.setNumberOfElements(length);
descriptor.setCompressionAlgorithm(getDescriptor());
descriptor.setCompressionType(getCompressionType());
CompressedDataBuffer buffer = new CompressedDataBuffer(ptr, descriptor);
Nd4j.getNDArrayFactory().convertDataEx(srcType, srcPointer, DataBuffer.TypeEx.INT16, ptr, length);
return buffer;
}
use of org.bytedeco.javacpp.BytePointer in project nd4j by deeplearning4j.
the class CompressedDataBuffer method dup.
@Override
public DataBuffer dup() {
Pointer nPtr = new BytePointer(compressionDescriptor.getCompressedLength());
Pointer.memcpy(nPtr, pointer, compressionDescriptor.getCompressedLength());
CompressionDescriptor nDesc = compressionDescriptor.clone();
CompressedDataBuffer nBuf = new CompressedDataBuffer(nPtr, nDesc);
return nBuf;
}
use of org.bytedeco.javacpp.BytePointer in project nd4j by deeplearning4j.
the class CompressedDataBuffer method readUnknown.
/**
* Drop-in replacement wrapper for BaseDataBuffer.read() method, aware of CompressedDataBuffer
* @param s
* @return
*/
public static DataBuffer readUnknown(DataInputStream s, long length) {
DataBuffer buffer = Nd4j.createBuffer(length);
buffer.read(s);
// if buffer is uncompressed, it'll be valid buffer, so we'll just return it
if (buffer.dataType() != Type.COMPRESSED)
return buffer;
else {
try {
// if buffer is compressed one, we''ll restore and decompress it here
String compressionAlgorithm = s.readUTF();
long compressedLength = s.readLong();
long originalLength = s.readLong();
long numberOfElements = s.readLong();
byte[] temp = new byte[(int) compressedLength];
for (int i = 0; i < compressedLength; i++) {
temp[i] = s.readByte();
}
try (Pointer pointer = new BytePointer(temp)) {
CompressionDescriptor descriptor = new CompressionDescriptor();
descriptor.setCompressedLength(compressedLength);
descriptor.setCompressionAlgorithm(compressionAlgorithm);
descriptor.setOriginalLength(originalLength);
descriptor.setNumberOfElements(numberOfElements);
CompressedDataBuffer compressedBuffer = new CompressedDataBuffer(pointer, descriptor);
return Nd4j.getCompressor().decompress(compressedBuffer);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Aggregations