Example usage for java.io ByteArrayInputStream reset

List of usage examples for java.io ByteArrayInputStream reset

Introduction

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

Prototype

public synchronized void reset() 

Source Link

Document

Resets the buffer to the marked position.

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);

    // print bytes
    System.out.println(bais.read());
    System.out.println(bais.read());
    System.out.println(bais.read());
    System.out.println(bais.read());
    System.out.println(bais.read());

    System.out.println("Reset() invocation");

    // reset() invocation
    bais.reset();
    System.out.println(bais.read());
    System.out.println(bais.read());

}

From source file:MainClass.java

public static void main(String args[]) throws IOException {
    String tmp = "abc";
    byte b[] = tmp.getBytes();
    ByteArrayInputStream in = new ByteArrayInputStream(b);

    for (int i = 0; i < 2; i++) {
        int c;//from  w  ww  .  j  a  v  a 2s  .  c o m
        while ((c = in.read()) != -1) {
            if (i == 0) {
                System.out.print((char) c);
            } else {
                System.out.print(Character.toUpperCase((char) c));
            }
        }
        System.out.println();
        in.reset();
    }
}

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);

    // print bytes
    System.out.println(bais.read());
    System.out.println(bais.read());
    System.out.println(bais.read());

    System.out.println("Mark() invocation");

    // mark() invocation;
    bais.mark(0);//w w w  .j a v a2s .  com
    System.out.println(bais.read());
    System.out.println(bais.read());

    System.out.println("Reset() invocation");

    // reset() invocation
    bais.reset();
    System.out.println(bais.read());
    System.out.println(bais.read());

}

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);

    // test support for mark() and reset() methods invocation
    boolean isMarkSupported = bais.markSupported();
    System.out.println("Is mark supported : " + isMarkSupported);
    System.out.println("Following is the proof:");

    // print bytes
    System.out.println(bais.read());
    System.out.println(bais.read());
    System.out.println(bais.read());

    System.out.println("Mark() invocation");

    // mark() invocation;
    bais.mark(0);/*w  ww  . j  a  va  2  s .c  o  m*/
    System.out.println(bais.read());
    System.out.println(bais.read());

    System.out.println("Reset() invocation");

    // reset() invocation
    bais.reset();
    System.out.println(bais.read());
    System.out.println(bais.read());

}

From source file:com.aeg.ims.ftp.SftpTestUtils.java

public static void createTestFiles(RemoteFileTemplate<LsEntry> template, final String... fileNames) {
    if (template != null) {
        final ByteArrayInputStream stream = new ByteArrayInputStream("foo".getBytes());
        template.execute(new SessionCallback<LsEntry, Void>() {

            @Override/*from  ww w  .  j  a va 2s.  c om*/
            public Void doInSession(Session<LsEntry> session) throws IOException {
                try {
                    session.mkdir("BriansTest");
                } catch (Exception e) {
                    assertThat(e.getMessage(), containsString("failed to create"));
                }
                for (int i = 0; i < fileNames.length; i++) {
                    stream.reset();
                    session.write(stream, "Test" + fileNames[i]);
                }
                return null;
            }
        });
    }
}

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

