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:edu.uci.ics.jung.io.PartitionDecorationReader.java

/**
 * Decorates vertices in the specified partition with strings. The
 * decorations are specified by a text file with the following format:
 * /*ww  w . j  a v  a2 s. co m*/
 * <pre>
 * vid_1 label_1 
 * vid_2 label_2 ...
 * </pre>
 * 
 * <p>
 * The strings must be unique within this partition; duplicate strings will
 * cause a <code>UniqueLabelException</code> to be thrown.
 * 
 * <p>
 * The end of the file may be artificially set by putting the string <code>end_of_file</code>
 * on a line by itself.
 * </p>
 * 
 * @param bg
 *            the bipartite graph whose vertices are to be decorated
 * @param name_reader
 *            the reader containing the decoration information
 * @param partition
 *            the vertex partition whose decorations are specified by this
 *            file
 * @param string_key
 *            the user data key for the decorations created
 */
public static void loadStrings(Graph bg, Reader name_reader, Predicate partition, Object string_key) {
    StringLabeller id_label = StringLabeller.getLabeller(bg, partition);
    StringLabeller string_label = StringLabeller.getLabeller(bg, string_key);
    try {
        BufferedReader br = new BufferedReader(name_reader);
        while (br.ready()) {
            String curLine = br.readLine();
            if (curLine == null || curLine.equals("end_of_file"))
                break;
            if (curLine.trim().length() == 0)
                continue;
            String[] parts = curLine.trim().split("\\s+", 2);

            Vertex v = id_label.getVertex(parts[0]);
            if (v == null)
                throw new FatalException("Invalid vertex label");

            // attach the string to this vertex
            string_label.setLabel(v, parts[1]);
        }
        br.close();
        name_reader.close();
    } catch (IOException ioe) {
        throw new FatalException("Error loading names from reader " + name_reader, ioe);
    } catch (UniqueLabelException ule) {
        throw new FatalException("Unexpected duplicate name in reader " + name_reader, ule);
    }
}

From source file:edu.uci.ics.jung.io.PartitionDecorationReader.java

public static void loadCounts(Graph bg, Reader count_reader, Predicate partition, Object count_key,
        UserData.CopyAction copyact, int num_types) {
    StringLabeller id_label = StringLabeller.getLabeller(bg, partition);
    try {//ww w.java  2s  . c  o m
        BufferedReader br = new BufferedReader(count_reader);

        while (br.ready()) {
            String curLine = br.readLine();
            if (curLine == null || curLine.equals("end_of_file"))
                break;
            if (curLine.trim().length() == 0)
                continue;

            StringTokenizer st = new StringTokenizer(curLine);
            String entity_id = st.nextToken();
            int type_id = new Integer(st.nextToken()).intValue() - 1;
            int count = new Integer(st.nextToken()).intValue();

            Vertex v = id_label.getVertex(entity_id);

            if (v == null)
                throw new IllegalArgumentException("Unrecognized vertex " + entity_id);

            double[] counts = (double[]) v.getUserDatum(count_key);
            if (counts == null) {
                counts = new double[num_types];
                v.addUserDatum(count_key, counts, copyact);
            }
            counts[type_id] = count;
        }
        br.close();
    } catch (IOException ioe) {
        throw new FatalException("Error in loading counts from " + count_reader);
    }
}

From source file:com.symbian.driver.engine.Utils.java

/**
 * @param aBufferedReader/* ww  w . j  ava2 s  . com*/
 * @param aString
 * @return
 * @throws IOException
 */
public static final boolean readBuffer(BufferedReader aBufferedReader, String[] aString) throws IOException {
    String lReadLine = null;
    int lLinesRead = 0;
    try {
        while (lLinesRead < 20 && aBufferedReader.ready() && (lReadLine = aBufferedReader.readLine()) != null) {
            System.out.println(lReadLine);
            for (int lIter = 0; lIter < aString.length; lIter++) {
                if (lReadLine.toLowerCase().indexOf(aString[lIter]) >= 0) {
                    aBufferedReader.close();
                    return true;
                }
            }
            lLinesRead++;
        }
    } catch (Exception e) {
        System.out.println("Buffer broke");
    }
    aBufferedReader.close();
    return false;
}

From source file:edu.uci.ics.jung.io.PartitionDecorationReader.java

