Example usage for java.io InputStream skip

List of usage examples for java.io InputStream skip

Introduction

In this page you can find the example usage for java.io InputStream skip.

Prototype

public long skip(long n) throws IOException 

Source Link

Document

Skips over and discards n bytes of data from this input stream.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    int i;//w w w.  j a v  a  2 s.  c  o  m

    InputStream is = new FileInputStream("C://test.txt");

    while ((i = is.read()) != -1) {
        // converts int to char
        char c = (char) i;

        System.out.println("Character Read: " + c);

        // skip one byte
        is.skip(1);
    }
    is.close();
}

From source file:org.cloudata.core.client.blob.NBlobInputStream.java

public static void main(String[] args) throws IOException {
    if (args.length < 3) {
        System.out.println("Usage: java NBlobInputStream <path> <offset> <length>");
        System.exit(0);/*  w w  w . j  a v a2  s.  c om*/
    }
    //    NBlobInputStream in = new NBlobInputStream(new NConfiguration(),
    //        args[0], Long.parseLong(args[1]), Long.parseLong(args[2]));

    FileSystem fs = FileSystem.get(new Configuration());
    InputStream in = fs.open(new Path(args[0]));
    in.skip(Long.parseLong(args[1]));

    byte[] buf = new byte[1024];

    long totalRead = 0;
    int readBytes = 0;

    while ((readBytes = in.read(buf)) > 0) {
        totalRead += readBytes;
        if (readBytes < buf.length) {
            System.out.println("readBytes:" + readBytes + "," + totalRead);
        }
    }
    in.close();
    System.out.println("Read:" + totalRead + " bytes");
}

From source file:Main.java

public static void skipAll(InputStream in) throws IOException {
    do {/* w ww . j av a 2  s .c  om*/
        in.skip(Long.MAX_VALUE);
    } while (in.read() != -1);
}

From source file:Main.java

public static void skip(InputStream stream, int numBytes) throws IOException {
    numBytes -= stream.skip(numBytes);
    for (; numBytes > 0; --numBytes) {
        if (stream.read() == -1) {
            throw new IOException("Unexpected end of stream");
        }//from w w  w.  j  ava  2  s .  c om
    }
}

From source file:Main.java

public static void skipFully(InputStream in, long n) throws IOException {
    while (n > 0) {
        long count = in.skip(n);
        if (count <= 0)
            throw new EOFException();
        n -= count;/*from w w  w . ja  v  a 2 s  .co  m*/
    }
}

From source file:Main.java

public static void skipForSure(InputStream is, long len) throws IOException {
    long leftToSkip = len;
    while (leftToSkip > 0) {
        long skiped = is.skip(leftToSkip);
        leftToSkip -= skiped;//from w w w . ja  va  2  s.c  o m
    }
}

From source file:Main.java

public static long skip(InputStream in, long len) throws IOException {
    if (len <= 0) {
        return 0;
    }// w w  w .ja  v a  2  s  .c o  m
    long r;
    long t = 0;
    while ((r = in.skip(len - t)) != 0) {
        t += r;
        if (t == len) {
            break;
        }
    }
    return t;
}

From source file:Main.java

public static Bitmap loadMpoBitmapFromFile(File file, long offset, int maxWidth, int maxHeight)
        throws IOException {
    // First, decode the width and height of the image, so that we know how much to scale
    // it down when loading it into our ImageView (so we don't need to do a huge allocation).
    BitmapFactory.Options opts = new BitmapFactory.Options();
    InputStream fs = null;
    try {//from  www  .  j  a  v a 2  s .  co m
        fs = new BufferedInputStream(new FileInputStream(file));
        fs.skip(offset);
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(fs, null, opts);
    } finally {
        if (fs != null) {
            try {
                fs.close();
            } catch (IOException e) {
                // don't worry
            }
        }
    }
    int scale = 1;
    if (opts.outHeight > maxHeight || opts.outWidth > maxWidth) {
        scale = (int) Math.pow(2, (int) Math
                .round(Math.log(maxWidth / (double) Math.max(opts.outHeight, opts.outWidth)) / Math.log(0.5)));
    }
    if ((opts.outHeight <= 0) || (opts.outWidth <= 0)) {
        return null;
    }
    // Decode the image for real, but now with a sampleSize.
    // We have to reopen the file stream, and re-skip to the correct offset, since
    // FileInputStream doesn't support reset().
    Bitmap bmp = null;
    fs = null;
    try {
        fs = new BufferedInputStream(new FileInputStream(file));
        fs.skip(offset);
        BitmapFactory.Options opts2 = new BitmapFactory.Options();
        opts2.inSampleSize = scale;
        bmp = BitmapFactory.decodeStream(fs, null, opts2);
    } finally {
        if (fs != null) {
            try {
                fs.close();
            } catch (IOException e) {
                // don't worry
            }
        }
    }
    return bmp;
}

From source file:cn.im47.cloud.storage.utilities.RangingResourceHttpRequestHandler.java

public static int copyStream(InputStream in, OutputStream out, int start, int end) throws IOException {
    long skipped = in.skip((long) start);
    if (start != skipped) {
        throw new IOException("copyStream failed range start " + start);
    }/*from ww  w.  j  a va  2s. c  o m*/
    byte[] buf = new byte[1000];
    int pos = start - 1;
    int count = 0;
    int l = 1;
    while (l > 0) {
        l = in.read(buf);
        if (l > 0) {
            pos = pos + l;
            if (pos > end) {
                l = l - (pos - end);
                out.write(buf, 0, l);
                count += l;
                break;
            }
            out.write(buf, 0, l);
            count += l;
        }
    }
    return count;
}

From source file:org.openamf.util.XMLUtils.java

/**
 * Reads XML Document from input stream/*from w ww. j av  a 2s  .  co m*/
 * 
 * @param is
 * @return Document
 * @throws IOException
 */
public static Document convertToDOM(InputStream is) throws IOException {
    Document document = null;
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    is.skip(4); // skip length
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.parse(new InputSource(is));
    } catch (Exception e) {
        log.error(e, e);
        throw new IOException("Error while parsing xml: " + e.getMessage());
    }
    return document;
}