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:uk.ac.cam.db538.dexter.manifest.BinXmlUtil.java

/**
 * Read the contents of first argument and display applicationClass; write modified version if other args provided.
 * //from  ww  w  .j av a  2  s  .c  o  m
 * The input and output filenames may be the same.
 * 
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {

    if (args.length == 0) {
        System.out.println("Usage: java BinXmlUtil input_file [new_name output_file]");
        return;
    }

    byte[] inputBinXml = FileUtils.readFileToByteArray(new File(args[0]));
    InputStream inputBinXmlStream = new ByteArrayInputStream(inputBinXml);

    System.out.println("App name read as: " + getApplicationClass(inputBinXmlStream));
    inputBinXmlStream.reset();
    System.out.println("App package read as: " + getPackage(inputBinXmlStream));
    //inputBinXmlStream.reset();
    //prettyPrint(inputBinXmlStream);

    if (args.length >= 3) {
        inputBinXmlStream.reset();
        byte[] modified = setApplicationClass(inputBinXmlStream, args[1]);
        InputStream modStream = new ByteArrayInputStream(modified);
        System.out.println("App name changed to: " + getApplicationClass(modStream));
        modStream.reset();
        FileUtils.writeByteArrayToFile(new File(args[2]), modified);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputStream is = new FileInputStream("C://test.txt");

    // read and print characters one by one
    System.out.println("Char : " + (char) is.read());
    System.out.println("Char : " + (char) is.read());
    System.out.println("Char : " + (char) is.read());

    // mark is set on the input stream
    is.mark(0);// w  ww  .  j  av  a  2s .  co  m

    System.out.println("Char : " + (char) is.read());
    System.out.println("Char : " + (char) is.read());

    if (is.markSupported()) {
        // reset invoked if mark() is supported
        is.reset();
        System.out.println("Char : " + (char) is.read());
        System.out.println("Char : " + (char) is.read());
    }
    is.close();
}

From source file:Main.java

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

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

    System.out.println("Characters printed:");
    // create new buffered reader

    // reads and prints BufferedReader
    System.out.println((char) is.read());
    System.out.println((char) is.read());

    // mark invoked at this position
    is.mark(0);/*from   w  w w. java 2  s  . c om*/
    System.out.println("mark() invoked");
    System.out.println((char) is.read());
    System.out.println((char) is.read());

    // reset() repositioned the stream to the mark
    if (is.markSupported()) {
        is.reset();
        System.out.println("reset() invoked");
        System.out.println((char) is.read());
        System.out.println((char) is.read());
    } else {
        System.out.print("InputStream does not support reset()");
    }
    is.close();
}

From source file:com.qualogy.qafe.bind.orm.jibx.JIBXExceptionTranslator.java

/**
 * Method to get the line JIBX complains about in its error message
 * Method resets the given inputstream to get a clear vision
 * @param e// w  w w.jav  a  2 s.c o m
 * @param in
 * @return
 * @throws BindException - when IOException occurs reading inputstream
 */
public static String getLine(JiBXException exception, InputStream in) throws BindException {
    String line = null;

    try {
        in.reset();
    } catch (IOException e) {
        throw new BindException("Getting the original line failed", e);
    }
    if (exception.getMessage() != null) {
        String message = exception.getMessage();

        int start = message.indexOf(JIBX_LINE_INDICATION_START_MARK) + JIBX_LINE_INDICATION_START_MARK.length();
        if (start > -1) {
            int end = message.indexOf(JIBX_LINE_INDICATION_END_MARK, start);

            if (end > -1) {
                String lineNrStr = message.substring(start, end);
                if (NumberUtils.isNumber(lineNrStr)) {
                    int lineNr = Integer.parseInt(lineNrStr);

                    List lines = null;
                    try {
                        lines = IOUtils.readLines(in);
                    } catch (IOException e) {
                        throw new BindException("Getting the original line failed", e);
                    }
                    if (lines != null) {
                        line = (String) (lines.get(lineNr - 1));
                    }
                }
            }
        }
    }
    if (line != null)
        line = StringUtils.trimToEmpty(line);
    return line;
}

From source file:Main.java

