Example usage for java.io BufferedReader read

List of usage examples for java.io BufferedReader read

Introduction

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

Prototype

public int read(java.nio.CharBuffer target) throws IOException 

Source Link

Document

Attempts to read characters into the specified character buffer.

Usage

From source file:org.jresponder.message.MessageRefImpl.java

@Override
public synchronized void refresh() throws InvalidMessageException {

    try {//  w ww  .  j  a va2s  . c om

        logger().debug("MessageRef - Starting refresh for: {}", file.getCanonicalPath());

        // set timestamp
        fileContentsTimestamp = file.lastModified();

        StringBuilder myStringBuilder = new StringBuilder();
        char[] buf = new char[4096];
        BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
        int len;
        while ((len = r.read(buf)) > 0) {
            myStringBuilder.append(buf, 0, len);
        }
        r.close();

        fileContents = myStringBuilder.toString();

        document = Jsoup.parse(fileContents, "UTF-8");

        propMap = new HashMap<String, String>();

        Elements myMetaTagElements = document.select("meta");
        if (myMetaTagElements == null || myMetaTagElements.isEmpty()) {
            throw new InvalidMessageException("No meta tags found in file: " + file.getCanonicalPath());
        }

        for (Element myPropElement : myMetaTagElements) {
            String myName = myPropElement.attr("name");
            String myValue = myPropElement.attr("content");
            propMap.put(myName, myValue);
        }

        // bodies are not read at all until message generation time

    } catch (IOException e) {
        throw new InvalidMessageException(e);
    }

    // debug dump
    if (logger().isDebugEnabled()) {
        for (String myKey : propMap.keySet()) {
            logger().debug("  property -- {}: {}", myKey, (propMap.get(myKey)));
        }
    }
}

From source file:org.ebayopensource.turmeric.tools.library.utils.TypeLibraryUtilities.java

private static String readContent(InputStream input) throws IOException {
    Charset defaultCharset = Charset.defaultCharset();
    StringBuilder strBuff = new StringBuilder();
    InputStreamReader isr = null;
    BufferedReader reader = null;

    try {/*  w  ww.j  ava  2s  .  co m*/
        isr = new InputStreamReader(input, defaultCharset);
        reader = new BufferedReader(isr);
        char[] charBuff = new char[512];
        int charsRead = -1;
        while ((charsRead = reader.read(charBuff)) > -1) {
            strBuff.append(charBuff, 0, charsRead);
        }
    } finally {
        CodeGenUtil.closeQuietly(reader);
        CodeGenUtil.closeQuietly(isr);
    }
    return strBuff.toString();
}

From source file:com.trafficspaces.api.controller.Connector.java

private char[] readResponseData(InputStream stream, String encoding) {
    BufferedReader in = null;
    char[] data = null;
    try {/*  w  w  w .j  av  a 2 s  .  co  m*/
        StringBuffer buf = new StringBuffer();
        data = new char[1024];

        in = new BufferedReader(new InputStreamReader(stream, encoding));
        int charsRead;
        while ((charsRead = in.read(data)) != -1) {
            buf.append(data, 0, charsRead);
        }
        data = new char[buf.length()];
        buf.getChars(0, data.length, data, 0);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return data != null ? data : null;
}

From source file:edu.clemson.lph.utils.CSVParserWrapper.java

private String readReaderAsString(BufferedReader reader) throws IOException {
    StringBuffer fileData = new StringBuffer();
    char[] buf = new char[1024];
    int numRead = 0;
    while ((numRead = reader.read(buf)) != -1) {
        String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);/*from w  w  w  . j a  v  a 2  s.  c  o  m*/
    }
    reader.close();
    return fileData.toString();
}

From source file:com.betel.flowers.pdf.util.XMLtoHtml.java

public void transform(File source, String srcEncoding, File target, String tgtEncoding) throws IOException {
    BufferedReader br = null;
    BufferedWriter bw = null;//from w ww . j a v a 2  s .  c om
    try {
        br = new BufferedReader(new InputStreamReader(new FileInputStream(source), srcEncoding));
        bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(target), tgtEncoding));
        char[] buffer = new char[16384];
        int read;
        while ((read = br.read(buffer)) != -1) {
            bw.write(buffer, 0, read);
        }
    } finally {
        try {
            if (br != null) {
                br.close();
            }
        } finally {
            if (bw != null) {
                bw.close();
            }
        }
    }
}

From source file:org.tellervo.desktop.wsi.JaxbResponseHandler.java

