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.b3log.symphony.api.CommentProcessor.java

/**
 * Get request body.//from  w  w w  . j a v  a2 s  .  c o m
 *
 * @param request req
 * @return body
 * @throws IOException io exception
 */
public String getBody(final HttpServletRequest request) throws IOException {
    final StringBuilder stringBuilder = new StringBuilder();
    BufferedReader bufferedReader = null;

    try {
        final InputStream inputStream = request.getInputStream();
        if (inputStream != null) {
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            final char[] charBuffer = new char[128];
            int bytesRead = -1;
            while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                stringBuilder.append(charBuffer, 0, bytesRead);
            }
        } else {
            stringBuilder.append("");
        }
    } catch (final IOException ex) {
        throw ex;
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (final IOException ex) {
                throw ex;
            }
        }
    }

    return stringBuilder.toString();
}

From source file:nl.imvertor.common.file.AnyFile.java

/** 
 * Write all file contents to the writer passed.
 * //  w  w  w .ja  v a 2s . c om
 * @param writer
 * @throws IOException
 */
public void readToWriter(Writer writer) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(this));
    char[] buf = new char[1024];
    int numRead = 0;
    while ((numRead = reader.read(buf)) != -1) {
        String readData = String.valueOf(buf, 0, numRead);
        writer.write(readData);
        buf = new char[1024];
    }
    reader.close();
}

From source file:nl.imvertor.common.file.AnyFile.java

public String getContent() throws IOException {
    StringBuffer fileData = new StringBuffer(1000);
    BufferedReader reader = new BufferedReader(new FileReader(this));
    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  ww  . j a va  2  s  . c  o m*/
        buf = new char[1024];
    }
    reader.close();
    return fileData.toString();
}

From source file:calendarioSeries.modelos.Serie.java

/**
 * readUrl/* www  .j  a v a2  s .c o  m*/
 * Mtodo que lee una url y devuelve la respuesta HTTP
 * @param stringUrl = Url a leer
 * @return String con la respuesta HTTP de la url.
 */
