Example usage for java.io ByteArrayInputStream skip

List of usage examples for java.io ByteArrayInputStream skip

Introduction

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

Prototype

public synchronized long skip(long n) 

Source Link

Document

Skips n bytes of input from this input stream.

Usage

From source file:Main.java

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

    byte[] buf = { 65, 66, 67, 68, 69 };

    // create new byte array input stream
    ByteArrayInputStream bais = new ByteArrayInputStream(buf);

    int value = 0;

    // read till the end of the stream
    while ((value = bais.read()) != -1) {

        // skip single byte
        bais.skip(1);
        System.out.println(value);
    }/*from ww w.  j  a va2s.  co m*/

}

From source file:edu.vt.middleware.crypt.util.CryptReader.java

/**
 * Attempts to create a Bouncy Castle <code>DERObject</code> from a byte array
 * representing ASN.1 encoded data./*  w w  w .j av a  2  s. c o  m*/
 *
 * @param  data  ASN.1 encoded data as byte array.
 * @param  discardWrapper  In some cases the value of the encoded data may
 * itself be encoded data, where the latter encoded data is desired. Recall
 * ASN.1 data is of the form {TAG, SIZE, DATA}. Set this flag to true to skip
 * the first two bytes, e.g. TAG and SIZE, and treat the remaining bytes as
 * the encoded data.
 *
 * @return  DER object.
 *
 * @throws  IOException  On I/O errors.
 */
public static DERObject readEncodedBytes(final byte[] data, final boolean discardWrapper) throws IOException {
    final ByteArrayInputStream inBytes = new ByteArrayInputStream(data);
    int size = data.length;
    if (discardWrapper) {
        inBytes.skip(2);
        size = data.length - 2;
    }

    final ASN1InputStream in = new ASN1InputStream(inBytes, size);
    try {
        return in.readObject();
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            final Log logger = LogFactory.getLog(CryptReader.class);
            if (logger.isWarnEnabled()) {
                logger.warn("Error closing ASN.1 input stream.", e);
            }
        }
    }
}

From source file:at.jku.rdfstats.hist.builder.HistogramCodec.java

public static String readString(ByteArrayInputStream stream) {
    try {/*from  ww  w . j av  a 2 s. c o  m*/
        int next;
        String s;
        StringBuilder sb = new StringBuilder();
        stream.mark(0);
        InputStreamReader in = new InputStreamReader(stream);

        while (true) {
            next = in.read();
            if (next < 0 || (char) next == END_OF_STRING) {
                s = sb.toString();
                break;
            } else if ((char) next == EMPTY_STRING) {
                s = "";
                break;
            } else
                sb.append((char) next);
        }

        stream.reset();
        stream.skip(s.length() + 1);
        return s;
    } catch (IOException e) {
        throw new RuntimeException("Unexpected error: cannot read String from ByteArrayOutputStream.", e);
    }
}

From source file:android.framework.util.jar.Manifest.java

/**
 * Returns a byte[] containing all the bytes from a ByteArrayInputStream.
 * Where possible, this returns the actual array rather than a copy.
 *//*ww w  . j  av a 2s. co m*/
private static byte[] exposeByteArrayInputStreamBytes(ByteArrayInputStream bais) {
    byte[] buffer;
    synchronized (bais) {
        byte[] buf;
        int pos;
        try {
            buf = (byte[]) BAIS_BUF.get(bais);
            pos = BAIS_POS.getInt(bais);
        } catch (IllegalAccessException iae) {
            throw new AssertionError(iae);
        }
        int available = bais.available();
        if (pos == 0 && buf.length == available) {
            buffer = buf;
        } else {
            buffer = new byte[available];
            System.arraycopy(buf, pos, buffer, 0, available);
        }
        bais.skip(available);
    }
    return buffer;
}

From source file:Decoder.java

