Example usage for javax.imageio.stream MemoryCacheImageOutputStream close

List of usage examples for javax.imageio.stream MemoryCacheImageOutputStream close

Introduction

In this page you can find the example usage for javax.imageio.stream MemoryCacheImageOutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this MemoryCacheImageOutputStream .

Usage

From source file:ar.com.zauber.common.image.impl.AbstractImage.java

/**
 * Creates a thumbnail/*from www  .ja  va2s .  c o m*/
 * 
 * @param is data source
 * @param os data source
 * @throws IOException if there is a problem reading is
 */
public static void createThumbnail(final InputStream is, final OutputStream os, final int target)
        throws IOException {
    final float compression = 0.85F;
    ImageWriter writer = null;
    MemoryCacheImageOutputStream mos = null;
    Graphics2D graphics2D = null;
    try {
        final BufferedImage bi = ImageIO.read(is);
        final Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("JPG");
        if (!iter.hasNext()) {
            throw new IllegalStateException("can't find JPG subsystem");
        }

        int w = bi.getWidth(), h = bi.getHeight();
        if (w < target && h < target) {
            // nothing to recalculate, ya es chiquita.
        } else {
            if (w > h) {
                h = target * bi.getHeight() / bi.getWidth();
                w = target;
            } else {
                w = target * bi.getWidth() / bi.getHeight();
                h = target;
            }
        }
        // draw original image to thumbnail image object and
        // scale it to the new size on-the-fly
        final BufferedImage thumbImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        graphics2D = thumbImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(bi, 0, 0, w, h, null);

        writer = (ImageWriter) iter.next();
        final ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(compression);
        mos = new MemoryCacheImageOutputStream(os);
        writer.setOutput(mos);
        writer.write(null, new IIOImage(thumbImage, null, null), iwp);
    } finally {
        if (writer != null) {
            writer.dispose();
        }
        if (mos != null) {
            mos.close();
        }
        if (graphics2D != null) {
            graphics2D.dispose();
        }
        is.close();
        os.close();
    }
}

From source file:org.apache.pdfbox.filter.LZWFilter.java

/**
 * {@inheritDoc}/* w w w.  ja v  a2 s  .c  o m*/
 */
@Override
protected void encode(InputStream rawData, OutputStream encoded, COSDictionary parameters) throws IOException {
    List<byte[]> codeTable = createCodeTable();
    int chunk = 9;

    byte[] inputPattern = null;
    final MemoryCacheImageOutputStream out = new MemoryCacheImageOutputStream(encoded);
    out.writeBits(CLEAR_TABLE, chunk);
    int foundCode = -1;
    int r;
    while ((r = rawData.read()) != -1) {
        byte by = (byte) r;
        if (inputPattern == null) {
            inputPattern = new byte[] { by };
            foundCode = by & 0xff;
        } else {
            inputPattern = Arrays.copyOf(inputPattern, inputPattern.length + 1);
            inputPattern[inputPattern.length - 1] = by;
            int newFoundCode = findPatternCode(codeTable, inputPattern);
            if (newFoundCode == -1) {
                // use previous
                chunk = calculateChunk(codeTable.size() - 1, 1);
                out.writeBits(foundCode, chunk);
                // create new table entry
                codeTable.add(inputPattern);

                if (codeTable.size() == 4096) {
                    // code table is full
                    out.writeBits(CLEAR_TABLE, chunk);
                    codeTable = createCodeTable();
                }

                inputPattern = new byte[] { by };
                foundCode = by & 0xff;
            } else {
                foundCode = newFoundCode;
            }
        }
    }
    if (foundCode != -1) {
        chunk = calculateChunk(codeTable.size() - 1, 1);
        out.writeBits(foundCode, chunk);
    }

    // PPDFBOX-1977: the decoder wouldn't know that the encoder would output 
    // an EOD as code, so he would have increased his own code table and 
    // possibly adjusted the chunk. Therefore, the encoder must behave as 
    // if the code table had just grown and thus it must be checked it is
    // needed to adjust the chunk, based on an increased table size parameter
    chunk = calculateChunk(codeTable.size(), 1);

    out.writeBits(EOD, chunk);

    // pad with 0
    out.writeBits(0, 7);

    // must do or file will be empty :-(
    out.flush();
    out.close();
}