private String readUrl(String stringUrl) {
    BufferedReader reader = null;
    try {
        URL url = new URL(stringUrl);
        try {
            reader = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
            StringBuffer buffer = new StringBuffer();
            int read;
            char[] chars = new char[1024];
            while ((read = reader.read(chars)) != -1)
                buffer.append(chars, 0, read);
            reader.close();
            return buffer.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.harvester.CsvFileHarvestJob.java

private String readFromFile(File file) {
    String contents = null;/*from w  ww  .  j  av a2s .c o m*/
    BufferedReader reader = null;
    try {
        int fileSize = (int) (file.length());
        char[] buffer = new char[fileSize];
        reader = new BufferedReader(new FileReader(file), fileSize);
        reader.read(buffer);
        contents = new String(buffer);
    } catch (IOException e) {
        log.error(e, e);
    } finally {
        try {
            if (reader != null)
                reader.close();
        } catch (IOException e) {
            log.error(e, e);
        }
    }

    return contents;
}

From source file:com.wso2telco.gsma.authenticators.OpCoCompositeAuthenticator.java

/**
 * Read string key./*www  .ja  va 2  s.c  o m*/
 *
 * @param fileName the file name
 * @return the string
 */
private String readStringKey(String fileName) {
    BufferedReader reader = null;
    StringBuffer fileData = null;
    try {
        fileData = new StringBuffer(2048);
        reader = new BufferedReader(new FileReader(fileName));
        char[] buf = new char[1024];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            buf = new char[1024];
        }
        reader.close();
    } catch (Exception e) {
        log.error("Error Ocured " + e);
    } finally {
        if (reader != null) {
            reader = null;
        }
    }
    return fileData.toString();
}

From source file:org.jengine.mbean.FTPHL7Client.java

private boolean processFile(String fileName) {
    File file = new File(fileName);
    BufferedReader inF = null;
    try {//from   w ww .  j  a va  2  s  . c  om
        if (file.canRead() == true) {
            inF = new BufferedReader(new FileReader(fileName));
            char[] cbuf = new char[500000];
            int numRead = inF.read(cbuf);
            String messages = new String(cbuf).trim();
            String[] messageList = messages.split(System.getProperty("line.separator"));
            log.info("File " + fileName + " (size:" + numRead + ") contains " + messageList.length
                    + " messages.");
            log.info(messages);
            for (int i = 0; i < messageList.length; i++) {
                //System.out.println("message:" + messageList[i].toString());
                if (messageList[i].startsWith("#") != true) {
                    //send HL7 message to Queue
                    if (sendMsgToQueue(messageList[i]) != true) {
                        log.error("sendMsgToQueue() error");
                        break;
                    } else {
                        numberMessages++;
                        timeStampLastMsg = getCurrentTime();
                    }
                }
            }
        }
    } catch (IOException e) {
        return false;
    }
    try {
        inF.close();
    } catch (IOException e) {

    }
    //file.delete();
    return true;
}

From source file:com.aselalee.trainschedule.GetResultsFromSiteV2.java

private String doJSONRequest(String getReqURLStr) {
    String JSONOutputStr = null;//from w w  w  .  j  ava  2s.com
    /**
     * Setup networking.
     */
    HttpGet httpGet = new HttpGet(getReqURLStr);
    HttpClient httpClient = new DefaultHttpClient();
    HttpResponse response = null;
    /**
     * Send HTTP GET request.
     */
    try {
        response = httpClient.execute(httpGet);
    } catch (ClientProtocolException e) {
        errorCode = Constants.ERR_NETWORK_ERROR;
        errorString = "HTTPERROR : ClientProtocolException : " + e;
        Log.e(Constants.LOG_TAG, errorString);
        return null;
    } catch (IOException e) {
        errorCode = Constants.ERR_NETWORK_ERROR;
        errorString = "HTTPERROR : IOException : " + e;
        Log.e(Constants.LOG_TAG, errorString);
        return null;
    }
    /**
     * Get output stream from response.
     */
    InputStream ips = null;
    try {
        ips = response.getEntity().getContent();
    } catch (IOException e) {
        errorCode = Constants.ERR_ERROR;
        errorString = "getEntity.getContentERROR : IOException : " + e;
        Log.e(Constants.LOG_TAG, errorString);
        return null;
    } catch (IllegalStateException e) {
        errorCode = Constants.ERR_ERROR;
        errorString = "getEntity.getContentERROR : IllegalStateException : " + e;
        Log.e(Constants.LOG_TAG, errorString);
        return null;
    }
    /**
     * Read output result from stream.
     */
    StringBuilder strBuilder = new StringBuilder();
    try {
        char[] bytes = new char[1024];
        int numRead = 0;
        BufferedReader reader = new BufferedReader(new InputStreamReader(ips, "UTF-8"), 8192);
        while ((numRead = reader.read(bytes)) > 0) {
            strBuilder.append(new String(bytes, 0, numRead));
        }
        reader.close();
        reader = null;
    } catch (IOException e) {
        errorCode = Constants.ERR_ERROR;
        errorString = "InputStreamReaderERROR : IOException - Read/Close Error : " + e;
        Log.e(Constants.LOG_TAG, errorString);
        return null;
    }
    JSONOutputStr = strBuilder.toString();
    try {
        ips.close();
    } catch (IOException e) {
        errorCode = Constants.ERR_ERROR;
        errorString = "InputStreamReaderError: IOException - Close Error" + e;
        Log.e(Constants.LOG_TAG, errorString);
    }
    strBuilder = null;
    httpClient = null;
    httpGet = null;
    response = null;
    ips = null;
    return JSONOutputStr;
}

From source file:com.adito.boot.Util.java

/**
 * Read an input stream and load it into a string.
 * /*w w  w . j av a  2  s.  c  om*/
 * @param in input stream
 * @param charsetName encoding or <code>null</code> for default
 * @return string
 * @throws IOException on any error
 */
public static String loadStreamToString(InputStream in, String charsetName) throws IOException {
    StringBuffer licenseText = new StringBuffer();
    BufferedReader br = new BufferedReader(
            charsetName == null ? new InputStreamReader(in) : new InputStreamReader(in, charsetName));
    try {
        char[] buf = new char[65536];
        int r = 0;
        while ((r = br.read(buf)) != -1)
            licenseText.append(buf, 0, r);
    } finally {
        br.close();
    }
    return licenseText.toString();
}

From source file:com.mp3bot.App.java

public String readFromURL(final String url) {
    String ret = "";
    URL my = null;/*from www  . j  a  va 2  s  . c  o  m*/
    contentBuf.clear();
    try {
        my = new URL(url);
    } catch (MalformedURLException ex) {
        Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        return ret;
    }
    InputStreamReader reader = null;
    BufferedReader breader = null;
    try {
        reader = new InputStreamReader(my.openStream());
        breader = new BufferedReader(reader);
    } catch (IOException ex) {
        Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        return ret;
    }

    try {
        if (reader != null && breader != null) {
            int rlen = 0;
            while ((rlen = breader.read(contentBuf)) > 0) {
                //System.out.println("read len= " + rlen + " bytes");
            }
            reader.close();
        }
    } catch (IOException ex) {
        Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        return ret;
    }
    reader = null;
    my = null;

    contentBuf.flip();
    ret = contentBuf.toString().replaceAll("[\n\r]", "");
    return ret;
}