Example usage for java.io BufferedReader ready

List of usage examples for java.io BufferedReader ready

Introduction

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

Prototype

public boolean ready() throws IOException 

Source Link

Document

Tells whether this stream is ready to be read.

Usage

From source file:com.wavemaker.commons.util.IOUtils.java

/**
 * Read an entire File into a String.//from   w w w.java  2s  .  com
 *
 * @param f The file to read from.
 * @return The contents of f as a String.
 * @throws IOException
 */
public static String read(File f) throws IOException {
    StringBuilder fileSB = new StringBuilder();
    char[] buf = new char[DEFAULT_BUFFER_SIZE];

    BufferedReader br = null;

    try {

        br = new BufferedReader(new FileReader(f));

        while (br.ready()) {
            int readlen = br.read(buf);
            fileSB.append(buf, 0, readlen);
        }
        return fileSB.toString();

    } finally {
        closeSilently(br);
    }
}

From source file:com.wavemaker.commons.util.IOUtils.java

/**
 * Read the bottom of a File into a String. This probably isn't the proper way to do this in java but my goals here
 * were limited to NOT flooding memory with the entire file, but just to grab the last N lines and never have more
 * than the last N lines in memory/*from   w w  w. j ava 2  s .  c om*/
 */
public static String tail(File f, int lines) throws IOException {
    java.util.ArrayList<String> lineList = new java.util.ArrayList<String>(lines);

    BufferedReader br = null;

    try {

        br = new BufferedReader(new FileReader(f));

        while (br.ready()) {
            String s = br.readLine();
            if (s == null) {
                break;
            } else {
                lineList.add(s);
            }
            if (lineList.size() > lines) {
                lineList.remove(0);
            }
        }
        StringBuilder fileSB = new StringBuilder();
        for (int i = 0; i < lineList.size(); i++) {
            fileSB.append(lineList.get(i) + "\n");
        }
        return fileSB.toString();

    } finally {
        closeSilently(br);
    }
}

From source file:com.wavemaker.common.util.IOUtils.java

/**
 * Read an entire File into a String./*from ww  w  .  j a  v  a2 s .c o m*/
 * 
 * @param f The file to read from.
 * @return The contents of f as a String.
 * @throws IOException
 */
public static String read(File f) throws IOException {
    StringBuilder fileSB = new StringBuilder();
    char[] buf = new char[DEFAULT_BUFFER_SIZE];

    BufferedReader br = null;

    try {

        br = new BufferedReader(new FileReader(f));

        while (br.ready()) {
            int readlen = br.read(buf);
            fileSB.append(buf, 0, readlen);
        }
        return fileSB.toString();

    } finally {

        try {
            br.close();
        } catch (Exception ignore) {
        }
    }
}

From source file:net.modsec.ms.connector.ConnRequestHandler.java

/**
 * Executes the shell scripts for starting, restarting and stopping modsecurity. 
 * @param cmd - path of the shell script.
 * @param jsonResp  - response to WebSiren(webapp) request.
 * @return jsonresponse - response to WebSiren(webapp) request. 
 *//*from  ww  w  . j a va  2 s  . c  om*/
@SuppressWarnings("unchecked")
public static JSONObject executeShScript(String cmd, JSONObject jsonResp) {

    log.info("starting shell script execution ... :" + cmd);
    try {

        Process process = Runtime.getRuntime().exec(cmd);
        String outStr = "", s = "";

        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
        while ((s = br.readLine()) != null) {
            outStr += s;
        }

        log.info("Output String : " + outStr);

        String errOutput = "";
        BufferedReader br2 = new BufferedReader(new InputStreamReader(process.getErrorStream()));

        while (br2.ready() && (s = br2.readLine()) != null) {
            errOutput += s;
        }
        log.info("Error String : " + errOutput);

        if (errOutput.contains("Syntax error")) {

            jsonResp.put("status", "1");
            jsonResp.put("message", "Failed to start modsecurity");

        } else {

            jsonResp.put("status", "0");
            jsonResp.put("message", outStr);

        }

    } catch (IOException e) {

        jsonResp.put("status", "1");
        jsonResp.put("message", "Error: internal service is down");
        log.info("Error Message: " + e.getMessage());
        //e.printStackTrace();

    }

    return jsonResp;
}

From source file:com.wavemaker.common.util.IOUtils.java

/**
 * Read the bottom of a File into a String. This probably isn't the proper way to do this in java but my goals here
 * were limited to NOT flooding memory with the entire file, but just to grab the last N lines and never have more
 * than the last N lines in memory//from  ww w.  j  a v  a2 s  . com
 */
public static String tail(File f, int lines) throws IOException {
    java.util.ArrayList<String> lineList = new java.util.ArrayList<String>(lines);

    BufferedReader br = null;

    try {

        br = new BufferedReader(new FileReader(f));

        while (br.ready()) {
            String s = br.readLine();
            if (s == null) {
                break;
            } else {
                lineList.add(s);
            }
            if (lineList.size() > lines) {
                lineList.remove(0);
            }
        }
        StringBuilder fileSB = new StringBuilder();
        for (int i = 0; i < lineList.size(); i++) {
            fileSB.append(lineList.get(i) + "\n");
        }
        return fileSB.toString();

    } finally {

        try {
            br.close();
        } catch (Exception ignore) {
        }
    }
}

