Example usage for java.io InputStreamReader read

List of usage examples for java.io InputStreamReader read

Introduction

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

Prototype

public int read(char cbuf[], int offset, int length) throws IOException 

Source Link

Document

Reads characters into a portion of an array.

Usage

From source file:nl.spellenclubeindhoven.dominionshuffle.DataReader.java

public static String readStringFromStream(InputStream inputStream) {
    InputStreamReader in = null;
    try {/*  w  ww  .  j a  va 2  s. co  m*/
        in = new InputStreamReader(inputStream);
        StringBuilder builder = new StringBuilder();
        char[] buffer = new char[1024];
        int numread = 0;

        numread = in.read(buffer, 0, 1024);
        while (numread != -1) {
            builder.append(buffer, 0, numread);
            numread = in.read(buffer, 0, 1024);
        }

        return builder.toString();
    } catch (IOException ignore) {
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ignore) {
            }
        }
    }

    return "";
}

From source file:net.xeger.rest.AbstractResource.java

static public String readResponse(HttpEntity entity) throws IOException {
    String response = "";

    int length = (int) entity.getContentLength();

    if (length <= 0) {
        length = (64 * 1024);/*from www.j  a  v  a  2  s  .com*/
    }

    StringBuffer sb = new StringBuffer(length);
    InputStreamReader isr = new InputStreamReader(entity.getContent(), "UTF-8");
    char buff[] = new char[length];
    int cnt;
    while ((cnt = isr.read(buff, 0, length - 1)) > 0) {
        sb.append(buff, 0, cnt);
    }

    response = sb.toString();
    isr.close();
    return response;
}

From source file:net.audumla.climate.bom.BOMClimateObserverCatalogue.java

protected static ClimateObserver getClimateObserverByCoordinates(ClimateDataSource coordinateSource) {

    ClimateObserverCollection aggregateObserver = new ClimateObserverCollectionHandler(coordinateSource);
    try {//from  w w w  . jav  a2  s .c  o  m
        String url = generationCoordinateURL(coordinateSource, historyKey);
        List<BOMClimateDataSource> stationSources = new ArrayList<BOMClimateDataSource>();
        InputStream is = new URL(url).openStream();
        InputStreamReader sr = new InputStreamReader(is);
        char[] buffer = new char[10240];
        int c = 0;
        if ((c = sr.read(buffer, 0, 10240)) > 0) {
            stationSources = parseSources(coordinateSource, 5, new String(buffer, 0, c));
        }

        for (BOMClimateDataSource source : stationSources) {
            aggregateObserver
                    .addClimateObserverTail(getClimateObserverById(BOMPeriodicClimateObserver.class, source));
        }
        for (BOMClimateDataSource source : stationSources) {
            aggregateObserver
                    .addClimateObserverTail(getClimateObserverById(BOMHistoricalClimateObserver.class, source));
        }
        for (BOMClimateDataSource source : stationSources) {
            aggregateObserver.addClimateObserverTail(
                    getClimateObserverById(BOMSimpleHistoricalClimateObserver.class, source));
        }
        for (BOMClimateDataSource source : stationSources) {
            aggregateObserver.addClimateObserverTail(
                    getClimateObserverById(BOMSimpleClimateForcastObserver.class, source));
        }

        url = generationCoordinateURL(coordinateSource, statisticKey);
        stationSources = new ArrayList<BOMClimateDataSource>();
        is = new URL(url).openStream();
        sr = new InputStreamReader(is);
        buffer = new char[10240];
        c = 0;
        if ((c = sr.read(buffer, 0, 10240)) > 0) {
            stationSources = parseSources(coordinateSource, 5, new String(buffer, 0, c));
        }

        for (BOMClimateDataSource source : stationSources) {
            aggregateObserver.addClimateObserverTail(
                    getClimateObserverById(BOMStatisticalClimateDataObserver.class, source));
        }
        return aggregateObserver.buildClimateObserver();

    } catch (Exception e) {
        throw new UnsupportedOperationException(e);
    }
}

From source file:nl.cwi.monetdb.xquery.xrpc.api.XRPCHTTPConnection.java

