javax.imageio.stream.MemoryCacheImageOutputStream Java Examples

The following examples show how to use javax.imageio.stream.MemoryCacheImageOutputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IntArrayTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void constructorRead() throws IOException
{
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final MemoryCacheImageOutputStream os = new MemoryCacheImageOutputStream(baos);
    os.writeInt(18);
    os.close();
    baos.close();

    final ByteArrayBitStreamReader in = new ByteArrayBitStreamReader(baos.toByteArray());

    final ArrayWrapper array = factory.create(in, 1, NUM_BITS);

    assertEquals(1, array.length());
    assertEquals(18, array.elementAt(0));
}
 
Example #2
Source File: ImageHelper.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * Can change this to choose a better compression level as the default
 * 
 * @param image
 * @param scaledImage
 * @return
 */
private static boolean writeTo(BufferedImage image, OutputStream scaledImage, Size scaledSize, String outputFormat) {
    try {
        if (!StringHelper.containsNonWhitespace(outputFormat)) {
            outputFormat = OUTPUT_FORMAT;
        }

        Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(outputFormat);
        if (writers.hasNext()) {
            ImageWriter writer = writers.next();
            ImageWriteParam iwp = getOptimizedImageWriteParam(writer, scaledSize);
            IIOImage iiOImage = new IIOImage(image, null, null);
            ImageOutputStream iOut = new MemoryCacheImageOutputStream(scaledImage);
            writer.setOutput(iOut);
            writer.write(null, iiOImage, iwp);
            writer.dispose();
            iOut.flush();
            iOut.close();
            return true;
        } else {
            return ImageIO.write(image, outputFormat, scaledImage);
        }
    } catch (IOException e) {
        return false;
    }
}
 
Example #3
Source File: JmeDesktopSystem.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void writeImageFile(OutputStream outStream, String format, ByteBuffer imageData, int width, int height) throws IOException {
    BufferedImage awtImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
    Screenshots.convertScreenShot2(imageData.asIntBuffer(), awtImage);

    ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next();
    ImageWriteParam writeParam = writer.getDefaultWriteParam();

    if (format.equals("jpg")) {
        JPEGImageWriteParam jpegParam = (JPEGImageWriteParam) writeParam;
        jpegParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        jpegParam.setCompressionQuality(0.95f);
    }

    awtImage = verticalFlip(awtImage);
    
    ImageOutputStream imgOut = new MemoryCacheImageOutputStream(outStream);
    writer.setOutput(imgOut);
    IIOImage outputImage = new IIOImage(awtImage, null, null);
    try {
        writer.write(null, outputImage, writeParam);
    } finally {
        imgOut.close();
        writer.dispose();
    }
}
 
Example #4
Source File: GImage.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private Icon getIcon(ImageWriter imageWriter) throws IOException {
	GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
	GraphicsDevice gDev = gEnv.getDefaultScreenDevice();
	GraphicsConfiguration gConfig = gDev.getDefaultConfiguration();

	BufferedImage bufferedImage = gConfig.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
	bufferedImage.setRGB(0, 0, width, height, array, 0, width);

	OutputStream out = new ByteArrayOutputStream();
	ImageOutputStream imageOut = new MemoryCacheImageOutputStream(out);

	imageWriter.setOutput(imageOut);

	try {
		imageWriter.write(bufferedImage);
	}
	finally {
		imageOut.close();
	}

	Icon icon = new ImageIcon(bufferedImage);
	return icon;
}
 
