Search in sources :

Example 16 with SequenceInputStream

use of java.io.SequenceInputStream in project Java-Tutorial by gpcodervn.

the class SequenceInputStreamExample method main.

public static void main(String[] args) throws Exception {
    FileInputStream input1 = null;
    FileInputStream input2 = null;
    SequenceInputStream inst = null;
    try {
        input1 = new FileInputStream("data/test1.txt");
        input2 = new FileInputStream("data/test2.txt");
        inst = new SequenceInputStream(input1, input2);
        int j;
        while ((j = inst.read()) != -1) {
            System.out.print((char) j);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        inst.close();
        input1.close();
        input2.close();
    }
}
Also used : SequenceInputStream(java.io.SequenceInputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 17 with SequenceInputStream

use of java.io.SequenceInputStream in project pdfbox by apache.

the class JBIG2Filter method decode.

@Override
public DecodeResult decode(InputStream encoded, OutputStream decoded, COSDictionary parameters, int index, DecodeOptions options) throws IOException {
    ImageReader reader = findImageReader("JBIG2", "jbig2-imageio is not installed");
    if (reader.getClass().getName().contains("levigo")) {
        logLevigoDonated();
    }
    int bits = parameters.getInt(COSName.BITS_PER_COMPONENT, 1);
    COSDictionary params = getDecodeParams(parameters, index);
    ImageReadParam irp = reader.getDefaultReadParam();
    irp.setSourceSubsampling(options.getSubsamplingX(), options.getSubsamplingY(), options.getSubsamplingOffsetX(), options.getSubsamplingOffsetY());
    irp.setSourceRegion(options.getSourceRegion());
    options.setFilterSubsampled(true);
    InputStream source = encoded;
    if (params != null) {
        COSBase globals = params.getDictionaryObject(COSName.JBIG2_GLOBALS);
        if (globals instanceof COSStream) {
            source = new SequenceInputStream(((COSStream) globals).createInputStream(), encoded);
        }
    }
    try (ImageInputStream iis = ImageIO.createImageInputStream(source)) {
        reader.setInput(iis);
        BufferedImage image;
        try {
            image = reader.read(0, irp);
        } catch (Exception e) {
            // wrap and rethrow any exceptions
            throw new IOException("Could not read JBIG2 image", e);
        }
        // depending on your renderer this might or might be needed
        if (image.getColorModel().getPixelSize() != bits) {
            if (bits != 1) {
                LOG.warn("Attempting to handle a JBIG2 with more than 1-bit depth");
            }
            BufferedImage packedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
            Graphics graphics = packedImage.getGraphics();
            graphics.drawImage(image, 0, 0, null);
            graphics.dispose();
            image = packedImage;
        }
        DataBuffer dBuf = image.getData().getDataBuffer();
        if (dBuf.getDataType() == DataBuffer.TYPE_BYTE) {
            decoded.write(((DataBufferByte) dBuf).getData());
        } else {
            throw new IOException("Unexpected image buffer type");
        }
    } finally {
        reader.dispose();
    }
    return new DecodeResult(parameters);
}
Also used : COSStream(org.apache.pdfbox.cos.COSStream) COSDictionary(org.apache.pdfbox.cos.COSDictionary) SequenceInputStream(java.io.SequenceInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) InputStream(java.io.InputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) Graphics(java.awt.Graphics) ImageReadParam(javax.imageio.ImageReadParam) SequenceInputStream(java.io.SequenceInputStream) COSBase(org.apache.pdfbox.cos.COSBase) ImageReader(javax.imageio.ImageReader) DataBuffer(java.awt.image.DataBuffer)

Example 18 with SequenceInputStream

use of java.io.SequenceInputStream in project fdroidclient by f-droid.

the class ZioEntry method getInputStream.

// Returns an input stream for reading the entry's data.
public InputStream getInputStream(OutputStream monitorStream) throws IOException {
    if (entryOut != null) {
        entryOut.close();
        size = entryOut.getSize();
        data = ((ByteArrayOutputStream) entryOut.getWrappedStream()).toByteArray();
        compressedSize = data.length;
        crc32 = entryOut.getCRC();
        entryOut = null;
        InputStream rawis = new ByteArrayInputStream(data);
        if (compression == 0)
            return rawis;
        else {
            // This extra dummy byte is required by InflaterInputStream when the data doesn't have the header and crc fields (as it is in zip files).
            return new InflaterInputStream(new SequenceInputStream(rawis, new ByteArrayInputStream(new byte[1])), new Inflater(true));
        }
    }
    ZioEntryInputStream dataStream;
    dataStream = new ZioEntryInputStream(this);
    if (monitorStream != null)
        dataStream.setMonitorStream(monitorStream);
    if (compression != 0) {
        // Note: When using nowrap=true with Inflater it is also necessary to provide
        // an extra "dummy" byte as input. This is required by the ZLIB native library
        // in order to support certain optimizations.
        dataStream.setReturnDummyByte(true);
        return new InflaterInputStream(dataStream, new Inflater(true));
    } else
        return dataStream;
}
Also used : SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater)

Example 19 with SequenceInputStream

use of java.io.SequenceInputStream in project quick-media by liuyueyi.

the class PNGImage method parse_IEND_chunk.

private void parse_IEND_chunk(PNGChunk chunk) throws Exception {
    // Store text strings
    int textLen = textKeys.size();
    String[] textArray = new String[2 * textLen];
    for (int i = 0; i < textLen; i++) {
        String key = (String) textKeys.get(i);
        String val = (String) textStrings.get(i);
        textArray[2 * i] = key;
        textArray[2 * i + 1] = val;
        if (emitProperties) {
            String uniqueKey = "text_" + i + ':' + key;
            properties.put(uniqueKey.toLowerCase(), val);
        }
    }
    if (encodeParam != null) {
        encodeParam.setText(textArray);
    }
    // Store compressed text strings
    int ztextLen = ztextKeys.size();
    String[] ztextArray = new String[2 * ztextLen];
    for (int i = 0; i < ztextLen; i++) {
        String key = (String) ztextKeys.get(i);
        String val = (String) ztextStrings.get(i);
        ztextArray[2 * i] = key;
        ztextArray[2 * i + 1] = val;
        if (emitProperties) {
            String uniqueKey = "ztext_" + i + ':' + key;
            properties.put(uniqueKey.toLowerCase(), val);
        }
    }
    if (encodeParam != null) {
        encodeParam.setCompressedText(ztextArray);
    }
    // Parse prior IDAT chunks
    InputStream seqStream = new SequenceInputStream(Collections.enumeration(streamVec));
    InputStream infStream = new InflaterInputStream(seqStream, new Inflater());
    dataStream = new DataInputStream(infStream);
    // Create an empty WritableRaster
    int depth = bitDepth;
    if ((colorType == PNG_COLOR_GRAY) && (bitDepth < 8) && output8BitGray) {
        depth = 8;
    }
    if ((colorType == PNG_COLOR_PALETTE) && expandPalette) {
        depth = 8;
    }
    int bytesPerRow = (outputBands * width * depth + 7) / 8;
    int scanlineStride = (depth == 16) ? (bytesPerRow / 2) : bytesPerRow;
    theTile = createRaster(width, height, outputBands, scanlineStride, depth);
    if (performGammaCorrection && (gammaLut == null)) {
        initGammaLut(bitDepth);
    }
    if ((postProcess == POST_GRAY_LUT) || (postProcess == POST_GRAY_LUT_ADD_TRANS) || (postProcess == POST_GRAY_LUT_ADD_TRANS_EXP)) {
        initGrayLut(bitDepth);
    }
    decodeImage(interlaceMethod == 1);
    sampleModel = theTile.getSampleModel();
    if ((colorType == PNG_COLOR_PALETTE) && !expandPalette) {
        if (outputHasAlphaPalette) {
            colorModel = new IndexColorModel(bitDepth, paletteEntries, redPalette, greenPalette, bluePalette, alphaPalette);
        } else {
            colorModel = new IndexColorModel(bitDepth, paletteEntries, redPalette, greenPalette, bluePalette);
        }
    } else if ((colorType == PNG_COLOR_GRAY) && (bitDepth < 8) && !output8BitGray) {
        byte[] palette = expandBits[bitDepth];
        colorModel = new IndexColorModel(bitDepth, palette.length, palette, palette, palette);
    } else {
        colorModel = createComponentColorModel(sampleModel);
    }
}
Also used : SequenceInputStream(java.io.SequenceInputStream) DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater) DataInputStream(java.io.DataInputStream) Point(java.awt.Point) IndexColorModel(java.awt.image.IndexColorModel)