static public final boolean isGzipStm(InputStream in) throws IOException {
    boolean ms = in.markSupported();
    if (ms)/*from  ww  w. j  a  v  a  2s.  c o m*/
        in.mark(10);
    int b1 = in.read();
    int b2 = in.read();
    if (ms)
        in.reset();
    return ((b2 << 8 | b1) == GZIPInputStream.GZIP_MAGIC);
}

From source file:IO.FileWriter.java

/**
 * Write a given InputStream to file//from  w w w  . j  a v  a2 s.  c o m
 *
 * @param is InputStream represent a file
 * @param file_path the destination file path
 * @return true if the file was successfully written to the disk
 */
public static boolean SaveInputStreamToFile(InputStream is, String file_path) {
    boolean success = false;
    try {
        File f = new File(file_path);
        is.reset();
        try (FileOutputStream fos = new FileOutputStream(f)) {
            byte[] buf = new byte[4096];
            int bytesRead;
            while ((bytesRead = is.read(buf)) != -1) {
                fos.write(buf, 0, bytesRead);
            }
        }
        success = true;
        is.reset();
    } catch (FileNotFoundException exception) {
        //Cope with filenames in exotic languages like chinees
        String fileName = Files.GetFileNameWithoutExtension(file_path);
        file_path = file_path.replace(fileName, fileName.hashCode() + "");
        success = SaveInputStreamToFile(is, file_path);
    } catch (IOException exception) {
        Console.PrintException("Error save InputStream to file", exception);
    }
    return success;
}

From source file:org.apache.camel.component.exec.ExecResultConverter.java

/**
 * Resets the stream, only if it's a ByteArrayInputStream.
 *///from w  w  w . ja v a 2 s  .co m
private static void resetIfByteArrayInputStream(InputStream stream) {
    if (stream != null && stream instanceof ByteArrayInputStream) {
        try {
            stream.reset();
        } catch (IOException ioe) {
            LOG.error("Unable to reset the stream ", ioe);
        }
    }
}

From source file:org.apache.hadoop.fs.tar.TarFSUtils.java

private static byte[] getFirstFewBytes(InputStream in, int n) throws IOException {
    byte[] b = new byte[n];

    if (in.markSupported()) {
        in.mark(n);/*from  ww w. j  a va  2s.c o  m*/
        int bytesRead = in.read(b);
        in.reset();

        if (bytesRead < n)
            return null;
    } else
        throw new IOException("Mark must be supported");

    return b;
}

From source file:org.apache.axis2.transport.base.BaseUtils.java

/**
 * Create a SOAP envelope using SOAP 1.1 or 1.2 depending on the namespace
 * @param in InputStream for the payload
 * @param namespace the SOAP namespace/*w w w.  j a  va2 s . c  o  m*/
 * @return the SOAP envelope for the correct version
 * @throws javax.xml.stream.XMLStreamException on error
 */
public static SOAPEnvelope getEnvelope(InputStream in, String namespace) throws XMLStreamException {

    try {
        in.reset();
    } catch (IOException ignore) {
    }
    XMLStreamReader xmlreader = StAXUtils.createXMLStreamReader(in, MessageContext.DEFAULT_CHAR_SET_ENCODING);
    StAXBuilder builder = new StAXSOAPModelBuilder(xmlreader, namespace);
    return (SOAPEnvelope) builder.getDocumentElement();
}

From source file:Main.java

/**
 * Determines if the given stream contains XML content. The stream will be
 * buffered and reset if necessary.//from  ww  w  .  j a  v a  2s  .  c  o m
 * 
 * @param stream
 *            The InputStream to read.
 * @return true if the stream contains XML content; false otherwise.
 */
public static boolean isXML(InputStream stream) {
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream, 1024);
    }
    stream.mark(1024);
    byte[] bytes = new byte[1024];
    try {
        try {
            stream.read(bytes);
        } finally {
            stream.reset();
        }
    } catch (IOException iox) {
        throw new RuntimeException("Failed to read or reset stream. " + iox.getMessage());
    }
    try {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader reader = factory.createXMLStreamReader(new ByteArrayInputStream(bytes));
        // If XML, now in START_DOCUMENT state; seek document element.
        reader.nextTag();
    } catch (XMLStreamException xse) {
        return false;
    }
    return true;
}