Example usage for java.util.zip InflaterInputStream InflaterInputStream

List of usage examples for java.util.zip InflaterInputStream InflaterInputStream

Introduction

In this page you can find the example usage for java.util.zip InflaterInputStream InflaterInputStream.

Prototype

public InflaterInputStream(InputStream in, Inflater inf, int size) 

Source Link

Document

Creates a new input stream with the specified decompressor and buffer size.

Usage

From source file:org.fastcatsearch.ir.document.DocumentReader.java

public Document readDocument(int docNo, boolean[] fieldSelectOption, boolean indexable) throws IOException {
    // if(docNo < baseDocNo) throw new
    // IOException("Request docNo cannot less than baseDocNo! docNo = "+docNo+", baseDocNo = "+baseDocNo);

    // baseDocNo?    .
    // docNo -= baseDocNo;

    DataInput bai = null;// w w  w .  ja  v  a 2 s  .c  o  m

    if (docNo != lastDocNo) {
        long positionOffset = docNo * IOUtil.SIZE_OF_LONG;
        if (positionOffset >= positionLimit) {
            //.
            return null;
        }
        positionInput.seek(positionOffset);
        long pos = positionInput.readLong();
        // find a document block
        docInput.seek(pos);
        int len = docInput.readInt();

        //2014-11-26 ?  working ?   ? ? GC ?? OOM ? ?.
        // Stream  .
        InflaterInputStream decompressInputStream = null;
        inflaterOutput.reset();
        int count = -1;
        try {
            BoundedInputStream boundedInputStream = new BoundedInputStream(docInput, len);
            boundedInputStream.setPropagateClose(false);// docInput  .
            decompressInputStream = new InflaterInputStream(boundedInputStream, new Inflater(), 512);
            while ((count = decompressInputStream.read(workingBuffer)) != -1) {
                inflaterOutput.write(workingBuffer, 0, count);
            }
        } finally {
            decompressInputStream.close();
        }

        BytesRef bytesRef = inflaterOutput.getBytesRef();
        bai = new BytesDataInput(bytesRef.bytes, 0, bytesRef.length);

        lastDocNo = docNo;
        lastBai = bai;
    } else {
        lastBai.reset();
        bai = lastBai;
    }

    Document document = new Document(fields.size());
    for (int i = 0; i < fields.size(); i++) {
        FieldSetting fs = fields.get(i);
        Field f = null;
        boolean hasValue = bai.readBoolean();
        //         logger.debug("read hasValue={}, select={}, fs={} ", hasValue, fieldSelectOption, fs);
        if (hasValue) {
            //1. fieldSelectOption ?  ? ??.
            //2. ? , true? ? ?.
            if (fieldSelectOption == null || (fieldSelectOption != null && fieldSelectOption[i])) {
                f = fs.createEmptyField();
                f.readRawFrom(bai);
            } else {
                bai.skipVIntData();
            }
            //            logger.debug("fill {} >> {}", i, f);
        } else {
            //?  ?   .
            f = fs.createEmptyField();
            //            logger.debug("fill {} >> empty", i);
        }
        if (f != null && indexable) {
            String multiValueDelimiter = fs.getMultiValueDelimiter();
            try {
                f.parseIndexable(multiValueDelimiter);
            } catch (FieldDataParseException e) {
                throw new IOException(e);
            }
        }
        document.set(i, f);
    }

    document.setDocId(docNo + baseDocNo);

    return document;
}

From source file:org.fastcatsearch.ir.document.DocumentWriter.java