/**
 * Decorates vertices in the specified partition with typed count data. 
 * The data must be contained in a text file in the following format:
 * /*from  w  w  w  .  ja  va  2 s  .  c  om*/
 * <pre>
 * vid_1    type_1  count_1
 * vid_2    type_2  count_2
 * ...
 * </pre>
 * 
 * <p>where <code>count_i</code> (an integer value) represents 
 * the number of elements of 
 * type <code>type_i</code> possessed by the vertex with ID 
 * <code>vid_i</code> (as defined by <code>BipartiteGraphReader.load()</code>)
 * for the <code>i</code>th line in the file.</p>
 * 
 * <p>For example, the vertices might represent authors, the type might
 * represent a topic, and the count might represent the number of 
 * papers that the specified author had written on that topic.<p>
 * 
 * <p>If <code>normalize</code> is <code>true</code>, then the 
 * count data will be scaled so that the counts for
 * each vertex will sum to 1.  (In this case, each vertex must have
 * a positive total count value.)
 * 
 * <p>The end of the file may be artificially set by putting the string
 * <code>end_of_file</code> on a line by itself.</p>
 * 
 * @return              the total number of types observed
 * @param bg            the bipartite graph whose vertices are to be decorated
 * @param count_reader  the reader containing the decoration data
 * @param partition     the partition whose decorations are specified by this file
 * @param count_key     the user key for the decorations
 * @param copyact       the copy action for the decorations
 */
public static int loadCounts(Graph bg, Reader count_reader, Predicate partition, Object count_key,
        UserData.CopyAction copyact) {
    StringLabeller id_label = StringLabeller.getLabeller(bg, partition);
    Set types = new HashSet();
    try {
        BufferedReader br = new BufferedReader(count_reader);

        while (br.ready()) {
            String curLine = br.readLine();
            if (curLine == null || curLine.equals("end_of_file"))
                break;
            if (curLine.trim().length() == 0)
                continue;

            StringTokenizer st = new StringTokenizer(curLine);
            String entity_id = st.nextToken();
            String type_id = st.nextToken();
            Integer count = new Integer(st.nextToken());

            types.add(type_id);

            Vertex v = id_label.getVertex(entity_id);

            if (v == null)
                throw new IllegalArgumentException("Unrecognized vertex " + entity_id);

            Map count_map = (Map) v.getUserDatum(count_key);
            if (count_map == null) {
                count_map = new HashMap();
                v.addUserDatum(count_key, count_map, copyact);
            }
            count_map.put(type_id, count);

        }
        br.close();
        count_reader.close();
    } catch (IOException ioe) {
        throw new FatalException("Error in loading counts from " + count_reader);
    }

    return types.size();
}

From source file:Zip.java

/**
 * Reads a Zip file, iterating through each entry and dumping the contents
 * to the console.//ww w  . jav  a 2 s. c o  m
 */
