Example usage for java.io Reader reset

List of usage examples for java.io Reader reset

Introduction

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

Prototype

public void reset() throws IOException 

Source Link

Document

Resets the stream.

Usage

From source file:Main.java

public static void main(String[] args) {
    try {//from   w w  w.ja va2s . c  om
        String s = "tutorial from java2s.com";

        Reader reader = new StringReader(s);

        for (int i = 0; i < 5; i++) {
            char c = (char) reader.read();
            System.out.println(c);
        }

        reader.mark(10);

        for (int i = 0; i < 6; i++) {
            char c = (char) reader.read();
            System.out.println(c);
        }

        reader.reset();

        for (int i = 0; i < 6; i++) {
            char c = (char) reader.read();
            System.out.println(c);
        }
        reader.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    String s = "tutorial from java2s.com";

    Reader reader = new StringReader(s);

    try {//from ww  w.  ja  v a  2s .com
        for (int i = 0; i < 5; i++) {
            char c = (char) reader.read();
            System.out.print(c);
        }

        reader.mark(10);

        for (int i = 0; i < 6; i++) {
            char c = (char) reader.read();
            System.out.println(c);
        }

        reader.reset();

        for (int i = 0; i < 6; i++) {
            char c = (char) reader.read();
            System.out.println(c);
        }

        reader.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:de.quist.samy.remocon.RemoteSession.java

private static char[] readCharArray(Reader reader) throws IOException {
    if (reader.markSupported())
        reader.mark(1024);//from w  w  w  .  ja v a  2  s. co  m
    int length = reader.read();
    int delimiter = reader.read();
    if (delimiter != 0) {
        if (reader.markSupported())
            reader.reset();
        throw new IOException("Unsupported reply exception");
    }
    char[] buffer = new char[length];
    reader.read(buffer);
    return buffer;
}

From source file:Main.java

private static Reader getReader(InputStream is) throws IOException {
    Reader reader = new BufferedReader(new InputStreamReader(is));
    char c[] = "<?".toCharArray();
    int pos = 0;/*w  w  w  .  j  a va  2  s.  c o m*/
    reader.mark(LOOKAHEAD);
    while (true) {
        int value = reader.read();

        // Check to see if we hit the end of the stream.
        if (value == -1) {
            throw new IOException("Encounter end of stream before start of XML.");
        } else if (value == c[pos]) {
            pos++;
        } else {
            if (pos > 0) {
                pos = 0;
            }
            reader.mark(LOOKAHEAD);
        }

        if (pos == c.length) {
            // We found the character set we were looking for.
            reader.reset();
            break;
        }
    }

    return reader;
}

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

/**
 * @param iso/*from w w w .ja v  a  2 s. com*/
 */
public static void resetInputSource(InputSource iso) {

    if (iso != null) {
        Reader rdr = iso.getCharacterStream();
        InputStream ism = iso.getByteStream();
        try {
            if (ism != null)
                ism.reset();
        } catch (Exception e) {
        }
        try {
            if (rdr != null)
                rdr.reset();
        } catch (Exception e) {
        }
    }

}

From source file:cn.fintecher.print.web.util.JacksonDecoder.java

@Override
public Object decode(Response response, Type type) throws IOException {
    if (response.status() == 404)
        return Util.emptyValueOf(type);
    if (response.body() == null)
        return null;
    Reader reader = response.body().asReader();
    if (!reader.markSupported()) {
        reader = new BufferedReader(reader, 1);
    }/*from w  w  w  .j a  va2s .  c o m*/
    try {
        // Read the first byte to see if we have any data
        reader.mark(1);
        if (reader.read() == -1) {
            return null; // Eagerly returning null avoids "No content to map due to end-of-input"
        }
        reader.reset();
        return mapper.readValue(reader, mapper.constructType(type));
    } catch (RuntimeJsonMappingException e) {
        if (e.getCause() != null && e.getCause() instanceof IOException) {
            throw IOException.class.cast(e.getCause());
        }
        throw e;
    }
}

From source file:org.wso2.msf4j.client.codec.MSF4JJacksonDecoder.java

@Override
public Object decode(Response response, Type type) throws IOException, FeignException {
    if (response.body() == null) {
        return null;
    }/*from   ww w  .  j a  va  2 s .  com*/
    Reader reader = response.body().asReader();
    if (!reader.markSupported()) {
        reader = new BufferedReader(reader, 1);
    }
    try {
        // Read the first byte to see if we have any data
        reader.mark(1);
        if (reader.read() == -1) {
            return null; // Eagerly returning null avoids "No content to map due to end-of-input"
        }
        reader.reset();
        return mapper.readValue(reader, mapper.constructType(type));
    } catch (RuntimeJsonMappingException e) {
        Throwable cause = e.getCause();
        if (cause != null && cause instanceof IOException) {
            throw IOException.class.cast(cause);
        }
        throw e;
    }
}

From source file:io.instacount.client.decoders.AbstractInstacountDecoder.java

/**
 * Helper method to construct an instance of {@link Reader} for reading the JSON response from Instacount.
 *
 * @param response/*  w  w  w .ja v a  2s  .c o m*/
 * @return
 * @throws IOException
 */
protected Optional<Reader> constructReader(final Response response) throws IOException {
    Preconditions.checkNotNull(response);
    Preconditions.checkNotNull(response.body());

    Reader reader = response.body().asReader();
    if (!reader.markSupported()) {
        reader = new BufferedReader(reader, 1);
    }
    try {
        // Read the first byte to see if we have any data
        reader.mark(1);
        if (reader.read() == -1) {
            // Eagerly returning null avoids "No content to map due to end-of-input"
            return Optional.absent();
        }
        reader.reset();
    } catch (RuntimeJsonMappingException e) {
        if (e.getCause() != null && e.getCause() instanceof IOException) {
            throw IOException.class.cast(e.getCause());
        }
        throw e;
    }

    return Optional.fromNullable(reader);
}

From source file:com.tulskiy.musique.system.configuration.Configuration.java

@SuppressWarnings("unchecked")
@Deprecated//  w  ww . j  a  v  a 2s .c  om
private void convertOldConfiguration(Reader reader) {
    // reader has been used, so try to reposition read point to start
    try {
        reader.reset();
    } catch (IOException e) {
        // just ignore, will try to read from where it is
    }

    loadFromCustomFormat(reader);

    setFile(new File(Application.getInstance().CONFIG_HOME, "config"));
    addProperty(PROPERTY_INFO_VERSION, VERSION);

    Iterator<Entry<String, Object>> entries = map.entrySet().iterator();
    while (entries.hasNext()) {
        Entry<String, Object> entry = entries.next();
        String key = /*"musique." +*/ entry.getKey();
        if (entry.getValue() instanceof List) {
            List values = (List) entry.getValue();
            if (key.equals("albumart.stubs")) {
                AlbumArtConfiguration.setStubs(values);
            } else if (key.equals("fileOperations.patterns")) {
                FileOperationsConfiguration.setPatterns(values);
            } else if (key.equals("hotkeys.list")) {
                HotkeyConfiguration.setHotkeysRaw(values);
            } else if (key.equals("library.folders")) {
                LibraryConfiguration.setFolders(values);
            } else if (key.equals("playlist.columns")) {
                PlaylistConfiguration.setColumnsRaw(values);
            } else if (key.equals("playlist.tabs.bounds")) {
                PlaylistConfiguration.setTabBoundsRaw(values);
            } else if (key.equals("playlists")) {
                PlaylistConfiguration.setPlaylistsRaw(values);
            } else {
                for (Object value : values) {
                    addProperty(key, value);
                }
            }
        } else {
            if (key.equals("wavpack.encoder.hybrid.wvc")) {
                addProperty("encoder.wavpack.hybrid.wvc.enabled", entry.getValue());
            } else if (key.equals("playlist.activePlaylist")) {
                addProperty("playlists.activePlaylist", entry.getValue());
            } else if (key.equals("playlist.cursorFollowsPlayback")) {
                addProperty("playlists.cursorFollowsPlayback", entry.getValue());
            } else if (key.equals("playlist.groupBy")) {
                addProperty("playlists.groupBy", entry.getValue());
            } else if (key.equals("playlist.lastDir")) {
                addProperty("playlists.lastDir", entry.getValue());
            } else if (key.equals("playlist.playbackFollowsCursor")) {
                addProperty("playlists.playbackFollowsCursor", entry.getValue());
            } else if (key.equals("playlist.tabs.hideSingle")) {
                addProperty("playlists.tabs.hideSingle", entry.getValue());
            } else if (key.startsWith("ape.encoder")) {
                addProperty(key.replace("ape.encoder", "encoder.ape"), entry.getValue());
            } else if (key.startsWith("vorbis.encoder")) {
                addProperty(key.replace("vorbis.encoder", "encoder.vorbis"), entry.getValue());
            } else if (key.startsWith("wavpack.encoder")) {
                addProperty(key.replace("wavpack.encoder", "encoder.wavpack"), entry.getValue());
            } else {
                addProperty(key, entry.getValue());
            }
        }
    }

    map.clear();

    try {
        save();
    } catch (ConfigurationException e) {
        logger.severe("Failed to save converted configuration: " + e.getMessage());
    }
}

From source file:com.twentyn.patentScorer.PatentScorer.java

@Override
public void processPatentText(File patentFile, Reader patentTextReader, int patentTextLength)
        throws IOException, ParserConfigurationException, SAXException, TransformerConfigurationException,
        TransformerException, XPathExpressionException {

    System.out.println("Patent text length: " + patentTextLength);
    CharBuffer buff = CharBuffer.allocate(patentTextLength);
    int read = patentTextReader.read(buff);
    System.out.println("Read bytes: " + read);
    patentTextReader.reset();
    String fullContent = new String(buff.array());

    PatentDocument patentDocument = PatentDocument
            .patentDocumentFromXMLStream(new ReaderInputStream(patentTextReader, Charset.forName("utf-8")));
    if (patentDocument == null) {
        LOGGER.info("Found non-patent type document, skipping.");
        return;/*from   w  ww . j  ava2  s.  com*/
    }

    double pr = this.patentModel.ProbabilityOf(fullContent);
    this.outputWriter
            .write(objectMapper.writeValueAsString(new ClassificationResult(patentDocument.getFileId(), pr)));
    this.outputWriter.write(LINE_SEPARATOR);
    System.out.println("Doc " + patentDocument.getFileId() + " has score " + pr);
}