Example usage for java.io DataInputStream skipBytes

List of usage examples for java.io DataInputStream skipBytes

Introduction

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

Prototype

public final int skipBytes(int n) throws IOException 

Source Link

Document

See the general contract of the skipBytes method of DataInput.

Usage

From source file:Main.java

public static void main(String[] args) throws IOException {
    byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    FileOutputStream fos = new FileOutputStream("c:\\test.txt");
    DataOutputStream dos = new DataOutputStream(fos);
    for (byte j : b) {
        dos.writeByte(j);//from www . j av a 2s .  c o m
    }
    dos.flush();

    InputStream is = new FileInputStream("c:\\test.txt");

    DataInputStream dis = new DataInputStream(is);

    while (dis.available() > 0) {
        int k = dis.read();
        System.out.print(k);
        dis.skipBytes(1);
    }

}

From source file:Main.java

static int readFirmwareFirmware(File fp) {
    try {//from  w  w w .  j  a  v a 2 s . c om
        FileInputStream fis = new FileInputStream(fp);
        DataInputStream dis = new DataInputStream(fis);
        if (dis.skipBytes(2048) != 2048) {
            // Log.v("DistoX", "failed skip");
            return 0; // skip 8 bootloader blocks
        }
        byte[] buf = new byte[64];
        if (dis.read(buf, 0, 64) != 64) {
            // Log.v("DistoX", "failed read");
            return 0;
        }
        for (int k = 0; k < 64; ++k) {
            // Log.v("DistoX", "byte " + k + " " + buf[k] + " sign " + signature[k] );
            if (k == 6 || k == 7 || k == 16 || k == 17)
                continue;
            if (buf[k] != signature[k])
                return 0;
        }
        if (buf[7] == (byte) 0xf8) {
            if (buf[6] == (byte) 0x34) {
                return 21;
            } else if (buf[6] == (byte) 0x3a) {
                return 22;
            }
        } else if (buf[7] == (byte) 0xf9) {
            if (buf[6] == (byte) 0x90) {
                return 23;
            }
        } else if (buf[7] == (byte) 0xfa) {
            if (buf[6] == (byte) 0x0a) {
                return 24;
            }
        }
    } catch (IOException e) {
    }
    return 0;
}

From source file:org.apache.xmlgraphics.image.codec.png.PNGChunk.java

/**
 * Skips the next chunk from the input stream.
 * @param distream the input stream/*from  w  w  w  .  j a  v  a  2 s. com*/
 * @return true if skipping successful, false otherwise
 */
public static boolean skipChunk(DataInputStream distream) {
    try {
        int length = distream.readInt();
        int type = distream.readInt();
        // is this really faster than reading?
        int skipped = distream.skipBytes(length);
        int crc = distream.readInt();
        if (skipped != length) {
            log.warn("Incorrect number of bytes skipped.");
            return false;
        }
        return true;
    } catch (Exception e) {
        log.warn(e.getMessage());
        return false;
    }
}

From source file:FileUtil.java

public static byte[] readFileAsBytes(String path, Integer start, Integer length) {

    byte[] byteData = null;

    try {/*from ww  w.jav a 2s .  c  o  m*/

        File file = new File(path);

        DataInputStream dis;
        dis = new DataInputStream(new FileInputStream(file));

        if (dis.available() > Integer.MAX_VALUE) {
            System.out.println("dis.available() > Integer.MAX_VALUE");
        }

        ByteArrayOutputStream os = new ByteArrayOutputStream(length);
        byte[] bytes = new byte[length];

        dis.skipBytes(start);
        int readBytes = dis.read(bytes, 0, length);
        os.write(bytes, 0, readBytes);

        byteData = os.toByteArray();

        dis.close();
        os.close();

    } catch (Exception e) {
        System.out.println(e);
    }

    return byteData;
}

From source file:r.base.Connections.java

public static byte[] readRawFromFile(@Current Context context, String file, IntVector key) throws IOException {
    if (key.length() != 2) {
        throw new EvalException("bad offset/length argument");
    }//  ww  w .java 2s  .  c  o  m
    int offset = key.getElementAsInt(0);
    int length = key.getElementAsInt(1);

    byte buffer[] = new byte[length];

    DataInputStream in = new DataInputStream(openInput(context, file));
    in.skipBytes(offset);
    in.readFully(buffer);
    in.close();

    return buffer;
}

From source file:ch.unil.genescore.vegas.Snp.java

/** Skip a SNP in the binary file */
public static void skip(DataInputStream is) throws IOException {

    // Skip bytes
    int numSkipped = is.skipBytes(numBytes_);
    assert numSkipped == numBytes_;
}

From source file:org.commoncrawl.service.crawler.CrawlSegmentLog.java

public static int readHeader(DataInputStream reader) throws IOException {
    reader.skipBytes(4);
    return reader.readInt();
}