public static void readZipFile(String fileName) {
    ZipFile zipFile = null;
    try {
        // ZipFile offers an Enumeration of all the files in the Zip file
        zipFile = new ZipFile(fileName);
        for (Enumeration e = zipFile.entries(); e.hasMoreElements();) {
            ZipEntry zipEntry = (ZipEntry) e.nextElement();
            System.out.println(zipEntry.getName() + " contains:");
            // use BufferedReader to get one line at a time
            BufferedReader zipReader = new BufferedReader(
                    new InputStreamReader(zipFile.getInputStream(zipEntry)));
            while (zipReader.ready()) {
                System.out.println(zipReader.readLine());
            }
            zipReader.close();
        }
    } catch (IOException ioe) {
        System.out.println("An IOException occurred: " + ioe.getMessage());
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:ch.zhaw.iamp.rct.weights.Weights.java

private static RealMatrix csvToMatrix(final String sourceFilePath) throws IOException {
    BufferedReader in = new BufferedReader(new FileReader(sourceFilePath));
    double[][] matrixArray = null;

    while (in.ready()) {
        String currentLine = in.readLine();

        if (currentLine.startsWith("#") || currentLine.trim().isEmpty()) {
            continue;
        }/*from   ww  w. j av a2  s  .  c o  m*/

        String[] tokens = currentLine.split(",");

        if (matrixArray == null) {
            matrixArray = new double[0][tokens.length];
        }

        double[][] tmpArray = new double[1][tokens.length];

        for (int i = 0; i < tokens.length; ++i) {
            if (!tokens[i].trim().isEmpty()) {
                tmpArray[0][i] = Double.parseDouble(tokens[i]);
            }
        }

        matrixArray = concat(matrixArray, tmpArray);
    }

    return MatrixUtils.createRealMatrix(matrixArray);
}

From source file:org.onehippo.forge.jcrshell.CommandHelper.java

public static void loadCommandsFromResource(String resourceName) {
    InputStream is = CommandHelper.class.getResourceAsStream(resourceName);
    if (is != null) {
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        try {/*from ww  w. j  a  v  a  2 s. co m*/
            while (br.ready()) {
                try {
                    String clazz = br.readLine().trim();
                    if (clazz != null && clazz.length() != 0) {
                        CommandHelper.registerCommandClass(clazz);
                    }
                } catch (IOException e) {
                    log.error("Error while reading line from '{}': {}", resourceName, e.getMessage());
                    log.debug("Stack:", e);
                }
            }
        } catch (IOException e) {
            log.error("Error while reading commands from '{}': {}", resourceName, e.getMessage());
            log.debug("Stack:", e);
        } finally {
            IOUtils.closeQuietly(br);
            IOUtils.closeQuietly(is);
        }
    } else {
        log.info("Commands file not found on classpath: '{}'", resourceName);
    }
}

From source file:simpleserver.thread.Statistics.java

private static String getRemoteContent(URL url, JSONObject data) throws IOException {
    URLConnection con = url.openConnection();
    if (data != null) {
        String post = "data=" + URLEncoder.encode(data.toString(), "UTF-8");
        con.setDoOutput(true);/*from ww w  .j av a2 s.c  o m*/
        OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
        writer.write(post);
        writer.flush();
    }
    BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));

    StringBuilder content = new StringBuilder();

    while (reader.ready()) {
        content.append(reader.readLine());
    }

    return content.toString();
}

From source file:Zip.java

/**
 * Reads a Jar file, displaying the attributes in its manifest and dumping
 * the contents of each file contained to the console.
 *//*  w ww.ja v a  2 s  .  c  om*/
public static void readJarFile(String fileName) {
    JarFile jarFile = null;
    try {
        // JarFile extends ZipFile and adds manifest information
        jarFile = new JarFile(fileName);
        if (jarFile.getManifest() != null) {
            System.out.println("Manifest Main Attributes:");
            Iterator iter = jarFile.getManifest().getMainAttributes().keySet().iterator();
            while (iter.hasNext()) {
                Attributes.Name attribute = (Attributes.Name) iter.next();
                System.out.println(
                        attribute + " : " + jarFile.getManifest().getMainAttributes().getValue(attribute));
            }
            System.out.println();
        }
        // use the Enumeration to dump the contents of each file to the console
        System.out.println("Jar file entries:");
        for (Enumeration e = jarFile.entries(); e.hasMoreElements();) {
            JarEntry jarEntry = (JarEntry) e.nextElement();
            if (!jarEntry.isDirectory()) {
                System.out.println(jarEntry.getName() + " contains:");
                BufferedReader jarReader = new BufferedReader(
                        new InputStreamReader(jarFile.getInputStream(jarEntry)));
                while (jarReader.ready()) {
                    System.out.println(jarReader.readLine());
                }
                jarReader.close();
            }
        }
    } catch (IOException ioe) {
        System.out.println("An IOException occurred: " + ioe.getMessage());
    } finally {
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:models.Calais.java

public static void test() throws IOException, ParseException {
    // Simpler test with two people and two companies
    // String html = "<html> <body> Mark Zuckerberg, Steve Jobs, Twitter and Apple Inc  </body> </html>";

    // Full test with real html
    String html = "";
    BufferedReader reader = new BufferedReader(new FileReader("input/techcrunch.html"));
    while (reader.ready()) {
        html += reader.readLine();/*from   w ww.j  a va2  s.c o  m*/
    }
    reader.close();
    System.out.println("html = " + html);
    JSONObject result = run(html);
    System.out.println("result = " + result);

    //httpClientPost.parseJSON("output/techcrunch.html.xml");
}