Example usage for java.io BufferedReader lines

List of usage examples for java.io BufferedReader lines

Introduction

In this page you can find the example usage for java.io BufferedReader lines.

Prototype

public Stream<String> lines() 

Source Link

Document

Returns a Stream , the elements of which are lines read from this BufferedReader .

Usage

From source file:org.auscope.portal.server.web.service.TemplateLintService.java

private List<LintResult> pylint(String template) throws PortalServiceException {
    List<LintResult> lints = new ArrayList<LintResult>();

    // Save template as a temporary python file
    Path f;//  www . j av a2 s. c  o m
    try {
        f = Files.createTempFile("contemplate", ".py");
        BufferedWriter writer = Files.newBufferedWriter(f);
        writer.write(template);
        writer.close();
    } catch (Exception ex) {
        throw new PortalServiceException("Failed to write template to temporary file.", ex);
    }

    // Run pylint in the temp file's directory
    String results;
    String errors;
    ArrayList<String> cmd = new ArrayList<String>(this.pylintCommand);
    cmd.add(f.getFileName().toString());
    try {
        ProcessBuilder pb = new ProcessBuilder(cmd).directory(f.getParent().toFile());

        // Start the process, and consume the results immediately so Windows is happy.
        Process p = pb.start();
        BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
        results = stdout.lines().collect(Collectors.joining("\n"));
        BufferedReader stderr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        errors = stderr.lines().collect(Collectors.joining("\n"));

        if (!p.waitFor(DEFAULT_TIMEOUT, TimeUnit.SECONDS)) {
            // Timed out
            throw new PortalServiceException(String.format(
                    "pylint process failed to complete before {} second timeout elapsed", DEFAULT_TIMEOUT));
        }

        // Finished successfully? pylint returns 0 on success *with no
        // issues*, 1 on failure to run properly, 2/4/8/16 for successful
        // completion with python convention/refactor/warning/error (codes
        // 2-16 bit-ORd into final result) or 32 on usage error.
        int rv = p.exitValue();
        if (rv == 1 || rv == 32) {
            logger.error("pylint failed");
            logger.debug("\npylint stderr:\n" + errors);
            logger.debug("\npylint stdout:\n" + results);
            throw new PortalServiceException(
                    String.format("pylint process returned non-zero exit value: {}", rv));
        } else if (rv != 0) {
            logger.info("pylint found issues");
        }
    } catch (PortalServiceException pse) {
        throw pse;
    } catch (Exception ex) {
        throw new PortalServiceException("Failed to run pylint on template", ex);
    }

    // Parse results into LintResult objects
    lints = parsePylintResults(results);

    // Clean up
    try {
        Files.delete(f);
    } catch (Exception ex) {
        throw new PortalServiceException("Failed to delete temporary template file.", ex);
    }

    return lints;
}

From source file:org.projectnyx.Nyx.java

@SneakyThrows({ IOException.class })
private void loadBuiltinModules() {
    URL resource = getClass().getClassLoader().getResource("mods.list");
    assert resource != null;
    @Cleanup
    BufferedReader reader = new BufferedReader(new InputStreamReader(resource.openStream()));
    reader.lines().forEach(line -> {
        line = line.trim();//w w  w .java  2  s  .co  m
        String[] args = line.split(":", 3);
        if (line.charAt(0) == '#' || args.length != 3) {
            return;
        }
        String moduleType = args[0].trim();
        if (moduleManagers.containsKey(moduleType)) {
            try {
                getLog().info(String.format("Loading builtin %s module: %s", moduleType, args[1]));
                moduleManagers.get(moduleType).loadForClass(args[1].trim(), args[2].trim());
            } catch (Exception e) {
                getLog().error("Cannot load builtin module", e);
            }
        } else {
            getLog().error("Unknown module type: " + moduleType);
        }
    });
}

From source file:io.vertx.lang.js.JSVerticleFactory.java

private boolean hasEngines(ClassLoader loader) {
    StringBuilder buf = new StringBuilder();
    BufferedReader br = new BufferedReader(new InputStreamReader(loader.getResourceAsStream("package.json")));
    br.lines().forEach(line -> buf.append(line));
    JsonObject json = new JsonObject(buf.toString());
    return json.containsKey("engines") && json.getJsonObject("engines").containsKey("node");
}

From source file:sx.blah.discord.api.internal.DiscordWS.java

@Override
public void onWebSocketBinary(byte[] payload, int offset, int len) {
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(new InflaterInputStream(new ByteArrayInputStream(payload, offset, len))));
    onWebSocketText(reader.lines().collect(Collectors.joining()));
    try {/* ww w  .  j a va 2s . co  m*/
        reader.close();
    } catch (IOException e) {
        Discord4J.LOGGER.error(LogMarkers.WEBSOCKET, "Encountered websocket error: ", e);
    }
}

From source file:org.talend.components.service.rest.JdbcComponentIntegrationTest.java

/** Quick and dirty to parse the test SQL for the inserted records. The stream must be closed to close the source. **/
private Stream<String[]> getInsertedValues() throws IOException {
    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(getClass().getResourceAsStream("data_users.sql")));
    return bufferedReader.lines()
            .map(in -> in.substring(in.indexOf("VALUES (") + "VALUES (".length(), in.lastIndexOf("');")))
            .map(in -> in.split("'?\\s*,\\s*'?"));
}