From source file:org.apache.pdfbox.tools.imageio.TestImageIOUtils.java

/**
 * checks whether the resolution of a BMP image file is as expected.
 *
 * @param filename the name of the BMP file
 * @param expectedResolution the expected resolution
 *
 * @throws IOException if something goes wrong
 *//*from  www. j  a v a2 s . c  o m*/
private void checkBmpResolution(String filename, int expectedResolution)
        throws FileNotFoundException, IOException {
    // BMP format explained here:
    // http://www.javaworld.com/article/2077561/learn-java/java-tip-60--saving-bitmap-files-in-java.html
    // we skip 38 bytes and then read two 4 byte-integers and reverse the bytes
    DataInputStream dis = new DataInputStream(new FileInputStream(new File(filename)));
    int skipped = dis.skipBytes(38);
    assertEquals("Can't skip 38 bytes in image file " + filename, 38, skipped);
    int pixelsPerMeter = Integer.reverseBytes(dis.readInt());
    int actualResolution = (int) Math.round(pixelsPerMeter / 100.0 * 2.54);
    assertEquals("X resolution doesn't match in image file " + filename, expectedResolution, actualResolution);
    pixelsPerMeter = Integer.reverseBytes(dis.readInt());
    actualResolution = (int) Math.round(pixelsPerMeter / 100.0 * 2.54);
    assertEquals("Y resolution doesn't match in image file " + filename, expectedResolution, actualResolution);
    dis.close();
}

From source file:com.serenegiant.media.TLMediaEncoder.java

static TLMediaFrameHeader readHeader(final DataInputStream in, final TLMediaFrameHeader header)
        throws IOException {
    header.size = 0;//ww  w . j av  a  2  s. co  m
    header.sequence = in.readInt();
    header.frameNumber = in.readInt(); // frame number
    header.presentationTimeUs = in.readLong();
    header.size = in.readInt();
    header.flags = in.readInt();
    in.skipBytes(40); // long x 5
    return header;
}

From source file:ffx.xray.CCP4MapFilter.java

/**
 * {@inheritDoc}//w  ww.  j a v a  2 s.com
 */
@Override
public Crystal getCrystal(String filename, CompositeConfiguration properties) {
    int imapdata;
    int sg = -1;
    double cella = -1.0;
    double cellb = -1.0;
    double cellc = -1.0;
    double cellalpha = -1.0;
    double cellbeta = -1.0;
    double cellgamma = -1.0;

    ByteOrder b = ByteOrder.nativeOrder();

    FileInputStream fis;
    DataInputStream dis;

    // first determine byte order of file versus system
    try {
        fis = new FileInputStream(filename);
        dis = new DataInputStream(fis);

        dis.skipBytes(212);
        byte bytes[] = new byte[4];
        dis.read(bytes, 0, 4);
        ByteBuffer bb = ByteBuffer.wrap(bytes);

        imapdata = bb.order(ByteOrder.BIG_ENDIAN).getInt();
        String stampstr = Integer.toHexString(imapdata);
        // System.out.println("stamp: " + stampstr);
        switch (stampstr.charAt(0)) {
        case '1':
        case '3':
            if (b.equals(ByteOrder.LITTLE_ENDIAN)) {
                b = ByteOrder.BIG_ENDIAN;
            }
            break;
        case '4':
            if (b.equals(ByteOrder.BIG_ENDIAN)) {
                b = ByteOrder.LITTLE_ENDIAN;
            }
            break;
        }
        fis.close();
    } catch (Exception e) {
        String message = "Fatal exception reading CCP4 map.\n";
        logger.log(Level.SEVERE, message, e);
        System.exit(-1);
    }

    try {
        fis = new FileInputStream(filename);
        dis = new DataInputStream(fis);

        dis.skipBytes(40);
        byte bytes[] = new byte[80];
        dis.read(bytes, 0, 80);
        ByteBuffer bb = ByteBuffer.wrap(bytes);

        cella = bb.order(b).getFloat();
        cellb = bb.order(b).getFloat();
        cellc = bb.order(b).getFloat();
        cellalpha = bb.order(b).getFloat();
        cellbeta = bb.order(b).getFloat();
        cellgamma = bb.order(b).getFloat();

        for (int i = 0; i < 3; i++) {
            bb.order(b).getInt();
        }
        for (int i = 0; i < 3; i++) {
            bb.order(b).getFloat();
        }

        sg = bb.order(b).getInt();
        fis.close();
    } catch (Exception e) {
        String message = "Fatal exception reading CCP4 map.\n";
        logger.log(Level.SEVERE, message, e);
        System.exit(-1);
    }

    return new Crystal(cella, cellb, cellc, cellalpha, cellbeta, cellgamma, SpaceGroup.spaceGroupNames[sg - 1]);
}