/** 
 * Sends the given XRPC request over an HTTP connection 
 * to the destination server and returns the server's 
        //from   w  w w  .  j  a v a  2  s.  c om
 * response message, which can be an XRPC response message or a SOAP 
 * Fault message, in a StringBuffer. 
 * 
 * @param server URL of the destination XRPC server 
 * @param request The (XRPC) request to send 
 * @return Server's response message, which can be an XRPC response 
 * message or a SOAP Fault message. 
 * @throws IOException If an I/O error occurs 
 */
public static StringBuffer sendReceive(String server, String request) throws IOException {
    URL url = new URL(server);

    PostMethod pmethod = new PostMethod(server);

    //        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); 
    //        httpConn.setDoInput(true); 
    //        httpConn.setDoOutput(true); 
    //        httpConn.setUseCaches(false); 
    //        httpConn.setRequestMethod("POST"); 

    //        httpConn.setRequestProperty("Content-Type", 
    //                                    "application/x-www-form-urlencoded"); 

    pmethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    //        httpConn.setRequestProperty("Content-Length", request.length()+""); 
    //        httpConn.connect(); 

    /* Send POST output. */
    //        DataOutputStream printout = new 
    //            DataOutputStream(httpConn.getOutputStream()); 
    //        printout.writeBytes(request); 
    //        printout.flush (); 
    pmethod.setRequestBody(request);

    HttpClient client = new HttpClient();
    int status = client.executeMethod(pmethod);

    StringBuffer response = new StringBuffer();

    InputStreamReader is = new InputStreamReader(pmethod.getResponseBodyAsStream());
    char[] buf = new char[1024];
    int len;
    while ((len = is.read(buf, 0, 1024)) > 0) {
        response.append(buf, 0, len);
    }

    /* Get response data. */
    //        InputStreamReader isReader; 
    //        StringBuffer response = new StringBuffer(16384); 
    //        if(httpConn.getResponseCode() != HttpURLConnection.HTTP_OK){ 
    //      System.out.println(httpConn.getResponseCode()); 
    //      System.out.println(httpConn.getResponseMessage()); 
    /* Read the SOAP Fault message. */
    //            isReader = new InputStreamReader(httpConn.getErrorStream()); 
    //        } else { 
    /* Read the response message, which can also be a SOAP Fault 
     * message. */
    //            isReader = new InputStreamReader(httpConn.getInputStream()); 
    //        } 
    //        int c; 
    //        while( (c = isReader.read()) >= 0) response.append((char)c); 
    //        isReader.close(); 
    //        printout.close();
    return response;
}

From source file:com.neuron.trafikanten.HelperFunctions.java

public static String InputStreamToString(InputStream stream) throws IOException {
    //long perfSTART = System.currentTimeMillis();

    final InputStreamReader input = new InputStreamReader(stream, "UTF-8");
    final char[] buffer = new char[8192];
    final StringBuilder output = new StringBuilder(8192);
    try {/*from   w  w w.  ja v  a2  s. c o m*/
        for (int read = input.read(buffer, 0, buffer.length); read != -1; read = input.read(buffer, 0,
                buffer.length)) {
            output.append(buffer, 0, read);
        }
    } catch (IOException e) {
    }
    //Log.i(TAG,"PERF : Downloading web request took " + ((System.currentTimeMillis() - perfSTART)) + "ms");
    return output.toString();
}

From source file:org.lucasr.layoutsamples.util.RawResource.java

