Example usage for java.io InputStream reset

List of usage examples for java.io InputStream reset

Introduction

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

Prototype

public synchronized void reset() throws IOException 

Source Link

Document

Repositions this stream to the position at the time the mark method was last called on this input stream.

Usage

From source file:org.infoscoop.request.filter.CalendarFilter.java

private static boolean isCalDAV(InputStream is) throws IOException {
    is.mark(1);//from   w  w  w.j  a  v  a 2  s . c o m
    byte[] xmldec = new byte[500];
    is.read(xmldec);

    String xmlDecStr = new String(xmldec);
    boolean isCalDAV = false;
    if (xmlDecStr.indexOf("multistatus") > 0) {
        isCalDAV = true;
    }
    is.reset();
    if (log.isDebugEnabled()) {
        log.debug(isCalDAV ? "Process caldav." : "Process feed.");
    }

    return isCalDAV;
}

From source file:Main.java

/**
 * WARNING: if the <param>inStream</param> instance does not support mark/reset, when this method returns,
 * subsequent reads on <param>inStream</param> will be 256 bytes into the stream. This may not be the expected
 * behavior. FileInputStreams are an example of an InputStream that does not support mark/reset. InputStreams
 * that do support mark/reset will be reset to the beginning of the stream when this method returns.
 * //from ww  w  . j ava  2  s.  c o m
 * @param inStream
 * @return
 * @throws IOException
 */
public static String readEncodingProcessingInstruction(final InputStream inStream) throws IOException {
    final int BUFF_SZ = 256;
    if (inStream.markSupported()) {
        inStream.mark(BUFF_SZ + 1); // BUFF_SZ+1 forces mark to NOT be forgotten
    }
    byte[] buf = new byte[BUFF_SZ];
    int totalBytesRead = 0;
    int bytesRead;
    do {
        bytesRead = inStream.read(buf, totalBytesRead, BUFF_SZ - totalBytesRead);
        if (bytesRead == -1) {
            break;
        }
        totalBytesRead += bytesRead;
    } while (totalBytesRead < BUFF_SZ);

    if (inStream.markSupported()) {
        inStream.reset();
    }

    return new String(buf);
}

From source file:Main.java

public static Bitmap getImageBitmapFromAssetsFolderThroughImagePathName(Context context, String imagePathName,
        int reqWidth, int reqHeight) {
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inJustDecodeBounds = true;//from w ww  . ja v  a 2  s. com
    Bitmap bitmap = null;
    AssetManager assetManager = context.getResources().getAssets();
    InputStream inputStream = null;

    try {
        inputStream = assetManager.open(imagePathName);
        inputStream.mark(Integer.MAX_VALUE);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        System.gc();
        return null;
    }

    try {
        if (inputStream != null) {
            BitmapFactory.decodeStream(inputStream, null, opts);
            inputStream.reset();
        } else {
            return null;
        }
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        System.gc();
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    opts.inSampleSize = calculateInSampleSiez(opts, reqWidth, reqHeight);
    //         Log.d(TAG,""+opts.inSampleSize);
    opts.inJustDecodeBounds = false;
    opts.inPreferredConfig = Bitmap.Config.RGB_565;
    opts.inPurgeable = true;
    opts.inInputShareable = true;
    opts.inDither = false;
    opts.inTempStorage = new byte[512 * 1024];
    try {
        if (inputStream != null) {
            bitmap = BitmapFactory.decodeStream(inputStream, null, opts);
        } else {
            return null;
        }
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        System.gc();
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }
    //Log.d(TAG,"w:"+bitmap.getWidth()+" h:"+bitmap.getHeight());
    if (bitmap != null) {
        try {
            bitmap = Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true);
        } catch (OutOfMemoryError outOfMemoryError) {
            outOfMemoryError.printStackTrace();
            System.gc();
            return null;
        }
    }
    return bitmap;
}

From source file:org.apache.nifi.controller.TemplateManager.java

private static boolean isMoreData(final InputStream in) throws IOException {
    in.mark(1);//  w w w .  ja  v a  2 s.  c  o  m
    final int nextByte = in.read();
    if (nextByte == -1) {
        return false;
    }

    in.reset();
    return true;
}

From source file:com.smartsheet.api.internal.util.StreamUtil.java

/**
 * used when you want to clone a InputStream's content and still have it appear "rewound" to the stream beginning
 * @param source       the stream around the contents we want to clone
 * @param readbackSize the farthest we should read a resetable stream before giving up
 * @param target       an output stream into which we place a copy of the content read from source
 * @return the source if it was resetable; a new stream rewound around the source data otherwise
 * @throws IOException if any issues occur with the reading of bytes from the source stream
 *//*  w  w w.j ava  2s  .  c om*/