public T toDocument(final HttpEntity entity, final String defaultCharset) throws IOException, ParseException {
    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }/*from   ww  w. j a v a2  s  . com*/

    InputStream instream = entity.getContent();
    if (instream == null) {
        return null;
    }

    String charset = EntityUtils.getContentCharSet(entity);
    if (charset == null) {
        charset = defaultCharset;
    }
    if (charset == null) {
        charset = HTTP.DEFAULT_CONTENT_CHARSET;
    }

    /**
     * Methodology
     * 1) Save XML to disk temporarily (it can be big, 
     *       we might want to look at it as a raw file)
     * 2) If we can't write to disk, throw IOException
     * 3) Try to parse
     * 4) Throw error with the raw document (it's malformed XML)
     */

    File tempFile = null;
    OutputStreamWriter fileOut = null;
    boolean usefulFile = false; // preserve this file outside this local context?

    try {
        tempFile = File.createTempFile("tellervo", ".xml");
        fileOut = new OutputStreamWriter(new FileOutputStream(tempFile, false), charset);

        // ok, dump the webservice xml to a file
        BufferedReader webIn = new BufferedReader(new InputStreamReader(instream, charset));

        char indata[] = new char[8192];
        int inlen;

        // write the file out
        while ((inlen = webIn.read(indata)) >= 0)
            fileOut.write(indata, 0, inlen);

        fileOut.close();
    } catch (IOException ioe) {
        // File I/O failed (?!) Clean up and re-throw the IOE.
        if (fileOut != null)
            fileOut.close();
        if (tempFile != null)
            tempFile.delete();

        throw ioe;
    }

    try {
        Unmarshaller um = context.createUnmarshaller();
        ValidationEventCollector vec = new ValidationEventCollector();

        // validate against this schema
        um.setSchema(validateSchema);

        // collect events instead of throwing exceptions
        um.setEventHandler(vec);

        // do the magic!
        Object ret = um.unmarshal(tempFile);

        // typesafe way of checking if this is the right type!
        return returnClass.cast(ret);
    } catch (UnmarshalException ume) {
        usefulFile = true;
        throw new ResponseProcessingException(ume, tempFile);

    } catch (JAXBException jaxbe) {
        usefulFile = true;
        throw new ResponseProcessingException(jaxbe, tempFile);

    } catch (ClassCastException cce) {
        usefulFile = true;
        throw new ResponseProcessingException(cce, tempFile);

    } finally {
        // clean up and delete
        if (tempFile != null) {
            if (!usefulFile)
                tempFile.delete();
            else
                tempFile.deleteOnExit();
        }
    }

    /*
    try {
       um.un
    } catch (JDOMException jdome) {
    // this document must be malformed
    usefulFile = true;
    throw new XMLParsingException(jdome, tempFile);
       } 
    } catch (IOException ioe) {
       // well, something there failed and it was lower level than just bad XML...
       if(tempFile != null) {
    usefulFile = true;
    throw new XMLParsingException(ioe, tempFile);
       }
               
       throw new XMLParsingException(ioe);
               
    } finally {
       // make sure we closed our file
       if(fileOut != null)
    fileOut.close();
               
       // make sure we delete it, too
       if(tempFile != null)
       {
    if (!usefulFile)
       tempFile.delete();
    else
       tempFile.deleteOnExit();
       }
    }
    */
}

From source file:com.ewhoxford.android.bloodpressure.ghealth.gdata.GDataHealthClient.java

private String bufferData(InputStream istream) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(istream));

    StringBuilder sb = new StringBuilder();
    int read;//from   www . j  ava  2 s.c  o  m
    char[] buffer = new char[1024];

    try {
        while ((read = reader.read(buffer)) != -1) {
            sb.append(buffer, 0, read);
        }
    } finally {
        istream.close();
    }

    return sb.toString();
}

From source file:calendarioSeries.vistas.NewSerieController.java

private String readUrl(String stringUrl) {
    BufferedReader reader = null;
    try {/*from  ww w .  j av  a 2  s .c  om*/
        URL url = new URL(stringUrl);
        try {
            reader = new BufferedReader(new InputStreamReader(url.openStream()));
            StringBuffer buffer = new StringBuffer();
            int read;
            char[] chars = new char[1024];
            while ((read = reader.read(chars)) != -1)
                buffer.append(chars, 0, read);
            return buffer.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.codehaus.mojo.native2ascii.Native2Ascii.java

/**
 * Converts given file into file unicode escaped ASCII file.
 *
 * @param src/*from   w w  w.  ja v  a 2s . co  m*/
 * @param dst
 * @param encoding
 * @throws IOException
 */
public void nativeToAscii(final File src, final File dst, final String encoding) throws IOException {
    log.info("Converting: '" + src + "' to: '" + dst + "'");
    BufferedReader input = null;
    BufferedWriter output = null;
    try {
        if (!dst.getParentFile().exists()) {
            dst.getParentFile().mkdirs();
        }
        input = new BufferedReader(new InputStreamReader(new FileInputStream(src), encoding));
        output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dst), "ISO-8859-1"));

        final char[] buffer = new char[4096];
        int len;
        while ((len = input.read(buffer)) != -1) {
            output.write(nativeToAscii(CharBuffer.wrap(buffer, 0, len).toString()));
        }
    } finally {
        closeQuietly(src, input);
        closeQuietly(dst, output);
    }
}

From source file:org.intermine.webservice.server.widget.ReportWidgetsServlet.java

private String readInFile(String path) throws java.io.IOException {
    StringBuffer fileData = new StringBuffer(1000);
    BufferedReader reader = new BufferedReader(new FileReader(path));
    char[] buf = new char[1024];
    int numRead = 0;
    while ((numRead = reader.read(buf)) != -1) {
        String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);//  w w  w.jav a 2  s  . c om
        buf = new char[1024];
    }
    reader.close();
    return fileData.toString();
}