From source file:org.protocoderrunner.utils.FileIO.java

/**
 * Read the contents of the file indicated by fileName
 *//*  w  ww.  j  a v a  2s  .  c  o m*/
public static String read(InputStream is) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    while (br.ready()) {
        String line = br.readLine();
        sb.append("\n");
        sb.append(line);
    }
    String data = sb.toString();
    return data;
}

From source file:org.protocoderrunner.utils.FileIO.java

/**
 * Read the contents of the file indicated by fileName
 *///w ww.ja v  a2s . c o m
public static String read(Context activity, String fileName) throws IOException {
    if (fileName.contains("/0/")) {
        fileName = fileName.replace("/0/", "/legacy/");
    }
    FileInputStream is = activity.openFileInput(fileName);
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    while (br.ready()) {
        String line = br.readLine();
        sb.append(line);
    }
    String data = sb.toString();
    return data;
}

From source file:de.bps.onyx.plugin.OnyxModule.java

public static ResourceEvaluation isOnyxTest(File file, String filename) {
    ResourceEvaluation eval = new ResourceEvaluation();
    BufferedReader reader = null;
    try {// w  ww . ja va  2s.com
        ImsManifestFileFilter visitor = new ImsManifestFileFilter();
        Path fPath = PathUtils.visit(file, filename, visitor);
        if (visitor.isValid()) {
            Path qtiPath = fPath.resolve("imsmanifest.xml");
            reader = Files.newBufferedReader(qtiPath, StandardCharsets.UTF_8);
            while (reader.ready()) {
                String l = reader.readLine();
                if (l.indexOf("imsqti_xmlv2p1") != -1 || l.indexOf("imsqti_test_xmlv2p1") != -1
                        || l.indexOf("imsqti_assessment_xmlv2p1") != -1) {
                    eval.setValid(true);
                    break;
                }
            }
        } else {
            eval.setValid(false);
        }
    } catch (NoSuchFileException nsfe) {
        eval.setValid(false);
    } catch (IOException | IllegalArgumentException e) {
        log.error("", e);
        eval.setValid(false);
    } finally {
        IOUtils.closeQuietly(reader);
    }
    return eval;
}

From source file:data_gen.Data_gen.java

public static LinkedHashMap<String, Object> Parse_Document_fields(String config)
        throws FileNotFoundException, IOException {
    LinkedHashMap<String, Object> fields = new LinkedHashMap<>();
    File file = new File(config);
    FileReader fr = null;/*w w  w.j  av a2s .com*/
    BufferedReader br = null;
    try {
        fr = new FileReader(file);
        br = new BufferedReader(fr);
        while (br.ready()) {
            String temp = br.readLine();
            if (temp.startsWith("\"")) {
                String[] t = temp.split("=");
                String trim = t[0].substring(1, t[0].length() - 1).trim();
                fields.put(trim, t[1].trim());
            }
        }
        br.close();
    } catch (FileNotFoundException e) {
        //e.printStackTrace();
        System.out.println("The file could not be found.");
    } catch (IOException e) {
        // e.printStackTrace();
        System.out.println("There was an error reading the file.");
    }
    return fields;
}

From source file:data_gen.Data_gen.java

public static void Parse_Document_values(String config) throws FileNotFoundException, IOException {
    File file = new File(config);
    FileReader fr = null;// w w  w.  ja  va 2  s. c o  m
    BufferedReader br = null;
    try {
        fr = new FileReader(file);
        br = new BufferedReader(fr);
        while (br.ready()) {
            String temp = br.readLine();
            String[] t = temp.split("=");

            if (t[0].equalsIgnoreCase("output_name")) {
                Default_DataSet_name = t[1].trim();
            }
            if (t[0].equalsIgnoreCase("documents_count")) {
                documents_count = Long.parseLong(t[1].trim());
            }
            if (t[0].equalsIgnoreCase("dataset_size")) {
                Docments_Total_size = Long.parseLong(t[1].trim());
            }
            if (t[0].equalsIgnoreCase("output_dir")) {
                output_dir = t[1].trim();

            }
        }

        br.close();
    } catch (FileNotFoundException e) {
        //e.printStackTrace();
        System.out.println("The configration file could not be found.");
        System.exit(0);
    } catch (IOException e) {
        // e.printStackTrace();
        System.out.println("There was an error reading the configration file.");
        System.exit(0);
    } catch (NullPointerException e) {
        // e.printStackTrace();
        System.out.println("There was an error reading the configration file.");
        System.exit(0);
    } catch (NumberFormatException e) {
        // e.printStackTrace();
        System.out.println("There was an error reading the configration file.");
        System.exit(0);
    }

    if (Default_DataSet_name == null || documents_count <= 0 || Docments_Total_size <= 0
            || output_dir == null) {
        System.out.println("There was an error reading the configration file\n"
                + "make sure you have the following parametes specified EX:\n" + "documents_count=25000\n"
                + "dataset_size=10000000\n" + "output_name=output.json\n" + "output_dir=./dataset_one\n");
        System.exit(0);
    }

}