public static String readString(ByteArrayInputStream stream) {
    try {/*  www. j a va2  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:net.sf.nutchcontentexporter.filter.CreativeCommonsCandidateFilter.java

@Override
public boolean acceptContent(WARCRecordInfo recordInfo) {
    ByteArrayInputStream contentStream = (ByteArrayInputStream) recordInfo.getContentStream();

    try {//from   w w  w .j  av a2 s.  c o  m
        List<String> lines = IOUtils.readLines(contentStream);
        contentStream.reset();

        for (String line : lines) {
            if (line.contains("creativecommons")) {
                return true;
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    return false;
}

From source file:com.github.devnied.emvnfccard.utils.TlvUtil.java

public static String prettyPrintAPDUResponse(final byte[] data, final int indentLength) {
    StringBuilder buf = new StringBuilder();

    ByteArrayInputStream stream = new ByteArrayInputStream(data);

    while (stream.available() > 0) {
        buf.append("\n");
        if (stream.available() == 2) {
            stream.mark(0);//from ww w . j a  v a 2s . c  o  m
            byte[] value = new byte[2];
            try {
                stream.read(value);
            } catch (IOException e) {
            }
            SwEnum sw = SwEnum.getSW(value);
            if (sw != null) {
                buf.append(getSpaces(0));
                buf.append(BytesUtils.bytesToString(value)).append(" -- ");
                buf.append(sw.getDetail());
                continue;
            }
            stream.reset();
        }

        buf.append(getSpaces(indentLength));

        TLV tlv = TlvUtil.getNextTLV(stream);

        byte[] tagBytes = tlv.getTagBytes();
        byte[] lengthBytes = tlv.getRawEncodedLengthBytes();
        byte[] valueBytes = tlv.getValueBytes();

        ITag tag = tlv.getTag();

        buf.append(prettyPrintHex(tagBytes));
        buf.append(" ");
        buf.append(prettyPrintHex(lengthBytes));
        buf.append(" -- ");
        buf.append(tag.getName());

        int extraIndent = (lengthBytes.length + tagBytes.length) * 3;

        if (tag.isConstructed()) {
            // indentLength += extraIndent; //TODO check this
            // Recursion
            buf.append(prettyPrintAPDUResponse(valueBytes, indentLength + extraIndent));
        } else {
            buf.append("\n");
            if (tag.getTagValueType() == TagValueTypeEnum.DOL) {
                buf.append(TlvUtil.getFormattedTagAndLength(valueBytes, indentLength + extraIndent));
            } else {
                buf.append(getSpaces(indentLength + extraIndent));
                buf.append(prettyPrintHex(BytesUtils.bytesToStringNoSpace(valueBytes),
                        indentLength + extraIndent));
                buf.append(" (");
                buf.append(TlvUtil.getTagValueAsString(tag, valueBytes));
                buf.append(")");
            }
        }
    }
    return buf.toString();
}

From source file:com.opengamma.integration.tool.portfolio.xml.XmlFileReader.java

/**
 * Constructs the reader from the supplied location. The following validation
 * steps are performed://from w w w .j  av  a2  s  . c  o  m
 * <ul>
 * <li>Check the file is valid xml and contains a schemaVersion attribute</li>
 * <li>Check that a converter is available for the schema version</li>
 * <li>Check the file validates against the required schema</li>
 * <li>Check the portfolio, position, trade, security details are valid</li>
 * </ul>
 *
 * @param inputStream the input stream of the file, must not be null
 * @param schemaRegister the schema register, use to determine what xml
 * versions can be handled
 */
public XmlFileReader(InputStream inputStream, SchemaRegister schemaRegister) {

    // Create a ByteArrayInputStream to ensure reset() is available
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        org.apache.commons.io.IOUtils.copy(inputStream, baos);
    } catch (IOException e) {
        throw new OpenGammaRuntimeException("Unable to read xml", e);
    }

    byte[] bytes = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);

    SchemaVersion version = extractSchemaVersion(bais);
    PortfolioConversion converter = schemaRegister.getConverterForSchema(version);

    //reset after schema version is extracted
    bais.reset();

    if (converter != null) {
        Iterable<VersionedPortfolioHandler> handlers = converter.convertPortfolio(bais);
        _readers = Iterables.transform(handlers, new Function<VersionedPortfolioHandler, PortfolioReader>() {
            @Override
            public PortfolioReader apply(final VersionedPortfolioHandler vph) {
                return new XmlPortfolioReader(vph);
            }
        });
    } else {
        throw new OpenGammaRuntimeException(
                "Unable to process schema version: " + version + " as no converter is available");
    }
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet.java

/**
 * Returns the contents of the specified input source, ignoring any {@link IOException}s.
 * @param source the input source from which to read
 * @return the contents of the specified input source, or an empty string if an {@link IOException} occurs
 *//*from w w w  . ja v  a 2 s .  c om*/
private static String toString(final InputSource source) {
    try {
        final Reader reader = source.getCharacterStream();
        if (null != reader) {
            // try to reset to produce some output
            if (reader instanceof StringReader) {
                final StringReader sr = (StringReader) reader;
                sr.reset();
            }
            return IOUtils.toString(reader);
        }
        final InputStream is = source.getByteStream();
        if (null != is) {
            // try to reset to produce some output
            if (is instanceof ByteArrayInputStream) {
                final ByteArrayInputStream bis = (ByteArrayInputStream) is;
                bis.reset();
            }
            return IOUtils.toString(is);
        }
        return "";
    } catch (final IOException e) {
        return "";
    }
}