Example 20 with SequenceInputStream

use of java.io.SequenceInputStream in project quick-media by liuyueyi.

the class PNGRed method parse_IEND_chunk.

private void parse_IEND_chunk(PNGChunk chunk) throws Exception {
    // Store text strings
    int textLen = textKeys.size();
    String[] textArray = new String[2 * textLen];
    for (int i = 0; i < textLen; i++) {
        String key = (String) textKeys.get(i);
        String val = (String) textStrings.get(i);
        textArray[2 * i] = key;
        textArray[2 * i + 1] = val;
        if (emitProperties) {
            String uniqueKey = "text_" + i + ':' + key;
            properties.put(uniqueKey.toLowerCase(), val);
        }
    }
    if (encodeParam != null) {
        encodeParam.setText(textArray);
    }
    // Store compressed text strings
    int ztextLen = ztextKeys.size();
    String[] ztextArray = new String[2 * ztextLen];
    for (int i = 0; i < ztextLen; i++) {
        String key = (String) ztextKeys.get(i);
        String val = (String) ztextStrings.get(i);
        ztextArray[2 * i] = key;
        ztextArray[2 * i + 1] = val;
        if (emitProperties) {
            String uniqueKey = "ztext_" + i + ':' + key;
            properties.put(uniqueKey.toLowerCase(), val);
        }
    }
    if (encodeParam != null) {
        encodeParam.setCompressedText(ztextArray);
    }
    // Parse prior IDAT chunks
    InputStream seqStream = new SequenceInputStream(Collections.enumeration(streamVec));
    InputStream infStream = new InflaterInputStream(seqStream, new Inflater());
    dataStream = new DataInputStream(infStream);
    // Create an empty WritableRaster
    int depth = bitDepth;
    if ((colorType == PNG_COLOR_GRAY) && (bitDepth < 8) && output8BitGray) {
        depth = 8;
    }
    if ((colorType == PNG_COLOR_PALETTE) && expandPalette) {
        depth = 8;
    }
    int width = bounds.width;
    int height = bounds.height;
    int bytesPerRow = (outputBands * width * depth + 7) / 8;
    int scanlineStride = (depth == 16) ? (bytesPerRow / 2) : bytesPerRow;
    theTile = createRaster(width, height, outputBands, scanlineStride, depth);
    if (performGammaCorrection && (gammaLut == null)) {
        initGammaLut(bitDepth);
    }
    if ((postProcess == POST_GRAY_LUT) || (postProcess == POST_GRAY_LUT_ADD_TRANS) || (postProcess == POST_GRAY_LUT_ADD_TRANS_EXP)) {
        initGrayLut(bitDepth);
    }
    decodeImage(interlaceMethod == 1);
    // Free resources associated with compressed data.
    dataStream.close();
    infStream.close();
    seqStream.close();
    streamVec = null;
    SampleModel sm = theTile.getSampleModel();
    ColorModel cm;
    if ((colorType == PNG_COLOR_PALETTE) && !expandPalette) {
        if (outputHasAlphaPalette) {
            cm = new IndexColorModel(bitDepth, paletteEntries, redPalette, greenPalette, bluePalette, alphaPalette);
        } else {
            cm = new IndexColorModel(bitDepth, paletteEntries, redPalette, greenPalette, bluePalette);
        }
    } else if ((colorType == PNG_COLOR_GRAY) && (bitDepth < 8) && !output8BitGray) {
        byte[] palette = expandBits[bitDepth];
        cm = new IndexColorModel(bitDepth, palette.length, palette, palette, palette);
    } else {
        cm = createComponentColorModel(sm);
    }
    init((CachableRed) null, bounds, cm, sm, 0, 0, properties);
}
Also used : SampleModel(java.awt.image.SampleModel) SequenceInputStream(java.io.SequenceInputStream) DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) IndexColorModel(java.awt.image.IndexColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) Inflater(java.util.zip.Inflater) DataInputStream(java.io.DataInputStream) Point(java.awt.Point) IndexColorModel(java.awt.image.IndexColorModel)

Aggregations

SequenceInputStream (java.io.SequenceInputStream)119 InputStream (java.io.InputStream)76 ByteArrayInputStream (java.io.ByteArrayInputStream)65 IOException (java.io.IOException)46 ArrayList (java.util.ArrayList)31 FileInputStream (java.io.FileInputStream)22 BufferedInputStream (java.io.BufferedInputStream)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 Test (org.junit.Test)10 Vector (java.util.Vector)9 lombok.val (lombok.val)9 OutputStream (java.io.OutputStream)8 List (java.util.List)8 FileOutputStream (java.io.FileOutputStream)7 InputStreamReader (java.io.InputStreamReader)7 HashMap (java.util.HashMap)7 File (java.io.File)6 ByteBuffer (java.nio.ByteBuffer)6 Support_ASimpleInputStream (tests.support.Support_ASimpleInputStream)6 Reader (java.io.Reader)5