From source file:com.smoketurner.pipeline.application.core.MessageProcessor.java

/**
 * Stream an {@link S3Object} object and process each line with the
 * processor.//from   w  w w  .ja v  a2s  . c o m
 * 
 * @param object
 *            S3Object to download and process
 * @return number of events processed
 * @throws IOException
 *             if unable to stream the object
 */
private int streamObject(@Nonnull final S3Object object) throws IOException {

    final AtomicInteger eventCount = new AtomicInteger(0);
    try (S3ObjectInputStream input = object.getObjectContent()) {

        final BufferedReader reader;
        if (AmazonS3Downloader.isGZipped(object)) {
            reader = new BufferedReader(
                    new InputStreamReader(new StreamingGZIPInputStream(input), StandardCharsets.UTF_8));
        } else {
            reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
        }

        // failed will be true if we did not successfully broadcast all
        // of the events because of no consumers
        final boolean failed = reader.lines().peek(event -> eventCount.incrementAndGet())
                .anyMatch(broadcaster::test);

        if (failed) {
            // abort the current S3 download
            input.abort();
            LOGGER.error("Partial events broadcast ({} sent) from key: {}/{}", eventCount.get(),
                    object.getBucketName(), object.getKey());
            throw new IOException("aborting download");
        }
    }
    return eventCount.get();
}

From source file:org.martin.ftp.net.FTPLinker.java

public void updateFile(String textoLog, String ruta) throws MalformedURLException, IOException {

    URL url = new URL("ftp://" + user + ":" + password + "@" + server + ruta);
    BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
    String textoFile = "";

    br.lines().forEach((line) -> addText(textoFile, line));
    br.close();/*from   ww w. j a va 2s.  c om*/
    addText(textoFile, textoLog);

    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(url.openConnection().getOutputStream()));
    bw.write(textoFile);
    bw.flush();
    bw.close();
}

From source file:com.couchbase.devex.CouchDBConfig.java

@Override
public Observable<Document> startImport() {
    BufferedReader inp2 = null;
    try {/*  w ww . j  a  v a  2s .com*/
        URL url = new URL(downloadURL);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");
        // assume this is going to be a big file...
        conn.setReadTimeout(0);
        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }

        inp2 = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    Observable.from(inp2.lines()::iterator).flatMap(s -> {
        JsonNode node = null;
        if (s.endsWith("\"rows\":[")) {
            // first line, find total rows, offset
            s = s.concat("]}");
            try {
                node = om.readTree(s.toString());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            long totalRows = node.get(TOTAL_ROWS_PROPERTY).asLong();
            long offset = node.get(OFFSET_PROPERTY).asLong();
            log.info(String.format("Query starting at offset %d for a total of %d rows.", offset, totalRows));
            return Observable.empty();
        } else if (s.length() == 2) {
            // last line, do nothing
            log.info("end of the feed.");
            return Observable.empty();
        } else {
            try {
                if (s.endsWith(",")) {
                    node = om.readTree(s.substring(0, s.length() - 1).toString());
                } else {
                    node = om.readTree(s.toString());
                }
                String key = node.get("key").asText();
                String jsonDoc = node.get("doc").toString();
                Document doc = RawJsonDocument.create(key, jsonDoc);
                return Observable.just(doc);
            } catch (IOException e) {
                return Observable.error(e);
            }
        }

    });
    return Observable.empty();
}

From source file:gov.llnl.lc.smt.command.file.SmtFile.java

private List<String> getFileList(String fileName) {
    List<String> fileList = new ArrayList<String>();

    try {/*from w w  w.j av a2 s. co  m*/
        BufferedReader br = Files.newBufferedReader(Paths.get(fileName));
        //br returns as stream and convert it into a List
        fileList = br.lines().collect(Collectors.toList());

        // trim empty lines from list
        fileList.removeIf(s -> s.isEmpty());

        // trim comment lines from list
        fileList.removeIf(s -> s.startsWith("#"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return fileList;
}

From source file:net.dv8tion.jda.core.requests.Response.java

protected Response(final okhttp3.Response response, final int code, final String message, final long retryAfter,
        final Set<String> cfRays) {
    this.rawResponse = response;
    this.code = code;
    this.message = message;
    this.exception = null;
    this.retryAfter = retryAfter;
    this.cfRays = cfRays;

    if (response == null || response.body().contentLength() == 0) {
        this.object = null;
        return;//from   www . j  a  va2s  .com
    }

    InputStream body = null;
    BufferedReader reader = null;
    try {
        body = Requester.getBody(response);
        // this doesn't add overhead as org.json would do that itself otherwise
        reader = new BufferedReader(new InputStreamReader(body));
        char begin; // not sure if I really like this... but we somehow have to get if this is an object or an array
        int mark = 1;
        do {
            reader.mark(mark++);
            begin = (char) reader.read();
        } while (Character.isWhitespace(begin));

        reader.reset();

        if (begin == '{')
            this.object = new JSONObject(new JSONTokener(reader));
        else if (begin == '[')
            this.object = new JSONArray(new JSONTokener(reader));
        else
            this.object = reader.lines().collect(Collectors.joining());
    } catch (final Exception e) {
        throw new RuntimeException("An error occurred while parsing the response for a RestAction", e);
    } finally {
        try {
            body.close();
            reader.close();
        } catch (NullPointerException | IOException ignored) {
        }
    }
}