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(char cbuf[]) throws IOException 

Source Link

Document

Reads characters into an array.

Usage

From source file:net.metanotion.json.StreamingParser.java

private Lexeme maybeFalse(final Reader in) throws IOException {
    final char[] cbuf = new char[BUFFER_LEN4];
    final int ct = in.read(cbuf);
    if ((ct == BUFFER_LEN4) && ("alse".equals(new String(cbuf)))) {
        return new Lexeme(Token.BOOL, false);
    } else {/*ww  w .  j av  a 2 s  .  c  o m*/
        throw new ParserException("Expected 'false', instead found: f" + new String(cbuf));
    }
}

From source file:net.metanotion.json.StreamingParser.java

private Lexeme maybeNull(final Reader in) throws IOException {
    final char[] cbuf = new char[BUFFER_LEN3];
    final int ct = in.read(cbuf);
    if ((ct == BUFFER_LEN3) && ("ull".equals(new String(cbuf)))) {
        return NULL;
    } else {//  w ww.ja va2  s . c om
        throw new ParserException("Expected 'null', instead found: n" + new String(cbuf));
    }
}

From source file:cz.lbenda.dataman.rc.DatamanHeadless.java

public void launch(@Nonnull CommandLine cmd) {
    DbConfig dbConfig = DbConfigFactory.getConfiguration(cmd.getOptionValue("c"));
    if (dbConfig == null) {
        LOG.error("Configuration with name " + cmd.getOptionValue("c") + " not exist.");
        System.exit(1);//w w w  .j ava2 s.  c o m
    }

    try {
        List<FileObject> scripts = scripts(cmd);
        List<FileObject> outputs = outputs(cmd);
        if (scripts.size() == 0) {
            LOG.error("The scripts isn't defined");
            System.exit(3);
        } else {
            SQLSExecutorConsumerClass sqlEditorController = new SQLSExecutorConsumerClass(!cmd.hasOption("nos"),
                    format(cmd));
            SQLSExecutor executor = new SQLSExecutor(dbConfig, sqlEditorController, null);
            dbConfig.getConnectionProvider().getConnection();
            int i = 0;
            for (FileObject file : scripts) {
                if (outputs.size() > i) {
                    sqlEditorController.setOutputFile(outputs.get(i));
                } else {
                    sqlEditorController.setOutputFile(null);
                }
                i++;
                try {
                    InputStream is = file.getContent().getInputStream();
                    Reader reader = new InputStreamReader(is);
                    char[] buff = new char[262144];
                    int l;
                    String rest = "";
                    while ((l = reader.read(buff)) != -1) {
                        String str = rest + new String(buff, 0, l);
                        String[] strs = SQLSExecutor.splitSQLS(str);
                        if (strs.length == 1) {
                            rest = str;
                        } else {
                            rest = strs[strs.length - 1];
                            if (!str.endsWith("\n") && rest.endsWith("\n")) {
                                rest = rest.substring(0, rest.length() - 1);
                                rest = str.substring(str.lastIndexOf(rest), str.length());
                            }
                            String[] sqls = new String[strs.length - 1];
                            System.arraycopy(strs, 0, sqls, 0, strs.length - 1);
                            executor.executeBlocking(sqls);
                        }
                    }
                    executor.executeBlocking(SQLSExecutor.splitSQLS(rest));
                } catch (IOException e) {
                    LOG.error("Problem with read script file", e);
                    System.exit(4);
                }
            }
        }
    } catch (FileSystemException e) {
        LOG.error("Problem with read script data", e);
        System.exit(2);
    }
}

From source file:com.plusub.lib.other.LruDiskCache.java

private static String readFully(Reader reader) throws IOException {
    StringWriter writer = null;//from   w w w  . j a  v a  2 s .c  o  m
    try {
        writer = new StringWriter();
        char[] buffer = new char[1024];
        int count;
        while ((count = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, count);
        }
        return writer.toString();
    } finally {
        try {
            reader.close();
            writer.close();
        } catch (Throwable e) {
        }
    }
}

From source file:onl.area51.httpd.action.Response.java

default Response copy(Reader r) throws IOException {
    char b[] = new char[1024];
    int s = r.read(b);
    while (s > -1) {
        write(b, 0, s);/*from  w  w  w .ja  v  a2s .  co m*/
        s = r.read(b);
    }
    return this;
}

From source file:com.microsoft.tfs.core.internal.db.DBStatement.java

private String clobToString(final Clob clob) throws SQLException, IOException {
    final StringWriter writer = new StringWriter();
    final Reader reader = clob.getCharacterStream();
    final char[] buf = new char[2048];
    int len;//w ww . j a  v  a  2 s  .  com
    while ((len = reader.read(buf)) != -1) {
        writer.write(buf, 0, len);
    }

    return writer.toString();
}