Example #5
Source File: MipmapGenerator.java    From render with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a mipmap and writes it to the specified output stream.
 *
 * @param  sourceUrl         URL string for the source image to be down-sampled.
 *
 * @param  mipmapLevelDelta  difference between the mipmap level of the source image and
 *                           the mipmap level for the generated image (should be positive).
 *
 * @param  format            format of the down-sampled image (e.g. {@link Utils#JPEG_FORMAT})
 *
 * @param  jpegQuality       JPEG quality (0.0 <= x <= 1.0, only relevant for JPEG images).
 *
 * @param  outputStream      output stream for the down-sampled image.
 *
 * @throws IllegalArgumentException
 *   if the source URL cannot be loaded or an invalid delta value is specified.
 *
 * @throws IOException
 *   if the down-sampled image cannot be written.
 */
public static void generateMipmap(final String sourceUrl,
                                  final int mipmapLevelDelta,
                                  final String format,
                                  final Float jpegQuality,
                                  final OutputStream outputStream)
        throws IllegalArgumentException, IOException {

    final ImagePlus sourceImagePlus = Utils.openImagePlusUrl(sourceUrl);
    if (sourceImagePlus == null) {
        throw new IllegalArgumentException("failed to load '" + sourceUrl + "' for scaling");
    }

    if (mipmapLevelDelta < 1) {
        throw new IllegalArgumentException("mipmap level delta value (" + mipmapLevelDelta +
                                           ") must be greater than 0");
    }

    final ImageProcessor downSampledProcessor =
            Downsampler.downsampleImageProcessor(sourceImagePlus.getProcessor(),
                                                 mipmapLevelDelta);
    final BufferedImage downSampledImage = downSampledProcessor.getBufferedImage();
    final ImageOutputStream imageOutputStream = new MemoryCacheImageOutputStream(outputStream);

    Utils.writeImage(downSampledImage, format, false, jpegQuality, imageOutputStream);
}
 
Example #6
Source File: GifWriter.java    From scrimage with Apache License 2.0 6 votes vote down vote up
@Override
    public void write(AwtImage image, ImageMetadata metadata, OutputStream out) throws IOException {

        javax.imageio.ImageWriter writer = ImageIO.getImageWritersByFormatName("gif").next();
        ImageWriteParam params = writer.getDefaultWriteParam();

        if (progressive) {
            params.setProgressiveMode(ImageWriteParam.MODE_DEFAULT);
        } else {
            params.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
        }

        try (MemoryCacheImageOutputStream output = new MemoryCacheImageOutputStream(out)) {
            writer.setOutput(output);
            writer.write(null, new IIOImage(image.awt(), null, null), params);
            writer.dispose();
        }

//        IOUtils.closeQuietly(out);
    }
 
Example #7
Source File: ShortArrayTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void constructorRead() throws IOException
{
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final MemoryCacheImageOutputStream os = new MemoryCacheImageOutputStream(baos);
    os.writeShort(18);
    os.close();
    baos.close();

    final ByteArrayBitStreamReader in = new ByteArrayBitStreamReader(baos.toByteArray());

    final ArrayWrapper array = factory.create(in, 1, NUM_BITS);

    assertEquals(1, array.length());
    assertEquals(18, array.elementAt(0));
}
 
Example #8
Source File: CommUtils.java    From tools-ocr with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static byte[] imageToBytes(BufferedImage img) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    MemoryCacheImageOutputStream outputStream = new MemoryCacheImageOutputStream(byteArrayOutputStream);
    try {
        Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter writer = (ImageWriter) iter.next();
        ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(IMAGE_QUALITY);
        writer.setOutput(outputStream);
        IIOImage image = new IIOImage(img, null, null);
        writer.write(null, image, iwp);
        writer.dispose();
        byte[] result = byteArrayOutputStream.toByteArray();
        byteArrayOutputStream.close();
        outputStream.close();
        return result;
    } catch (IOException e) {
        StaticLog.error(e);
        return new byte[0];
    }
}
 
Example #9
Source File: LongArrayTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void constructorRead() throws IOException
{
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final MemoryCacheImageOutputStream os = new MemoryCacheImageOutputStream(baos);
    os.writeLong(18);
    os.close();
    baos.close();

    final ByteArrayBitStreamReader in = new ByteArrayBitStreamReader(baos.toByteArray());

    final ArrayWrapper array = factory.create(in, 1, NUM_BITS);

    assertEquals(1, array.length());
    assertEquals(18, array.elementAt(0));
}
 
Example #10
Source File: UnsignedByteArrayTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void constructorRead() throws IOException
{
    final int HIGH_NIBBLE = 1;
    final int LOW_NIBBLE = 2;

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final MemoryCacheImageOutputStream os = new MemoryCacheImageOutputStream(baos);
    os.writeByte((HIGH_NIBBLE << 4) | LOW_NIBBLE);
    os.close();
    baos.close();

    final ByteArrayBitStreamReader in = new ByteArrayBitStreamReader(baos.toByteArray());

    final ArrayWrapper array = factory.create(in, 2, 4);

    assertEquals(2, array.length());
    assertEquals(HIGH_NIBBLE, array.elementAt(0));
    assertEquals(LOW_NIBBLE, array.elementAt(1));
}
 
Example #11
Source File: BufferedImageStreamingOutput.java    From render with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void write(final OutputStream outputStream)
        throws IOException, WebApplicationException {

    LOG.info("write: entry");

    if (Utils.RAW_FORMAT.equals(format)) {
        writeRawImage(targetImage, outputStream);
    } else if (Utils.PNG_FORMAT.equals(format)) {
        writePngImage(targetImage, 6, FilterType.FILTER_PAETH, outputStream);
    } else if (Utils.TIFF_FORMAT.equals(format)) {
        Utils.writeTiffImage(targetImage, outputStream);
    } else {
        final ImageOutputStream imageOutputStream = new MemoryCacheImageOutputStream(outputStream);
        Utils.writeImage(targetImage, format, convertToGray, quality, imageOutputStream);
    }

    LOG.info("write: exit");
}
 
Example #12
Source File: ByteArrayTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void constructorRead() throws IOException
{
    final byte BYTE_VALUE = -10;
    final int NUM_BITS = 8;

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final MemoryCacheImageOutputStream os = new MemoryCacheImageOutputStream(baos);
    os.writeByte(BYTE_VALUE);
    os.close();
    baos.close();

    final ByteArrayBitStreamReader in = new ByteArrayBitStreamReader(baos.toByteArray());

    final ArrayWrapper array = factory.create(in, 1, NUM_BITS);

    assertEquals(1, array.length());
    assertEquals(BYTE_VALUE, array.elementAt(0));
}
 
Example #13
Source File: OutputStreamImageOutputStreamSpi.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public ImageOutputStream createOutputStreamInstance(Object output,
                                                    boolean useCache,
                                                    File cacheDir)
    throws IOException {
    if (output instanceof OutputStream) {
        OutputStream os = (OutputStream)output;
        if (useCache) {
            return new FileCacheImageOutputStream(os, cacheDir);
        } else {
            return new MemoryCacheImageOutputStream(os);
        }
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example #14
Source File: LosslessFactory.java    From sambox with Apache License 2.0 5 votes vote down vote up
private static PDImageXObject createFromGrayImage(BufferedImage image) throws IOException
{
    int height = image.getHeight();
    int width = image.getWidth();
    int[] rgbLineBuffer = new int[width];
    int bpc = image.getColorModel().getPixelSize();
    FastByteArrayOutputStream baos = new FastByteArrayOutputStream(
            ((width * bpc / 8) + (width * bpc % 8 != 0 ? 1 : 0)) * height);
    try (MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(baos))
    {
        for (int y = 0; y < height; ++y)
        {
            for (int pixel : image.getRGB(0, y, width, 1, rgbLineBuffer, 0, width))
            {
                mcios.writeBits(pixel & 0xFF, bpc);
            }

            int bitOffset = mcios.getBitOffset();
            if (bitOffset != 0)
            {
                mcios.writeBits(0, 8 - bitOffset);
            }
        }
        mcios.flush();
    }
    return prepareImageXObject(baos.toByteArray(), image.getWidth(), image.getHeight(), bpc,
            PDDeviceGray.INSTANCE);
}
 
Example #15
Source File: LosslessFactory.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private static PDImageXObject createFromGrayImage(BufferedImage image, PDDocument document)
        throws IOException
{
    int height = image.getHeight();
    int width = image.getWidth();
    int[] rgbLineBuffer = new int[width];
    int bpc = image.getColorModel().getPixelSize();
    ByteArrayOutputStream baos = new ByteArrayOutputStream(((width*bpc/8)+(width*bpc%8 != 0 ? 1:0))*height);
    MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(baos);
    for (int y = 0; y < height; ++y)
    {
        for (int pixel : image.getRGB(0, y, width, 1, rgbLineBuffer, 0, width))
        {
            mcios.writeBits(pixel & 0xFF, bpc);
        }

        int bitOffset = mcios.getBitOffset();
        if (bitOffset != 0)
        {
            mcios.writeBits(0, 8 - bitOffset);
        }
    }
    mcios.flush();
    mcios.close();
    return prepareImageXObject(document, baos.toByteArray(),
            image.getWidth(), image.getHeight(), bpc, PDDeviceGray.INSTANCE);
}
 
Example #16
Source File: OutputStreamImageOutputStreamSpi.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public ImageOutputStream createOutputStreamInstance(Object output,
                                                    boolean useCache,
                                                    File cacheDir)
    throws IOException {
    if (output instanceof OutputStream) {
        OutputStream os = (OutputStream)output;
        if (useCache) {
            return new FileCacheImageOutputStream(os, cacheDir);
        } else {
            return new MemoryCacheImageOutputStream(os);
        }
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example #17
Source File: OutputStreamImageOutputStreamSpi.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public ImageOutputStream createOutputStreamInstance(Object output,
                                                    boolean useCache,
                                                    File cacheDir)
    throws IOException {
    if (output instanceof OutputStream) {
        OutputStream os = (OutputStream)output;
        if (useCache) {
            return new FileCacheImageOutputStream(os, cacheDir);
        } else {
            return new MemoryCacheImageOutputStream(os);
        }
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example #18
Source File: JPEGMovWriter.java    From pumpernickel with MIT License 5 votes vote down vote up
@Override
protected void writeFrame(OutputStream out, BufferedImage image,
		Map<String, Object> settings) throws IOException {
	if (image.getType() == BufferedImage.TYPE_INT_ARGB
			|| image.getType() == BufferedImage.TYPE_INT_ARGB_PRE) {
		if (printWarning == false) {
			printWarning = true;
			System.err
					.println("JPEGMovWriter Warning: a BufferedImage of type TYPE_INT_ARGB may produce unexpected results. The recommended type is TYPE_INT_RGB.");
		}
	}
	float quality;
	if (settings != null
			&& settings.get(PROPERTY_QUALITY) instanceof Number) {
		quality = ((Number) settings.get(PROPERTY_QUALITY)).floatValue();
	} else if (settings != null
			&& settings.get(PROPERTY_QUALITY) instanceof String) {
		quality = Float.parseFloat((String) settings.get(PROPERTY_QUALITY));
	} else {
		quality = defaultQuality;
	}

	MemoryCacheImageOutputStream iOut = new MemoryCacheImageOutputStream(
			out);
	ImageWriter iw = ImageIO.getImageWritersByMIMEType("image/jpeg").next();
	ImageWriteParam iwParam = iw.getDefaultWriteParam();
	iwParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
	iwParam.setCompressionQuality(quality);
	iw.setOutput(iOut);
	IIOImage img = new IIOImage(image, null, null);
	iw.write(null, img, iwParam);
}
 
Example #19
Source File: OutputStreamImageOutputStreamSpi.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public ImageOutputStream createOutputStreamInstance(Object output,
                                                    boolean useCache,
                                                    File cacheDir)
    throws IOException {
    if (output instanceof OutputStream) {
        OutputStream os = (OutputStream)output;
        if (useCache) {
            return new FileCacheImageOutputStream(os, cacheDir);
        } else {
            return new MemoryCacheImageOutputStream(os);
        }
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example #20
Source File: FlushBefore.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    OutputStream ostream = new ByteArrayOutputStream();

    FileCacheImageOutputStream fcios =
        new FileCacheImageOutputStream(ostream, null);
    test(fcios);

    MemoryCacheImageOutputStream mcios =
        new MemoryCacheImageOutputStream(ostream);
    test(mcios);
}
 
Example #21
Source File: OutputStreamImageOutputStreamSpi.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public ImageOutputStream createOutputStreamInstance(Object output,
                                                    boolean useCache,
                                                    File cacheDir)
    throws IOException {
    if (output instanceof OutputStream) {
        OutputStream os = (OutputStream)output;
        if (useCache) {
            return new FileCacheImageOutputStream(os, cacheDir);
        } else {
            return new MemoryCacheImageOutputStream(os);
        }
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example #22
Source File: BufferedImageHttpMessageConverter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private ImageOutputStream createImageOutputStream(OutputStream os) throws IOException {
	if (this.cacheDir != null) {
		return new FileCacheImageOutputStream(os, this.cacheDir);
	}
	else {
		return new MemoryCacheImageOutputStream(os);
	}
}
 
Example #23
Source File: OutputStreamImageOutputStreamSpi.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public ImageOutputStream createOutputStreamInstance(Object output,
                                                    boolean useCache,
                                                    File cacheDir)
    throws IOException {
    if (output instanceof OutputStream) {
        OutputStream os = (OutputStream)output;
        if (useCache) {
            return new FileCacheImageOutputStream(os, cacheDir);
        } else {
            return new MemoryCacheImageOutputStream(os);
        }
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example #24
Source File: CCITTFactory.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Creates a new CCITT group 4 (T6) compressed image XObject from a b/w BufferedImage. This
 * compression technique usually results in smaller images than those produced by {@link LosslessFactory#createFromImage(PDDocument, BufferedImage)
 * }.
 *
 * @param document the document to create the image as part of.
 * @param image the image.
 * @return a new image XObject.
 * @throws IOException if there is an error creating the image.
 * @throws IllegalArgumentException if the BufferedImage is not a b/w image.
 */
public static PDImageXObject createFromImage(PDDocument document, BufferedImage image)
        throws IOException
{
    if (image.getType() != BufferedImage.TYPE_BYTE_BINARY && image.getColorModel().getPixelSize() != 1)
    {
        throw new IllegalArgumentException("Only 1-bit b/w images supported");
    }
    
    int height = image.getHeight();
    int width = image.getWidth();

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(bos);

    for (int y = 0; y < height; ++y)
    {
        for (int x = 0; x < width; ++x)
        {
            // flip bit to avoid having to set /BlackIs1
            mcios.writeBits(~(image.getRGB(x, y) & 1), 1);
        }
        if (mcios.getBitOffset() != 0)
        {
            mcios.writeBits(0, 8 - mcios.getBitOffset());
        }
    }
    mcios.flush();
    mcios.close();

    return prepareImageXObject(document, bos.toByteArray(), width, height, PDDeviceGray.INSTANCE);
}
 
Example #25
Source File: OutputStreamImageOutputStreamSpi.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public ImageOutputStream createOutputStreamInstance(Object output,
                                                    boolean useCache,
                                                    File cacheDir)
    throws IOException {
    if (output instanceof OutputStream) {
        OutputStream os = (OutputStream)output;
        if (useCache) {
            return new FileCacheImageOutputStream(os, cacheDir);
        } else {
            return new MemoryCacheImageOutputStream(os);
        }
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example #26
Source File: BufferedImageHttpMessageConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private ImageOutputStream createImageOutputStream(OutputStream os) throws IOException {
	if (this.cacheDir != null) {
		return new FileCacheImageOutputStream(os, this.cacheDir);
	}
	else {
		return new MemoryCacheImageOutputStream(os);
	}
}
 
Example #27
Source File: OutputStreamImageOutputStreamSpi.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public ImageOutputStream createOutputStreamInstance(Object output,
                                                    boolean useCache,
                                                    File cacheDir)
    throws IOException {
    if (output instanceof OutputStream) {
        OutputStream os = (OutputStream)output;
        if (useCache) {
            return new FileCacheImageOutputStream(os, cacheDir);
        } else {
            return new MemoryCacheImageOutputStream(os);
        }
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example #28
Source File: BufferedImageHttpMessageConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private ImageOutputStream createImageOutputStream(OutputStream os) throws IOException {
	if (this.cacheDir != null) {
		return new FileCacheImageOutputStream(os, this.cacheDir);
	}
	else {
		return new MemoryCacheImageOutputStream(os);
	}
}
 
Example #29
Source File: FlushBefore.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    OutputStream ostream = new ByteArrayOutputStream();

    FileCacheImageOutputStream fcios =
        new FileCacheImageOutputStream(ostream, null);
    test(fcios);

    MemoryCacheImageOutputStream mcios =
        new MemoryCacheImageOutputStream(ostream);
    test(mcios);
}
 
Example #30
Source File: OutputStreamImageOutputStreamSpi.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public ImageOutputStream createOutputStreamInstance(Object output,
                                                    boolean useCache,
                                                    File cacheDir)
    throws IOException {
    if (output instanceof OutputStream) {
        OutputStream os = (OutputStream)output;
        if (useCache) {
            return new FileCacheImageOutputStream(os, cacheDir);
        } else {
            return new MemoryCacheImageOutputStream(os);
        }
    } else {
        throw new IllegalArgumentException();
    }
}