Example usage for java.io DataInput skipBytes

List of usage examples for java.io DataInput skipBytes

Introduction

In this page you can find the example usage for java.io DataInput skipBytes.

Prototype

int skipBytes(int n) throws IOException;

Source Link

Document

Makes an attempt to skip over n bytes of data from the input stream, discarding the skipped bytes.

Usage

From source file:org.apache.fop.render.pdf.ImageRawJPEGAdapter.java

/** {@inheritDoc} */
public void outputContents(OutputStream out) throws IOException {
    InputStream in = getImage().createInputStream();
    in = ImageUtil.decorateMarkSupported(in);
    try {// w  w  w.  j  a va 2s.c  o  m
        JPEGFile jpeg = new JPEGFile(in);
        DataInput din = jpeg.getDataInput();

        //Copy the whole JPEG file except:
        // - the ICC profile
        //TODO Thumbnails could safely be skipped, too.
        //TODO Metadata (XMP, IPTC, EXIF) could safely be skipped, too.
        while (true) {
            int reclen;
            int segID = jpeg.readMarkerSegment();
            switch (segID) {
            case JPEGConstants.SOI:
                out.write(0xFF);
                out.write(segID);
                break;
            case JPEGConstants.EOI:
            case JPEGConstants.SOS:
                out.write(0xFF);
                out.write(segID);
                IOUtils.copy(in, out); //Just copy the rest!
                return;
            /*
            case JPEGConstants.APP1: //Metadata
            case JPEGConstants.APPD:
            jpeg.skipCurrentMarkerSegment();
            break;*/
            case JPEGConstants.APP2: //ICC (see ICC1V42.pdf)
                boolean skipICCProfile = false;
                in.mark(16);
                try {
                    reclen = jpeg.readSegmentLength();
                    // Check for ICC profile
                    byte[] iccString = new byte[11];
                    din.readFully(iccString);
                    din.skipBytes(1); //string terminator (null byte)

                    if ("ICC_PROFILE".equals(new String(iccString, "US-ASCII"))) {
                        skipICCProfile = (this.image.getICCProfile() != null);
                    }
                } finally {
                    in.reset();
                }
                if (skipICCProfile) {
                    //ICC profile is skipped as it is already embedded as a PDF object
                    jpeg.skipCurrentMarkerSegment();
                    break;
                }
            default:
                out.write(0xFF);
                out.write(segID);

                reclen = jpeg.readSegmentLength();
                //write short
                out.write((reclen >>> 8) & 0xFF);
                out.write((reclen >>> 0) & 0xFF);
                int left = reclen - 2;
                byte[] buf = new byte[2048];
                while (left > 0) {
                    int part = Math.min(buf.length, left);
                    din.readFully(buf, 0, part);
                    out.write(buf, 0, part);
                    left -= part;
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.apache.geode.pdx.internal.PdxInstanceImpl.java

private static PdxInputStream createDis(DataInput in, int len) {
    PdxInputStream dis;//from  w  w w. ja va 2 s.  c  om
    if (in instanceof PdxInputStream) {
        dis = new PdxInputStream((ByteBufferInputStream) in, len);
        try {
            int bytesSkipped = in.skipBytes(len);
            int bytesRemaining = len - bytesSkipped;
            while (bytesRemaining > 0) {
                in.readByte();
                bytesRemaining--;
            }
        } catch (IOException ex) {
            throw new PdxSerializationException("Could not deserialize PDX", ex);
        }
    } else {
        byte[] bytes = new byte[len];
        try {
            in.readFully(bytes);
        } catch (IOException ex) {
            throw new PdxSerializationException("Could not deserialize PDX", ex);
        }
        dis = new PdxInputStream(bytes);
    }
    return dis;
}

From source file:org.cloudata.core.common.io.CWritableUtils.java

/**
 * Skip <i>len</i> number of bytes in input stream<i>in</i>
 * @param in input stream/*from ww  w  .  j av a2 s . c  om*/
 * @param len number of bytes to skip
 * @throws IOException when skipped less number of bytes
 */
public static void skipFully(DataInput in, int len) throws IOException {
    int total = 0;
    int cur = 0;

    while ((total < len) && ((cur = in.skipBytes(len - total)) > 0)) {
        total += cur;
    }

    if (total < len) {
        throw new IOException("Not able to skip " + len + " bytes, possibly " + "due to end of input.");
    }
}

From source file:org.commoncrawl.service.queryserver.index.PositionBasedSequenceFileIndex.java

public void seekReaderToItemAtIndex(SequenceFile.Reader reader, long desiredIndexPos) throws IOException {
    IndexItem indexItem = findIndexDataPosForItemIndex(desiredIndexPos);
    if (indexItem == null) {
        throw new IOException("Invalid Index Position:" + desiredIndexPos);
    }/*ww w  . jav a 2 s  .c o  m*/

    //LOG.info("Seeking to appropriate position in file");
    long timeStart = System.currentTimeMillis();
    reader.seek(indexItem._offsetValue);
    //LOG.info("Seek Took:" + (System.currentTimeMillis() - timeStart));

    DataOutputBuffer skipBuffer = new DataOutputBuffer() {
        @Override
        public void write(DataInput in, int length) throws IOException {
            in.skipBytes(length);
        }
    };

    timeStart = System.currentTimeMillis();

    int skipCount = 0;

    ValueBytes skipValue = reader.createValueBytes();

    long currentIndexPos = indexItem._indexValue;
    while (currentIndexPos < desiredIndexPos) {

        reader.nextRawKey(skipBuffer);
        reader.nextRawValue(skipValue);
        ++skipCount;
        ++currentIndexPos;
    }

    //LOG.info("Skip of:" + skipCount +" Values took:" + (System.currentTimeMillis() - timeStart));

}

From source file:org.kalypso.shape.dbf.DBFField.java

public static IDBFField read(final DataInput input, final Charset charset) throws IOException, DBaseException {
    final byte[] columnNameBytes = new byte[MAX_COLUMN_NAME_LENGTH];
    input.readFully(columnNameBytes);//from ww w  . j  av  a 2 s . co  m

    final int columNameLength = findColumnNameLength(columnNameBytes);

    final String columnName = new String(columnNameBytes, 0, columNameLength, charset);

    final char columnType = (char) input.readByte();

    input.skipBytes(4);

    // get field length and precision
    final short fieldLength = DataUtils.fixByte(input.readByte());
    final short decimalCount = DataUtils.fixByte(input.readByte());

    input.skipBytes(14);

    final FieldType fieldType = FieldType.valueOf("" + columnType);
    return new DBFField(columnName, fieldType, fieldLength, decimalCount);
}

From source file:org.netbeans.nbbuild.VerifyClassLinkageForIISI.java

private static void skip(DataInput input, int bytes) throws IOException {
    int skipped = input.skipBytes(bytes);
    if (skipped != bytes) {
        throw new IOException("Truncated class file");
    }/*from  w  w w  . j  a v a 2  s .  c  o  m*/
}