public Document readDocument(int docNo) throws IOException, IRException {
    long prevPosPos = positionOutput.position();
    long docPos = -1;
    try {/*from w ww.  j a  v  a2  s  .co m*/
        long positionOffset = ((long) docNo) * IOUtil.SIZE_OF_LONG;
        positionOutput.seek(positionOffset);
        docPos = IOUtil.readLong(positionOutput.getRaf());
    } finally {
        positionOutput.seek(prevPosPos);
    }

    // find a document block
    long prevDocPos = docOutput.position();
    try {
        docOutput.seek(docPos);
        RandomAccessFile raf = docOutput.getRaf();
        int len = IOUtil.readInt(raf);
        long n = raf.getFilePointer();
        InputStream docInput = Channels.newInputStream(docOutput.getRaf().getChannel().position(n));
        //2014-11-26 ?  working ?   ? ? GC ?? OOM ? ?.
        // Stream  .
        InflaterInputStream decompressInputStream = null;
        inflaterOutput.reset();
        int count = -1;
        try {
            BoundedInputStream boundedInputStream = new BoundedInputStream(docInput, len);
            boundedInputStream.setPropagateClose(false);// docInput  .
            decompressInputStream = new InflaterInputStream(boundedInputStream, new Inflater(), 512);
            while ((count = decompressInputStream.read(workingBuffer)) != -1) {
                inflaterOutput.write(workingBuffer, 0, count);
            }
        } finally {
            decompressInputStream.close();
        }
    } finally {
        docOutput.seek(prevDocPos);
    }

    BytesRef bytesRef = inflaterOutput.getBytesRef();
    DataInput bai = new BytesDataInput(bytesRef.bytes, 0, bytesRef.length);

    Document document = new Document(fields.size());
    for (int i = 0; i < fields.size(); i++) {
        FieldSetting fs = fields.get(i);
        Field f = null;
        boolean hasValue = bai.readBoolean();
        if (hasValue) {
            f = fs.createEmptyField();
            f.readRawFrom(bai);
        } else {
            f = fs.createEmptyField();
        }
        if (f != null) {
            String multiValueDelimiter = fs.getMultiValueDelimiter();
            try {
                f.parseIndexable(multiValueDelimiter);
            } catch (FieldDataParseException e) {
                throw new IOException(e);
            }
        }
        document.add(f);
    }
    document.setDocId(docNo);
    return document;
}

From source file:jfs.sync.encryption.JFSEncryptedStream.java

/**
 * /*from   w  w w  .  ja va 2s . c o  m*/
 * @param fis
 * @param expectedLength
 *            length to be expected or -2 if you don't want the check
 * @param cipher
 * @return
 */
public static InputStream createInputStream(InputStream fis, long expectedLength, Cipher cipher) {
    try {
        InputStream in = fis;
        ObjectInputStream ois = new ObjectInputStream(in);
        byte marker = readMarker(ois);
        long l = readLength(ois);
        if (log.isDebugEnabled()) {
            log.debug(
                    "JFSEncryptedStream.createInputStream() length check " + expectedLength + " == " + l + "?");
        } // if
        if (expectedLength != DONT_CHECK_LENGTH) {
            if (l != expectedLength) {
                log.error("JFSEncryptedStream.createInputStream() length check failed");
                return null;
            } // if
        } // if
        if (cipher == null) {
            log.error("JFSEncryptedStream.createInputStream() no cipher for length " + expectedLength);
        } else {
            in = new CipherInputStream(in, cipher);
        } // if
        if (marker == COMPRESSION_DEFLATE) {
            Inflater inflater = new Inflater(true);
            in = new InflaterInputStream(in, inflater, COMPRESSION_BUFFER_SIZE);
        } // if
        if (marker == COMPRESSION_BZIP2) {
            in = new BZip2CompressorInputStream(in);
        } // if
        if (marker == COMPRESSION_LZMA) {
            Decoder decoder = new Decoder();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

            byte[] properties = new byte[5];
            int readBytes = in.read(properties, 0, properties.length);
            boolean result = decoder.SetDecoderProperties(properties);
            if (log.isDebugEnabled()) {
                log.debug("JFSEncryptedStream.createInputStream() readBytes=" + readBytes);
                log.debug("JFSEncryptedStream.createInputStream() result=" + result);
            } // if

            decoder.Code(in, outputStream, l);
            in.close();
            outputStream.close();
            if (log.isDebugEnabled()) {
                log.debug("JFSEncryptedStream.createInputStream() " + outputStream.size());
            } // if
            in = new ByteArrayInputStream(outputStream.toByteArray());
        } // if
        return in;
    } catch (IOException ioe) {
        log.error("JFSEncryptedStream.createInputStream() I/O Exception " + ioe.getLocalizedMessage());
        return null;
    } // try/catch
}