Example usage for java.io Reader read

List of usage examples for java.io Reader read

Introduction

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

Prototype

public int read() throws IOException 

Source Link

Document

Reads a single character.

Usage

From source file:gov.nih.nci.ncicb.tcga.dcc.dam.processors.DataAccessMatrixQueriesMockImpl.java

public void addPathsToSelectedFiles(List<DataFile> selectedFiles) throws DAMQueriesException {
    //boolean ret = true;
    Reader in = null;
    Writer out = null;/* w  w  w  .j a v  a  2 s . com*/
    try {
        for (DataFile fileInfo : selectedFiles) {
            //noinspection IOResourceOpenedButNotSafelyClosed
            in = new BufferedReader(new FileReader(testDownloadFile));
            String fname = dataFilePath + fileInfo.getFileId() + ".idat";
            File f = new File(fname);
            if (f.exists()) {
                f.delete();
            }
            //noinspection IOResourceOpenedButNotSafelyClosed
            out = new BufferedWriter(new FileWriter(fname));
            int c;
            while ((c = in.read()) != -1) {
                out.write(c);
            }
            out.flush();
            out.close();
            in.close();
            System.out.println("wrote file " + fname);
            fileInfo.setPath(fname);
            fname = fname.replace('\\', '/');
            fname = fname.substring(fname.lastIndexOf('/') + 1);
            fileInfo.setFileName(fname);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        //all or nothing
        for (DataFile fileInfo : selectedFiles) {
            fileInfo.setPath(null);
        }
        //ret = false;
        throw new DAMQueriesException(ex);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
    //return ret;
}

From source file:com.streamsets.datacollector.json.JsonObjectReaderImpl.java

public JsonObjectReaderImpl(Reader reader, long initialPosition, Mode mode, Class<?> objectClass,
        ObjectMapper objectMapper) throws IOException {
    this.mode = mode;
    this.objectClass = objectClass;
    this.objectMapper = objectMapper;

    starting = true;//from w ww.  j  a v  a  2  s  .c om
    this.reader = reader;
    if (mode == Mode.MULTIPLE_OBJECTS) {
        if (initialPosition > 0) {
            IOUtils.skipFully(reader, initialPosition);
            posCorrection += initialPosition;
        }
        if (reader.markSupported()) {
            reader.mark(MAX_CHARS_TO_READ_FORWARD);
            int count = 0;
            byte firstByte = -1;
            while (count++ < MAX_CHARS_TO_READ_FORWARD && (firstByte = (byte) reader.read()) != -1
                    && firstByte <= ' ')
                ; // everything less than a space is whitespace
            if (firstByte > ' ') {
                firstNonSpaceChar = firstByte;
            }
            reader.reset();
        }
    }
    jsonParser = getObjectMapper().getFactory().createParser(reader);
    if (mode == Mode.ARRAY_OBJECTS && initialPosition > 0) {
        fastForwardJsonParser(initialPosition);
    }
}

From source file:au.org.ala.delta.directives.DirectiveParser.java

@SuppressWarnings("unchecked")
protected void doParse(Reader reader, C context, ParsingContext pc) throws IOException, DirectiveException {

    StringBuilder currentData = new StringBuilder();
    int prev = ' ';
    pc.setCurrentLine(1);/*from  w  w  w. j a v  a2s .  c o m*/
    StringBuilder line = new StringBuilder();
    StringBuilder directiveText = new StringBuilder();
    pc.setCurrentDirectiveText(directiveText);
    StringBuilder currentWord = new StringBuilder();
    List<String> currentWords = new ArrayList<String>();

    AbstractDirective<C> currentDirective = null;
    boolean foundDirectiveDelimiter = false;

    int ch = reader.read();

    while (ch >= 0) {
        if (ch == DIRECTIVE_DELIMITER && _blank.indexOf(prev) >= 0) {

            // First test to see if this text is delimited...
            if (currentDirective != null && isDelimited(currentDirective, currentData)) {
                currentData.append((char) ch);
            } else {
                // Finish off any existing directive
                if (currentDirective != null) {
                    executeDirective(currentDirective, currentData.toString(), context);
                } else if (!currentWords.isEmpty()) {
                    handleUnrecognizedDirective(context, currentWords);
                }

                directiveText.setLength(0);
                foundDirectiveDelimiter = true;
                // Start a potentially new directive
                currentWords.clear();
                currentWord = new StringBuilder();
                currentData = new StringBuilder();
                currentDirective = null;
                pc.setCurrentDirectiveStartLine(pc.getCurrentLine());
                long offset = pc.getCurrentOffset() - 1;
                pc.setCurrentDirectiveStartOffset(offset < 0 ? 0 : offset);
            }
        } else if (_blank.indexOf(ch) >= 0 && currentDirective == null && foundDirectiveDelimiter) {
            if (currentWord.length() > 0) {
                currentWords.add(currentWord.toString());
                currentData.append((char) ch);
                currentWord = new StringBuilder();
                DirectiveSearchResult result = _registry.findDirective(currentWords);
                if (result.getResultType() == ResultType.Found) {
                    currentData = new StringBuilder();
                    pc.markDirectiveEnd();
                    currentDirective = (AbstractDirective<C>) result.getMatches().get(0);
                }
            }
        } else {
            currentWord.append((char) ch);
            currentData.append((char) ch);
        }
        line.append((char) ch);
        prev = ch;
        ch = reader.read();

        // Handle end of line if the previous character was a newline, or
        // there are no more characters to read
        boolean endOfLine = (prev == '\n' || ch < 0);
        updateParsingContext(pc, line, directiveText, endOfLine);
    }

    if (currentDirective != null) {
        executeDirective(currentDirective, currentData.toString(), context);
    } else {
        if (currentData.length() > 0) {
            processTrailing(currentData, context);
        }
    }

    Logger.log("Finished!");
    context.endCurrentParsingContext();
}

From source file:BayesianAnalyzer.java

private String nextToken(Reader reader) throws java.io.IOException {
    StringBuffer token = new StringBuffer();
    int i;//from ww  w . j  a  v  a2 s .c o  m
    char ch, ch2;
    boolean previousWasDigit = false;
    boolean tokenCharFound = false;

    if (!reader.ready()) {
        return null;
    }

    while ((i = reader.read()) != -1) {

        ch = (char) i;

        if (ch == ':') {
            String tokenString = token.toString() + ':';
            if (tokenString.equals("From:") || tokenString.equals("Return-Path:")
                    || tokenString.equals("Subject:") || tokenString.equals("To:")) {
                return tokenString;
            }
        }

        if (Character.isLetter(ch) || ch == '-' || ch == '$' || ch == '\u20AC' // the EURO symbol
                || ch == '!' || ch == '\'') {
            tokenCharFound = true;
            previousWasDigit = false;
            token.append(ch);
        } else if (Character.isDigit(ch)) {
            tokenCharFound = true;
            previousWasDigit = true;
            token.append(ch);
        } else if (previousWasDigit && (ch == '.' || ch == ',')) {
            reader.mark(1);
            previousWasDigit = false;
            i = reader.read();
            if (i == -1) {
                break;
            }
            ch2 = (char) i;
            if (Character.isDigit(ch2)) {
                tokenCharFound = true;
                previousWasDigit = true;
                token.append(ch);
                token.append(ch2);
            } else {
                reader.reset();
                break;
            }
        } else if (ch == '\r') {
            // cr found, ignore
        } else if (ch == '\n') {
            // eol found
            tokenCharFound = true;
            previousWasDigit = false;
            token.append(ch);
            break;
        } else if (tokenCharFound) {
            break;
        }
    }

    if (tokenCharFound) {
        //          System.out.println("Token read: " + token);
        return token.toString();
    } else {
        return null;
    }
}

From source file:net.sf.jabref.openoffice.OOBibStyle.java

private void readFormatFile(Reader in) throws IOException {

    // First read all the contents of the file:
    StringBuilder sb = new StringBuilder();
    int c;//from  www.  j  ava  2 s.c o m
    while ((c = in.read()) != -1) {
        sb.append((char) c);
    }
    // Break into separate lines:
    String[] lines = sb.toString().split("\n");
    int mode = OOBibStyle.MODE_NONE;

    for (String line1 : lines) {
        String line = line1;
        if (!line.isEmpty() && (line.charAt(line.length() - 1) == '\r')) {
            line = line.substring(0, line.length() - 1);
        }
        // Check for empty line or comment:
        if (line.trim().isEmpty() || (line.charAt(0) == '#')) {
            continue;
        }
        // Check if we should change mode:
        if (line.equals(OOBibStyle.NAME_MARK)) {
            mode = OOBibStyle.MODE_NAME;
            continue;
        } else if (line.equals(OOBibStyle.LAYOUT_MRK)) {
            mode = OOBibStyle.MODE_LAYOUT;
            continue;
        } else if (line.equals(OOBibStyle.PROPERTIES_MARK)) {
            mode = OOBibStyle.MODE_PROPERTIES;
            continue;
        } else if (line.equals(OOBibStyle.CITATION_MARK)) {
            mode = OOBibStyle.MODE_CITATION;
            continue;
        } else if (line.equals(OOBibStyle.JOURNALS_MARK)) {
            mode = OOBibStyle.MODE_JOURNALS;
            continue;
        }

        switch (mode) {
        case MODE_NAME:
            if (!line.trim().isEmpty()) {
                name = line.trim();
            }
            break;
        case MODE_LAYOUT:
            handleStructureLine(line);
            break;
        case MODE_PROPERTIES:
            handlePropertiesLine(line, properties);
            break;
        case MODE_CITATION:
            handlePropertiesLine(line, citProperties);
            break;
        case MODE_JOURNALS:
            handleJournalsLine(line);
            break;
        default:
            break;
        }

    }

    // Set validity boolean based on whether we found anything interesting
    // in the file:
    if (mode != OOBibStyle.MODE_NONE) {
        valid = true;
    }

}

From source file:de.micromata.genome.gwiki.page.gspt.ExtendedTemplate.java

/**
 * Parses the to elements.// w w w  . j  a v a 2s  .  c  om
 *
 * @param reader the reader
 * @return the list
 * @throws IOException Signals that an I/O exception has occurred.
 */
protected List<ParseElement> parseToElements(Reader reader) throws IOException {
    if (!reader.markSupported()) {
        reader = new BufferedReader(reader);
    }
    // StringWriter sw = new StringWriter();
    List<ParseElement> elements = new ArrayList<ParseElement>();
    startScript(elements);
    // boolean start = false;
    int c;
    while ((c = reader.read()) != -1) {
        if (c == '<') {
            reader.mark(1);
            c = reader.read();
            if (c != '%') {
                // sw.write('<');
                elements.add(new ParseElement(Type.ConstString, "<"));
                reader.reset();
            } else {
                reader.mark(1);
                c = reader.read();
                if (c == '=') {
                    groovyExpression(reader, elements);
                } else if (c == '-') {
                    reader.read();
                    groovyComment(reader, elements);
                } else if (c == '#') {
                    groovySection(Type.GlobalCode, reader, elements);
                } else if (c == '!') {
                    groovySection(Type.ClassCode, reader, elements);
                } else {
                    reader.reset();
                    groovySection(Type.Statement, reader, elements);
                }
            }
            continue; // at least '<' is consumed ... read next chars.
        } else if (c == '-') {
            reader.mark(4);
            if (reader.read() == '-' && reader.read() == '%' && reader.read() == '>') {
                /**
                 * @logging
                 * @reason Innerhalb einer GSPT-Datei ist ein Kommentarendesequenz ohne oeffnendes
                 * @action GSPT korrigieren
                 */
                GWikiLog.warn("In gspt --%> comment without open");
            }
            reader.reset();
        }
        if (elements.size() == 0 || elements.get(elements.size() - 1).type != Type.ConstString)
            elements.add(new ParseElement(Type.ConstString, ""));
        elements.get(elements.size() - 1).text.append((char) c);

    }
    return elements;
}

From source file:net.sf.jabref.logic.openoffice.OOBibStyle.java

private void readFormatFile(Reader in) throws IOException {

    // First read all the contents of the file:
    StringBuilder sb = new StringBuilder();
    int c;//from   www.  j a v a 2s  . com
    while ((c = in.read()) != -1) {
        sb.append((char) c);
    }

    // Store a local copy for viewing
    localCopy = sb.toString();

    // Break into separate lines:
    String[] lines = sb.toString().split("\n");
    BibStyleMode mode = BibStyleMode.NONE;

    for (String line1 : lines) {
        String line = line1;
        if (!line.isEmpty() && (line.charAt(line.length() - 1) == '\r')) {
            line = line.substring(0, line.length() - 1);
        }
        // Check for empty line or comment:
        if (line.trim().isEmpty() || (line.charAt(0) == '#')) {
            continue;
        }
        // Check if we should change mode:
        switch (line) {
        case NAME_MARK:
            mode = BibStyleMode.NAME;
            continue;
        case LAYOUT_MRK:
            mode = BibStyleMode.LAYOUT;
            continue;
        case PROPERTIES_MARK:
            mode = BibStyleMode.PROPERTIES;
            continue;
        case CITATION_MARK:
            mode = BibStyleMode.CITATION;
            continue;
        case JOURNALS_MARK:
            mode = BibStyleMode.JOURNALS;
            continue;
        default:
            break;

        }

        switch (mode) {
        case NAME:
            if (!line.trim().isEmpty()) {
                name = line.trim();
            }
            break;
        case LAYOUT:
            handleStructureLine(line);
            break;
        case PROPERTIES:
            handlePropertiesLine(line, properties);
            break;
        case CITATION:
            handlePropertiesLine(line, citProperties);
            break;
        case JOURNALS:
            handleJournalsLine(line);
            break;
        default:
            break;
        }

    }

    // Set validity boolean based on whether we found anything interesting
    // in the file:
    if (mode != BibStyleMode.NONE) {
        valid = true;
    }

}