Search in sources :

Example 31 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream in project vcell by virtualcell.

the class MicroscopyXmlproducer method getXML.

/**
 * This methods returns a XML represnetation of a VCImage object.
 * Creation date: (3/1/2001 3:02:37 PM)
 * @param param cbit.image.VCImage
 * @return Element
 * @throws XmlParseException
 */
private static Element getXML(UShortImage param, Xmlproducer vcellXMLProducer, boolean bSaveCompressed) throws XmlParseException {
    Element image = new Element(MicroscopyXMLTags.UShortImageTag);
    // add atributes
    if (param.getName() != null && param.getName().length() > 0) {
        image.setAttribute(XMLTags.NameAttrTag, mangle(param.getName()));
    }
    // Add annotation
    if (param.getDescription() != null && param.getDescription().length() > 0) {
        Element annotationElement = new Element(XMLTags.AnnotationTag);
        annotationElement.setText(mangle(param.getDescription()));
        image.addContent(annotationElement);
    }
    // Add extent
    if (param.getExtent() != null) {
        image.addContent(vcellXMLProducer.getXML(param.getExtent()));
    }
    // Add Origin
    if (param.getOrigin() != null) {
        image.addContent(vcellXMLProducer.getXML(param.getOrigin()));
    }
    final int BYTES_PER_SHORT = 2;
    int aNumX = param.getNumX();
    int aNumY = param.getNumY();
    int aNumZ = param.getNumZ();
    // Add Imagedata subelement
    int UNCOMPRESSED_SIZE_BYTES = aNumX * aNumY * aNumZ * BYTES_PER_SHORT;
    short[] pixelsShort = param.getPixels();
    byte[] compressedPixels = null;
    boolean bForceUncompressed = false;
    while (true) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        OutputStream dos = bos;
        if (bSaveCompressed && !bForceUncompressed) {
            dos = new DeflaterOutputStream(bos);
        }
        ByteBuffer byteBuffer = ByteBuffer.allocate(UNCOMPRESSED_SIZE_BYTES);
        for (int i = 0; i < pixelsShort.length; i++) {
            byteBuffer.putShort(pixelsShort[i]);
        }
        try {
            dos.write(byteBuffer.array());
        } catch (IOException e) {
            e.printStackTrace(System.out);
            throw new XmlParseException("failed to create compressed pixel data");
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }
        compressedPixels = bos.toByteArray();
        if (!bSaveCompressed || bForceUncompressed) {
            if (UNCOMPRESSED_SIZE_BYTES != compressedPixels.length) {
                throw new XmlParseException("failed to create uncompressed pixel data");
            }
        } else if (!bForceUncompressed) {
            if (UNCOMPRESSED_SIZE_BYTES <= compressedPixels.length) {
                // Compression didn't compress
                // Save uncompressed so reader not confused
                bForceUncompressed = true;
                continue;
            }
        }
        break;
    }
    Element imagedata = new Element(XMLTags.ImageDataTag);
    // Get imagedata attributes
    imagedata.setAttribute(XMLTags.XAttrTag, String.valueOf(aNumX));
    imagedata.setAttribute(XMLTags.YAttrTag, String.valueOf(aNumY));
    imagedata.setAttribute(XMLTags.ZAttrTag, String.valueOf(aNumZ));
    imagedata.setAttribute(XMLTags.CompressedSizeTag, String.valueOf(compressedPixels.length));
    // Get imagedata content
    // encode
    imagedata.addContent(org.vcell.util.Hex.toString(compressedPixels));
    // Add imagedata to VCImage element
    image.addContent(imagedata);
    return image;
}
Also used : ProfileDataElement(org.vcell.optimization.ProfileDataElement) Element(org.jdom.Element) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) XmlParseException(cbit.vcell.xml.XmlParseException) ByteBuffer(java.nio.ByteBuffer) XmlParseException(cbit.vcell.xml.XmlParseException) IOException(java.io.IOException)

Example 32 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream in project vcell by virtualcell.

the class VCImageUncompressed method getPixelsCompressed.

/**
 * This method was created in VisualAge.
 * @return byte[]
 */
public byte[] getPixelsCompressed() throws ImageException {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DeflaterOutputStream dos = new DeflaterOutputStream(bos);
        // DeflaterOutputStream dos = new DeflaterOutputStream(bos,new Deflater(5,false));
        dos.write(pixels, 0, pixels.length);
        dos.close();
        return bos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace(System.out);
        throw new ImageException(e.getMessage());
    }
}
Also used : DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 33 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream in project vcell by virtualcell.

the class ByteImage method getPixelsCompressed.

/**
 * This method was created in VisualAge.
 * @return byte[]
 */
public byte[] getPixelsCompressed() throws ImageException {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DeflaterOutputStream dos = new DeflaterOutputStream(bos);
        // DeflaterOutputStream dos = new DeflaterOutputStream(bos,new Deflater(5,false));
        dos.write(pixels, 0, pixels.length);
        dos.close();
        return bos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace(System.out);
        throw new ImageException(e.getMessage());
    }
}
Also used : ImageException(cbit.image.ImageException) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 34 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream in project vcell by virtualcell.

the class BeanUtils method toCompressedSerialized.

public static byte[] toCompressedSerialized(Serializable cacheObj) throws java.io.IOException {
    byte[] bytes = toSerialized(cacheObj);
    long before = System.currentTimeMillis();
    byte[] objData = null;
    java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
    DeflaterOutputStream dos = new DeflaterOutputStream(bos);
    java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(dos);
    oos.writeObject(cacheObj);
    oos.flush();
    dos.close();
    bos.flush();
    objData = bos.toByteArray();
    oos.close();
    bos.close();
    long after = System.currentTimeMillis();
    System.out.println("BeanUtils.toSerialized, t=" + (after - before) + " ms, (" + cacheObj + ") ratio=" + objData.length + "/" + bytes.length);
    return objData;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 35 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream in project vcell by virtualcell.

the class VCellApiClient method toCompressedSerialized.

public static byte[] toCompressedSerialized(Serializable cacheObj) throws java.io.IOException {
    java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
    DeflaterOutputStream dos = new DeflaterOutputStream(bos);
    java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(dos);
    oos.writeObject(cacheObj);
    oos.flush();
    dos.close();
    bos.flush();
    byte[] objData = bos.toByteArray();
    oos.close();
    bos.close();
    return objData;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

DeflaterOutputStream (java.util.zip.DeflaterOutputStream)76 ByteArrayOutputStream (java.io.ByteArrayOutputStream)50 Deflater (java.util.zip.Deflater)25 IOException (java.io.IOException)24 OutputStream (java.io.OutputStream)13 InflaterInputStream (java.util.zip.InflaterInputStream)9 DataOutputStream (java.io.DataOutputStream)8 GZIPOutputStream (java.util.zip.GZIPOutputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 BufferedOutputStream (java.io.BufferedOutputStream)3 EOFException (java.io.EOFException)3 InputStream (java.io.InputStream)3 Test (org.junit.Test)3 ImageException (cbit.image.ImageException)2 DeflateCompressor (com.linkedin.r2.filter.compression.streaming.DeflateCompressor)2 StreamingCompressor (com.linkedin.r2.filter.compression.streaming.StreamingCompressor)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 PipedInputStream (java.io.PipedInputStream)2