public static InputStream cloneContent(InputStream source, int readbackSize, ByteArrayOutputStream target)
        throws IOException {
    if (source == null) {
        return null;
    }
    // if the source supports mark/reset then we read and then reset up to the read-back size
    if (source.markSupported()) {
        readbackSize = Math.max(TEN_KB, readbackSize); // at least 10 KB (minimal waste, handles those -1 ContentLength cases)
        source.mark(readbackSize);
        copyContentIntoOutputStream(source, target, readbackSize, false);
        source.reset();
        return source;
    } else {
        copyContentIntoOutputStream(source, target, ONE_MB, true);
        byte[] fullContentBytes = target.toByteArray();
        // if we can't reset the source we need to create a replacement stream so others can read the content
        return new ByteArrayInputStream(fullContentBytes);
    }
}

From source file:com.amazonaws.services.s3.internal.AWSS3V4Signer.java

/**
 * Read the content of the request to get the length of the stream.
 * This method will wrap the stream by RepeatableInputStream if it is 
 * not mark-supported.//  w ww.  j  ava 2s.  com
 */
private static long getContentLength(Request<?> request) throws IOException {
    InputStream content = request.getContent();
    if (!content.markSupported()) {
        int streamBufferSize = Constants.getStreamBufferSize();
        content = new RepeatableInputStream(content, streamBufferSize);
        request.setContent(content);
    }

    long contentLength = 0;
    byte[] tmp = new byte[4096];
    int read;
    try {
        content.mark(-1);
        while ((read = content.read(tmp)) != -1) {
            contentLength += read;
        }
        content.reset();
    } finally {
        content.close();
    }

    return contentLength;
}

From source file:com.zotoh.core.io.StreamUte.java

/**
 * Calls InputStream.reset().//from   w  w w .  j ava  2s  . c  om
 * 
 * @param inp
 */
public static void safeReset(InputStream inp) {
    try {
        if (inp != null)
            inp.reset();
    } catch (Exception e) {
    }
}

From source file:parquet.compat.test.Utils.java

public static File createTestFile(long largerThanMB) throws IOException {
    File outputFile = new File("target/test/csv/perftest.csv");
    if (outputFile.exists()) {
        return outputFile;
    }/*from   w  w w  . j a  va  2  s  .  c  o  m*/
    File toCopy = new File("../parquet-testdata/tpch/customer.csv");
    FileUtils.copyFile(new File("../parquet-testdata/tpch/customer.schema"),
            new File("target/test/csv/perftest.schema"));

    OutputStream output = null;
    InputStream input = null;

    try {
        output = new BufferedOutputStream(new FileOutputStream(outputFile, true));
        input = new BufferedInputStream(new FileInputStream(toCopy));
        input.mark(Integer.MAX_VALUE);
        while (outputFile.length() <= largerThanMB * 1024 * 1024) {
            //appendFile(output, toCopy);
            IOUtils.copy(input, output);
            input.reset();
        }
    } finally {
        closeQuietly(input);
        closeQuietly(output);
    }

    return outputFile;
}

From source file:org.ut.biolab.medsavant.shared.util.IOUtils.java

/**
 * Checks if an input stream is gzipped.
 *
 * @param in/* w ww . j a va 2  s  .c  o  m*/
 * @return
 */
public static boolean isGZipped(InputStream in) {
    if (!in.markSupported()) {
        in = new BufferedInputStream(in);
    }
    in.mark(2);
    int magic = 0;
    try {
        magic = in.read() & 0xff | ((in.read() << 8) & 0xff00);
        in.reset();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        return false;
    }
    return magic == GZIPInputStream.GZIP_MAGIC;
}

From source file:com.alibaba.simpleimage.util.ImageUtils.java

public static boolean isJPEG(InputStream source) throws IOException {
    InputStream iis = source;

    if (!source.markSupported()) {
        throw new IllegalArgumentException("Input stream must support mark");
    }//from   w w  w  .j a  v  a 2 s .c o m

    iis.mark(30);
    // If the first two bytes are a JPEG SOI marker, it's probably
    // a JPEG file. If they aren't, it definitely isn't a JPEG file.
    try {
        int byte1 = iis.read();
        int byte2 = iis.read();
        if ((byte1 == 0xFF) && (byte2 == 0xD8)) {

            return true;
        }
    } finally {
        iis.reset();
    }

    return false;
}