Example usage for java.io InputStreamReader close

List of usage examples for java.io InputStreamReader close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:Main.java

public static JSONObject getAssetsJson(Context context, String jsonName) {
    try {//  w  ww  .ja v a  2 s  . co m
        InputStreamReader inputStreamReader = new InputStreamReader(context.getAssets().open(jsonName),
                "UTF-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line;
        StringBuilder stringBuilder = new StringBuilder();
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line);
        }
        bufferedReader.close();
        inputStreamReader.close();
        return new JSONObject(stringBuilder.toString());
    } catch (IOException | JSONException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String readInputStream(InputStream inputStream) throws IOException {
    InputStreamReader inputStreamReader = null;
    try {/*from  w w w .java  2 s  . c  o m*/
        inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        char[] buffer = new char[512];
        StringBuilder str = new StringBuilder();
        int i = 0;
        while ((i = inputStreamReader.read(buffer)) != -1)
            str.append(buffer, 0, i);
        return str.toString();
    } finally {
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
    }
}

From source file:org.cmuchimps.gort.modules.webinfoservice.AppEngineUpload.java

private static String uploadBlobstoreDataNoRetry(String url, String filename, String mime, byte[] data) {
    if (url == null || url.length() <= 0) {
        return null;
    }//from ww  w  . j  a  v a  2 s  . c  o m

    if (data == null || data.length <= 0) {
        return null;
    }

    HttpClient httpClient = new DefaultHttpClient();

    HttpPost httpPost = new HttpPost(url);
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    entity.addPart("data", new ByteArrayBody(data, mime, filename));

    httpPost.setEntity(entity);

    try {
        HttpResponse response = httpClient.execute(httpPost);

        System.out.println("Blob upload status code: " + response.getStatusLine().getStatusCode());

        /*
        //http://grinder.sourceforge.net/g3/script-javadoc/HTTPClient/HTTPResponse.html
        // 2xx - success
        if (response.getStatusLine().getStatusCode() / 100 != 2) {
            return null;
        }
        */

        InputStreamReader isr = new InputStreamReader(response.getEntity().getContent());
        BufferedReader br = new BufferedReader(isr);

        String blobKey = br.readLine();

        blobKey = (blobKey != null) ? blobKey.trim() : null;

        br.close();
        isr.close();

        if (blobKey != null && blobKey.length() > 0) {
            return String.format("%s%s", MTURKSERVER_BLOB_DOWNLOAD_URL, blobKey);
        } else {
            return null;
        }

    } catch (ClientProtocolException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.swisscom.safeconnect.backend.PlumberTask.java

private static String inputStreamToString(InputStream is) throws IOException {
    StringBuilder sb = new StringBuilder();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader rd = new BufferedReader(isr);

    try {//ww  w  .j av a  2 s .  c om
        String line;
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
    } finally {
        rd.close();
        isr.close();
        is.close();
    }

    return sb.toString();
}

From source file:com.uber.hoodie.hive.util.TestUtil.java

public static void generateParquetData(Path filePath, String schemaFile) throws IOException {
    MessageType schema = readSchema(schemaFile);
    CsvParquetWriter writer = new CsvParquetWriter(filePath, schema);

    BufferedReader br = new BufferedReader(
            new InputStreamReader(TestUtil.class.getResourceAsStream(getDataFile(schemaFile))));
    String line;//from w w  w  .j a v a2  s.  c  o  m
    try {
        while ((line = br.readLine()) != null) {
            String[] fields = line.split(Pattern.quote(CSV_DELIMITER));
            writer.write(Arrays.asList(fields));
        }
        writer.close();
    } finally {
        br.close();
    }

    InputStreamReader io = null;
    FSDataOutputStream hdfsPath = null;
    try {
        io = new FileReader(filePath.toString());
        hdfsPath = fileSystem.create(filePath);
        IOUtils.copy(io, hdfsPath);
    } finally {
        if (io != null) {
            io.close();
        }
        if (hdfsPath != null) {
            hdfsPath.close();
        }
    }
}

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  w w w.j a  va2  s.  c om*/
    }

    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:com.xxg.jdeploy.util.FileUtil.java

/**
 * //  ww w  .  jav  a2 s. c  o m
 * @param fileName
 * @return
 * @throws FileNotFoundException
 * @throws IOException
 */
public static String getFileEncoding(String fileName) throws IOException {
    InputStreamReader read = null;
    String encoding = "";
    try {
        read = new InputStreamReader(new FileInputStream(new File(fileName)));
        encoding = read.getEncoding();
        read.close();
    } catch (IOException e) {
        throw e;
    } finally {
        if (read != null) {
            try {
                read.close();
            } catch (IOException e) {
                throw e;
            }
        }
    }
    return encoding;
}

From source file:Main.java

public static String toConvertString(InputStream is) {
    StringBuffer res = new StringBuffer();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader read = new BufferedReader(isr);
    try {/* ww  w .  j av a 2 s  .  c  om*/
        String line;
        line = read.readLine();
        while (line != null) {
            res.append(line + "<br>");
            line = read.readLine();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (null != isr) {
                isr.close();
                isr.close();
            }
            if (null != read) {
                read.close();
                read = null;
            }
            if (null != is) {
                is.close();
                is = null;
            }
        } catch (IOException e) {
        }
    }
    return res.toString();
}

From source file:com.jamesgiang.aussnowcam.Utils.java

public static String ReadSettings(Context context, String file) throws IOException {
    FileInputStream fIn = null;//from   w  w w  . j  a  va2 s.  c o  m
    InputStreamReader isr = null;
    String data = null;
    fIn = context.openFileInput(file);
    isr = new InputStreamReader(fIn);
    char[] inputBuffer = new char[fIn.available()];
    isr.read(inputBuffer);
    data = new String(inputBuffer);
    isr.close();
    fIn.close();
    return data;
}

From source file:org.apache.roller.planet.config.PlanetRuntimeConfig.java

/**
 * Get the runtime configuration definitions XML file as a string.
 *
 * This is basically a convenience method for accessing this file.
 * The file itself contains meta-data about what configuration
 * properties we change at runtime via the UI and how to setup
 * the display for editing those properties.
 */// w  w  w . ja  v a  2 s.c  om
public static String getRuntimeConfigDefsAsString() {

    log.debug("Trying to load runtime config defs file");

    try {
        InputStreamReader reader = new InputStreamReader(
                PlanetConfig.class.getResourceAsStream(runtime_config));
        StringWriter configString = new StringWriter();

        char[] buf = new char[8196];
        int length = 0;
        while ((length = reader.read(buf)) > 0)
            configString.write(buf, 0, length);

        reader.close();

        return configString.toString();
    } catch (Exception e) {
        log.error("Error loading runtime config defs file", e);
    }

    return "";
}