private Reader unzipText(byte[] licenseText) throws Exception {
    ByteArrayInputStream in = new ByteArrayInputStream(licenseText);
    in.skip(LICENSE_PREFIX.length);
    InflaterInputStream zipIn = new InflaterInputStream(in, new Inflater());
    try {/*w  w w  .  j  a  v a  2  s . com*/
        return new InputStreamReader(zipIn, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new Exception(e);
    }
}

From source file:com.atlassian.extras.decoder.v2.Version2LicenseDecoder.java

private Reader unzipText(byte[] licenseText) {
    ByteArrayInputStream in = new ByteArrayInputStream(licenseText);
    in.skip(LICENSE_PREFIX.length);//from   ww w .j a v  a2 s  .  co  m
    InflaterInputStream zipIn = new InflaterInputStream(in, new Inflater());
    try {
        return new InputStreamReader(zipIn, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new LicenseException(e);
    }
}

From source file:Version2LicenseDecoder.java

private Reader unzipText(byte[] licenseText) {
    ByteArrayInputStream in = new ByteArrayInputStream(licenseText);
    in.skip((long) LICENSE_PREFIX.length);
    InflaterInputStream zipIn = new InflaterInputStream(in, new Inflater());

    try {//  w w  w  .j av a2 s  . c o m
        return new InputStreamReader(zipIn, "UTF-8");
    } catch (UnsupportedEncodingException var5) {
        throw new LicenseException(var5);
    }
}

From source file:org.apache.oodt.product.handlers.ofsn.MD5GetHandler.java

public byte[] retrieveChunk(String filepath, long offset, int length) throws ProductException {
    try {//from w  ww  .ja  va 2s  .c o  m
        String hash = this.hashData(FileUtils.readFileToByteArray(new File(filepath)));
        byte[] retBytes = new byte[length];
        byte[] hashBytes = hash.getBytes();
        ByteArrayInputStream is = new ByteArrayInputStream(hashBytes);
        is.skip(offset);
        is.read(retBytes, 0, length);
        return retBytes;
    } catch (IOException e) {
        LOG.log(Level.SEVERE, e.getMessage());
        throw new ProductException(
                "Error reading bytes from file: [" + filepath + "] MD5: Message: " + e.getMessage());
    }
}

From source file:org.structr.common.ImageHelper.java

/**
 * Let ImageIO read and write a JPEG image. This should normalize all types of weird
 * image sub formats, e.g. when extracting images from a flash file.
 *
 * Some images can not be read by ImageIO (or the browser) because they
 * have an JPEG EOI and SOI marker at the beginning of the file.
 *
 * This method detects and removes the bytes, so that the image
 * can be read again./*from   w ww.  j  a v a2  s  .  com*/
 *
 * @param original
 * @return normalized image
 */
public static byte[] normalizeJpegImage(final byte[] original) {

    if (original == null) {

        return new byte[] {};
    }

    ByteArrayInputStream in = new ByteArrayInputStream(original);

    // If JPEG image starts with ff d9 ffd8, strip this sequence from the beginning
    // FF D9 = EOI (end of image)
    // FF D8 = SOI (start of image)
    if ((original[0] == (byte) 0xff) && (original[1] == (byte) 0xd9) && (original[2] == (byte) 0xff)
            && (original[3] == (byte) 0xd8)) {

        in.skip(4);
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BufferedImage source;

    try {

        source = ImageIO.read(in);

        // If ImageIO cannot read it, return original
        if (source == null) {

            return original;
        }

        ImageIO.write(source, "jpeg", out);

    } catch (IOException ex) {

        logger.log(Level.SEVERE, null, ex);

        if (in != null) {

            try {

                in.close();

            } catch (IOException ignore) {
            }

        }

    } finally {
    }

    return out.toByteArray();

}

From source file:org.wso2.carbon.analytics.datasource.core.AnalyticsFileSystemTest.java

private void fileReadSeekPosition(String path, int n, int chunk, int... locs) throws IOException {
    byte[] data = generateData(n);
    OutputStream out = this.analyticsFileSystem.createOutput(path);
    out.write(data, 0, data.length);/*from ww  w . ja v  a 2s.  c o m*/
    out.close();
    byte[] din = new byte[chunk];
    ByteArrayInputStream bin = new ByteArrayInputStream(data);
    byte[] din2 = new byte[chunk];
    DataInput in = this.analyticsFileSystem.createInput(path);
    int count, count2;
    for (int i : locs) {
        in.seek(i);
        Assert.assertEquals(in.getPosition(), i);
        count = in.read(din, 0, din.length);
        Assert.assertEquals(in.getPosition(), i + (count < 0 ? 0 : count));
        bin.reset();
        bin.skip(i);
        count2 = bin.read(din2, 0, din2.length);
        Assert.assertEquals(count, count2);
        Assert.assertEquals(din, din2);
    }
}