From source file:com.icesoft.applications.faces.auctionMonitor.beans.ReadmeBean.java

/**
 * Method to load a default readme file (at readme.html) The file will be
 * read in and converted to a displayable string
 *
 * @return boolean true on successful read
 *///www.  j av a2s . c  o m
private boolean loadDefaultReadmeFile() {
    try {
        Reader readmeReader = new InputStreamReader(this.getClass().getClassLoader()
                .getResourceAsStream("com/icesoft/applications/faces/auctionMonitor/readme.html"));
        StringWriter readmeWriter = new StringWriter();

        char[] buf = new char[2000];
        int len;
        try {
            while ((len = readmeReader.read(buf)) > -1) {
                readmeWriter.write(buf, 0, len);
            }
        } catch (IOException e) {
            if (log.isErrorEnabled()) {
                log.error("Something went wrong while parsing the readme file, likely because of " + e);
            }
        }
        // clean up the stream reader
        readmeReader.close();

        this.readmeText = readmeWriter.toString();

        // clean up the writer
        readmeWriter.close();

        return true;

    } catch (Exception e) {
        if (log.isWarnEnabled()) {
            log.warn("General error while attempting to load the readme file, cause may be " + e);
        }
    }

    return false;
}

From source file:com.freedomotic.helpers.HttpHelper.java

/**
 *
 * @return @throws IOException if the URL format is wrong or if cannot read
 * from source//from  w  w  w .  j  av  a2s  .  c  om
 */
private String doGet(String url, String username, String password) throws IOException {

    Authenticator.setDefault(new MyAuthenticator(username, password));

    DefaultHttpClient client = new DefaultHttpClient(httpParams);
    String decodedUrl;
    HttpGet request = null;
    try {
        decodedUrl = URLDecoder.decode(url, "UTF-8");
        request = new HttpGet(new URL(decodedUrl).toURI());
    } catch (URISyntaxException | MalformedURLException ex) {
        throw new IOException("The URL " + url + "' is not properly formatted: " + ex.getMessage(), ex);
    }
    HttpResponse response = client.execute(request);

    Reader reader = null;
    try {
        reader = new InputStreamReader(response.getEntity().getContent());

        StringBuilder buffer = new StringBuilder();
        int read;
        char[] cbuf = new char[1024];
        while ((read = reader.read(cbuf)) != -1) {
            buffer.append(cbuf, 0, read);
        }
        return buffer.toString();

    } finally {
        IOUtils.closeQuietly(reader);
    }
}

From source file:com.yahoo.glimmer.util.BySubjectRecordTest.java

@Test
public void relationsReaderTest() throws IOException {
    String expecdedRelationsString = RELATION_1_1 + '\t' + RELATION_1_2 + '\t' + RELATION_1_1 + '\t'
            + RELATION_1_2 + '\t';
    Reader relationsReader = record.getRelationsReader();

    // No relations..
    char[] buffer = new char[4096];
    int charsRead = relationsReader.read(buffer);
    assertEquals(-1, charsRead);/*from   w  w  w . j  a  va 2  s  .co m*/
    assertTrue(Arrays.equals(new char[4096], buffer));

    record.setId(55);
    record.setSubject(SUBJECT_1);
    record.addRelation(RELATION_1_1);
    record.addRelation(RELATION_1_2);
    record.addRelation(RELATION_1_1);
    record.addRelation(RELATION_1_2);

    relationsReader = record.getRelationsReader();
    charsRead = relationsReader.read(buffer);
    assertEquals(144, charsRead);
    assertEquals(expecdedRelationsString, new String(buffer, 0, charsRead));

    // Reading with different buffer sizes.
    StringBuilder sb = new StringBuilder();
    charsRead = Integer.MAX_VALUE;
    for (int bufferSize = 1; bufferSize < (expecdedRelationsString.length() + 10); bufferSize++) {
        buffer = new char[bufferSize];

        relationsReader = record.getRelationsReader();
        for (;;) {
            charsRead = relationsReader.read(buffer);
            if (charsRead == -1) {
                break;
            }

            sb.append(buffer, 0, charsRead);

            if (charsRead < bufferSize) {
                break;
            }
        }

        assertEquals(expecdedRelationsString, sb.toString());

        sb.setLength(0);
    }
}

From source file:com.bigdata.gom.RemoteGOMTestCase.java

protected void print(final URL n3) throws IOException {
    if (log.isInfoEnabled()) {
        InputStream in = n3.openConnection().getInputStream();
        Reader reader = new InputStreamReader(in);
        try {//w ww  . jav a2s  . c o  m
            char[] buf = new char[256];
            int rdlen = 0;
            while ((rdlen = reader.read(buf)) > -1) {
                if (rdlen == 256)
                    System.out.print(buf);
                else
                    System.out.print(new String(buf, 0, rdlen));
            }
        } finally {
            reader.close();
        }
    }
}