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:com.evolveum.midpoint.util.MiscUtil.java

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

From source file:com.provenance.cloudprovenance.policyhandler.ws.controler.PolicyRequestHandler.java

public static String getBody(HttpServletRequest request) throws IOException {

    String body = null;/*from w w  w  .  j  av  a 2  s  . c  o  m*/
    StringBuilder stringBuilder = new StringBuilder();
    BufferedReader bufferedReader = null;

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

    body = stringBuilder.toString();
    return body;
}

From source file:com.tek271.reverseProxy.servlet.ProxyFilter.java

@SuppressWarnings({ "unchecked" })
private static StringEntity getEntity(HttpServletRequest request) throws IOException {
    if (APPLICATION_JSON.equalsIgnoreCase(request.getHeader("Content-type"))) {
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = null;
        InputStream inputStream = request.getInputStream();
        if (inputStream != null) {
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            char[] charBuffer = new char[512];
            int bytesRead = -1;
            while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                stringBuilder.append(charBuffer, 0, bytesRead);
            }/*from www  . j  a  v  a 2  s . c  o  m*/
            if (bufferedReader != null) {
                bufferedReader.close();
            }
        } else {
            stringBuilder.append("");
        }
        return new StringEntity(stringBuilder.toString(), "UTF-8");

    }
    List<NameValuePair> formparams = new ArrayList<NameValuePair>();
    Enumeration<String> en = request.getParameterNames();
    while (en.hasMoreElements()) {
        String name = en.nextElement();
        String value = request.getParameter(name);
        formparams.add(new BasicNameValuePair(name, value));
    }
    return new UrlEncodedFormEntity(formparams, "UTF-8");
}

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

/**
 * Read string key./*from w ww  . ja  va2  s  .c  o  m*/
 *
 * @param fileName the file name
 * @return the string
 */
public static 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 " + e);
    } finally {
        if (reader != null) {
            reader = null;
        }
    }
    return fileData.toString();

}

From source file:gov.nih.nci.ncicb.tcga.dcc.common.util.FileUtil.java

/**
 * Returns the file content as String// w  w w.jav  a 2s . c  o  m
 *
 * @param file                         the file which content is to return
 * @param normalizeToUnixLineSeparator <code>true</code> if the file content returned should have its line separators normalized to Unix standard, <code>false</code> otherwise
 * @return the file content as a String
 * @throws IOException
 */
public static String readFile(final File file, final boolean normalizeToUnixLineSeparator) throws IOException {

    String result;

    final StringBuilder stringBuilder = new StringBuilder();
    BufferedReader reader = null;

    try {
        reader = new BufferedReader(new FileReader(file));
        final char[] buffer = new char[BUFFER_SIZE];
        int character;

        while ((character = reader.read(buffer)) != -1) {
            stringBuilder.append(buffer, 0, character);
        }
    } finally {

        if (reader != null) {
            reader.close();
        }
    }

    result = stringBuilder.toString();

    if (normalizeToUnixLineSeparator) {
        result = replaceLineSeparators(result);
    }

    return result;
}

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

/**
 * Read an entire File into a String./*from   ww w  . j a v a2 s  .  co 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 {
        closeSilently(br);
    }
}

From source file:org.helm.rest.AjaxTool.java

static String getValue(Part part) throws IOException {
    if (part == null) {
        return null;
    }//from  w  ww . jav  a 2s  .co  m

    BufferedReader reader = new BufferedReader(new InputStreamReader(part.getInputStream(), "UTF-8"));
    StringBuilder value = new StringBuilder();
    char[] buffer = new char[1024];
    for (int length = 0; (length = reader.read(buffer)) > 0;) {
        value.append(buffer, 0, length);
    }
    return value.toString();
}

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

/**
 * Read an entire File into a String.//w w w.  j  ava2s  . c om
 * 
 * @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:com.provenance.cloudprovenance.traceabilitystore.ws.controler.TraceabilityStoreController.java

/**
 * Get request body content//from w w  w .  j  a  va2 s.c  o  m
 * 
 * @param request
 * @return string
 * @throws IOException
 */
public static String getBody(HttpServletRequest request) throws IOException {

    String body = null;
    StringBuilder stringBuilder = new StringBuilder();
    BufferedReader bufferedReader = null;

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

    body = stringBuilder.toString();
    return body;
}

From source file:menusearch.json.JSONProcessor.java

/**
 * //  www  .java  2  s  .  co m
 * @param query -- formatted HTML that's used with the searchRecipes API on yummly
 * @return  a string in JSON format that contains all the recipes that match the search parameters.
 * @throws MalformedURLException
 * @throws IOException 
 * 
 * You CAN NOT call this method directly -- to search Yummly call the method queryBuilder and pass it your search parameters.
 */
private static String searchYummly(String query) throws MalformedURLException, IOException {
    BufferedReader reader = null;
    try {
        URL url = new URL(query);
        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();
    } finally {
        if (reader != null)
            reader.close();
    }
}