public static JSONArray getAsJSON(Context context, int id) throws IOException {
    InputStreamReader reader = null;

    try {//  ww  w.j av a  2 s . c om
        final Resources res = context.getResources();
        final InputStream is = res.openRawResource(id);
        if (is == null) {
            return null;
        }

        reader = new InputStreamReader(is);

        final char[] buffer = new char[1024];
        final StringWriter s = new StringWriter();

        int n;
        while ((n = reader.read(buffer, 0, buffer.length)) != -1) {
            s.write(buffer, 0, n);
        }

        return new JSONArray(s.toString());
    } catch (JSONException e) {
        return null;
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:org.guzz.util.FileUtil.java

/**
 * ???//from   w  ww .j  a v a  2  s . c o m
 * 
 * @param is ???
 * @param encoding ?
 */
public static String readText(InputStream is, String encoding) throws IOException {
    StringBuffer sb = new StringBuffer(8192);
    InputStreamReader isr = null;

    try {
        isr = new InputStreamReader(is, encoding);

        char buff[] = new char[4096];
        int length;

        while ((length = isr.read(buff, 0, 4096)) != -1) {
            if (length > 0) {
                sb.append(buff, 0, length);
            }
        }

        return sb.toString();
    } catch (Exception e) {
        return null;
    } finally {
        CloseUtil.close(isr);
        CloseUtil.close(is);
    }
}

From source file:IORoutines.java

public static String loadAsText(InputStream in, String encoding, int bufferSize) throws IOException {
    InputStreamReader reader = new InputStreamReader(in, encoding);
    char[] buffer = new char[bufferSize];
    int offset = 0;
    for (;;) {//  w w w.j a  v a2 s .c  o m
        int remain = buffer.length - offset;
        if (remain <= 0) {
            char[] newBuffer = new char[buffer.length * 2];
            System.arraycopy(buffer, 0, newBuffer, 0, offset);
            buffer = newBuffer;
            remain = buffer.length - offset;
        }
        int numRead = reader.read(buffer, offset, remain);
        if (numRead == -1) {
            break;
        }
        offset += numRead;
    }
    return new String(buffer, 0, offset);
}

From source file:org.rapidandroid.ApplicationGlobals.java

/**
 * //from  w  w w  . ja  v a 2  s.  c  o m
 */
public static JSONObject loadSettingsFromFile(Context context) {
    FileInputStream fin = null;
    InputStreamReader irdr = null;
    JSONObject readobject = null;
    try {

        fin = context.openFileInput(SETTINGS_FILE);

        irdr = new InputStreamReader(fin); // promote

        int size = (int) fin.getChannel().size();
        char[] data = new char[size]; // allocate char array of right
        // size
        irdr.read(data, 0, size); // read into char array
        irdr.close();

        String contents = new String(data);
        readobject = new JSONObject(contents);

        if (!readobject.has(KEY_ACTIVE_ALL)) {
            //dmyung hack to keep compatability with new version
            readobject.put(KEY_ACTIVE_ALL, false);
        }

        if (!readobject.has(KEY_ACTIVE_LOGGING)) {
            //dmyung hack to keep compatability with new version
            readobject.put(KEY_ACTIVE_LOGGING, false);
        }

        //         mParseCheckbox.setChecked(readobject.getBoolean(KEY_PARSE_REPLY));
        //         mParseReplyText.setText(readobject.getString(KEY_PARSE_REPLY_TEXT));
        //         mNoparseCheckBox.setChecked(readobject.getBoolean(KEY_FAILED_REPLY));
        //         mNoparseReplyText.setText(readobject.getString(KEY_FAILED_REPLY_TEXT));

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            if (irdr != null) {
                irdr.close();
            }
            if (fin != null) {
                fin.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return readobject;
}

From source file:gov.nasa.ensemble.dictionary.nddl.ModelGenerator.java

private static StringBuilder readModelFile(File file) throws IOException {
    StringBuilder result = new StringBuilder();
    FileInputStream fis = null;/*from  w w w  .  jav  a  2  s  . c om*/
    InputStreamReader reader = null;
    int size;
    try {
        char[] buffer = new char[1024];
        fis = new FileInputStream(file);

        FileWriter filewriter = new FileWriter("out");
        String encname = filewriter.getEncoding();
        filewriter.close();

        Charset cs = Charset.forName(encname);
        CharsetDecoder csd = cs.newDecoder();
        csd.onMalformedInput(CodingErrorAction.REPLACE);

        reader = new InputStreamReader(fis, csd);
        while ((size = reader.read(buffer, 0, 1024)) > 0) {
            result.append(buffer, 0, size);
        }
    } catch (Exception e) {
        // System.out.println(encoding);
        e.printStackTrace();
    } finally {
        if (fis != null) {
            fis.close();
        }

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

    return result;
}