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:menusearch.json.JSONProcessor.java

/**
 * /*from w w  w. j av a  2  s.  c o m*/
 * @param searchID is the ID of the recipe you would like to search for 
 * @return a string containing one recipe in JSON format.
 * @throws IOException 
 * 
 * This method creates a formated URL that then connects to Yummly and returns a JSON string for the recipeID provided.
 * 
 * RecipeID can be obtained only by doing a general search through yummly.
 */
public static String getRecipeAPI(String searchID) throws IOException {
    BufferedReader reader = null;
    try {
        URL url = new URL("http://api.yummly.com/v1/api/recipe/" + searchID
                + "?_app_id=95a21eb2&_app_key=d703fa9e11ee34f104bc271ec3bbcdb9");
        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();
    }
}

From source file:org.codehaus.groovy.ant.Groovy.java

private static String getText(BufferedReader reader) throws IOException {
    StringBuilder answer = new StringBuilder();
    // reading the content of the file within a char buffer allow to keep the correct line endings
    char[] charBuffer = new char[4096];
    int nbCharRead = 0;
    while ((nbCharRead = reader.read(charBuffer)) != -1) {
        // appends buffer
        answer.append(charBuffer, 0, nbCharRead);
    }//  ww  w  . ja va2s . co m
    reader.close();
    return answer.toString();
}

From source file:eu.comvantage.dataintegration.QueryDistributionServiceImpl.java

private static String getBody(HttpServletRequest request) throws IOException {
    String body = null;//from   w w w. j a  v  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:org.apache.sysml.validation.ValidateLicAndNotice.java

/**
 * This will return the content of file.
 *
 * @param   file is the parameter of type File from which contents will be read and returned.
 * @return    Returns the contents from file in String format.
 *///from  w  w w .ja v  a 2  s.  c o m
private static String readFile(File file) throws java.io.IOException {
    StringBuffer fileData = new StringBuffer();
    BufferedReader reader = new BufferedReader(new FileReader(file));
    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();
    return fileData.toString();
}

From source file:org.grails.ide.eclipse.test.util.GrailsTest.java

public static String getContents(IFile file) throws CoreException, IOException {
    InputStream is = file.getContents();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    StringBuffer buffer = new StringBuffer();
    char[] readBuffer = new char[2048];
    int n = br.read(readBuffer);
    while (n > 0) {
        buffer.append(readBuffer, 0, n);
        n = br.read(readBuffer);/*from  ww w.j  a v  a2  s .  c  o  m*/
    }
    return buffer.toString();
}

From source file:eu.planets_project.tb.gui.backing.admin.wsclient.faces.WSClientBean.java

/** 
 * reads the contents of a file into a String
 * // w w w.  j a  v a 2 s.com
 * @param filePath the name of the file to open.  
 */
private static String readFileAsString(File file) throws java.io.IOException {
    StringBuffer fileData = new StringBuffer(1000);
    BufferedReader reader = new BufferedReader(new FileReader(file));
    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();
    return fileData.toString();
}

From source file:com.att.android.arodatacollector.utils.AROCollectorUtils.java

/**
 * find the process info/*  w ww  .  j ava 2 s  . c om*/
 * @param processName
 * @return
 * @throws IOException
 * @throws InterruptedException
 */
public static String executePS(String processName) throws IOException, InterruptedException {
    AROLogger.d(TAG, "entered ps...");

    final Process process = Runtime.getRuntime().exec("ps " + processName);
    final InputStreamReader inputStream = new InputStreamReader(process.getInputStream());
    final BufferedReader reader = new BufferedReader(inputStream);
    try {
        String line = null;

        int read;
        final char[] buffer = new char[4096];
        final StringBuffer output = new StringBuffer();
        while ((read = reader.read(buffer)) > 0) {
            output.append(buffer, 0, read);
        }
        // Waits for the command to finish.
        process.waitFor();
        //no need to destroy the process since waitFor() will wait until all subprocesses exit

        line = output.toString();
        return line;
    } finally {
        try {
            reader.close();
            inputStream.close();
            reader.close();

        } catch (Exception e) {
            AROLogger.e(TAG,
                    "Exception caught while closing resources in executePS. Error msg=" + e.getMessage());
            AROLogger.e(TAG, "execution will be allowed to continue");
        }

        AROLogger.d(TAG, "exiting ps...");
    }
}

From source file:com.senseidb.svc.impl.HttpRestSenseiServiceImpl.java

public static String convertStreamToString(InputStream is) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    char[] buf = new char[1024]; //1k buffer
    try {/*from   ww w . j  av a 2  s  . co m*/

        while (true) {
            int count = reader.read(buf);
            if (count < 0)
                break;
            sb.append(buf, 0, count);
        }
    } finally {
        is.close();
    }

    String json = sb.toString();
    if (log.isDebugEnabled()) {
        log.debug("received: " + json);
    }
    return json;
}

From source file:com.piaoyou.util.FileUtil.java

public static String readFile(String fileName, String srcEncoding) throws FileNotFoundException, IOException {

    File file = null;/*from   w  w w.  j av a 2  s  . co  m*/
    try {
        file = new File(fileName);
        if (file.isFile() == false) {
            throw new IOException("'" + fileName + "' is not a file.");
        }
    } finally {
        // we don't have to close File here
    }

    BufferedReader reader = null;
    try {
        StringBuffer result = new StringBuffer(1024);
        FileInputStream fis = new FileInputStream(fileName);
        reader = new BufferedReader(new InputStreamReader(fis, srcEncoding));

        char[] block = new char[512];
        while (true) {
            int readLength = reader.read(block);
            if (readLength == -1)
                break;// end of file
            result.append(block, 0, readLength);
        }
        return result.toString();
    } catch (FileNotFoundException fe) {
        log.error("Error", fe);
        throw fe;
    } catch (IOException e) {
        log.error("Error", e);
        throw e;
    } finally {
        try {
            if (reader != null)
                reader.close();
        } catch (IOException ex) {
        }
    }
}

From source file:free.yhc.feeder.model.Utils.java

/**
 *
 * @param file//  w w  w . j  ava2s . c om
 *   Text file.
 * @return
 *   value when reading non-text files, is not defined.
 */
public static String readTextFile(File file) {
    try {
        StringBuffer fileData = new StringBuffer(4096);
        BufferedReader reader = new BufferedReader(new FileReader(file));
        char[] buf = new char[4096];
        int bytes;
        while (-1 != (bytes = reader.read(buf)))
            fileData.append(buf, 0, bytes);
        reader.close();
        return fileData.toString();
    } catch (FileNotFoundException e) {
        return null;
    } catch (IOException e) {
        return null;
    }
}