Example usage for java.io InputStream close

List of usage examples for java.io InputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:cz.cas.lib.proarc.authentication.utils.AuthUtils.java

/**
 * Writes the authentication OK status to the HTTP response.
 * @param response response/*from   w ww.ja v  a 2s  .c  o m*/
 * @throws IOException failure
 * @see <a href='http://www.smartclient.com/smartgwt/javadoc/com/smartgwt/client/docs/Relogin.html'>SmartGWT Relogin</a>
 */
public static void setLoginSuccesResponse(HttpServletResponse response) throws IOException {
    response.setStatus(HttpServletResponse.SC_OK);
    response.setContentType(MediaType.TEXT_HTML);
    InputStream res = ProarcAuthFilter.class.getResourceAsStream("loginSuccessMarker.html");
    try {
        IOUtils.copy(res, response.getOutputStream());
        res.close();
    } finally {
        IOUtils.closeQuietly(res);
    }
}

From source file:Main.java

public static String requestData(String address) throws Exception {
    URL url = new URL(address);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setConnectTimeout(5000);/* w  w  w  . j a  va 2  s.c  o  m*/
    connection.setReadTimeout(5000);
    String data = null;
    InputStream is = null;
    if (connection.getResponseCode() == 200) {
        is = connection.getInputStream();
        data = readFromStream(is);
    }
    if (is != null) {
        is.close();
    }
    return data;
}

From source file:Main.java

public static String readTextFile(File file) throws IOException {
    String text = null;/* www  . ja va  2s. c  o m*/
    InputStream is = null;
    try {
        is = new FileInputStream(file);
        text = readTextInputStream(is);
        ;
    } finally {
        if (is != null) {
            is.close();
        }
    }
    return text;
}

From source file:Main.java

public static String requestData(String address) throws IOException {
    URL url = new URL(address);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setConnectTimeout(5000);/*w ww . j av a2 s  .co m*/
    connection.setReadTimeout(5000);
    String data = null;
    InputStream is = null;
    if (connection.getResponseCode() == 200) {
        is = connection.getInputStream();
        data = readFromStream(is);
    }
    if (is != null) {
        is.close();
    }
    return data;
}

From source file:cz.cas.lib.proarc.authentication.utils.AuthUtils.java

/**
 * Writes the authentication required status to the HTTP response.
 * @param response response/* w  w w  . j a  va  2 s.c o m*/
 * @throws IOException failure
 * @see #HEADER_AUTHENTICATE_TYPE
 * @see <a href='http://www.smartclient.com/smartgwt/javadoc/com/smartgwt/client/docs/Relogin.html'>SmartGWT Relogin</a>
 */
public static void setLoginRequiredResponse(HttpServletResponse response) throws IOException {
    response.setHeader(HEADER_AUTHENTICATE_TYPE, Authenticators.getInstance().getLoginType());
    response.setStatus(HttpServletResponse.SC_FORBIDDEN);
    response.setContentType(MediaType.TEXT_HTML);
    InputStream res = ProarcAuthFilter.class.getResourceAsStream("loginRequiredMarker.html");
    try {
        IOUtils.copy(res, response.getOutputStream());
        res.close();
    } finally {
        IOUtils.closeQuietly(res);
    }
}

From source file:net.saga.sync.gameserver.portal.CreatePlayer.java

public static Player createPlayer(HttpServletRequest req) throws UnsupportedEncodingException {
    SkeletonKeySession session = (SkeletonKeySession) req.getAttribute(SkeletonKeySession.class.getName());

    HttpClient client = new HttpClientBuilder().trustStore(session.getMetadata().getTruststore())
            .hostnameVerification(HttpClientBuilder.HostnameVerificationPolicy.ANY).build();
    try {/*  w ww.ja va 2s.  co  m*/
        HttpPost post = new HttpPost("https://localhost:8443/gameserver-database/player");
        post.addHeader("Authorization", "Bearer " + session.getTokenString());
        post.setEntity(new StringEntity("{}"));
        try {
            HttpResponse response = client.execute(post);
            if (response.getStatusLine().getStatusCode() != 200) {
                throw new RuntimeException("" + response.getStatusLine().getStatusCode());
            }
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            try {
                return JsonSerialization.readValue(is, Player.class);
            } finally {
                is.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
}

From source file:com.capitaltg.bbcodeguard.StringUtils.java

public static String readResourceAndUpdateText(String resourceName, Map<String, String> values)
        throws IOException {
    InputStream inputStream = StringUtils.class.getClassLoader().getResourceAsStream(resourceName);
    StringWriter writer = new StringWriter();
    IOUtils.copy(inputStream, writer);/*from   w w  w.  j ava  2 s.c o  m*/
    inputStream.close();
    return StringUtils.replaceAll(writer.toString(), values);
}

From source file:com.richtodd.android.repository.JSONUtility.java

public static JSONObject loadJSONObject(File file) throws RepositoryException {
    try {/*from ww w  .  jav  a 2  s .c o  m*/
        InputStream in = new FileInputStream(file);
        try {
            return loadJSONObject(in);
        } finally {
            in.close();
        }
    } catch (FileNotFoundException ex) {
        throw new RepositoryException(ex);
    } catch (IOException ex) {
        throw new RepositoryException(ex);
    }
}

From source file:Main.java

public static Bitmap loadBitmap(Context mContext, int id) {
    InputStream is = mContext.getResources().openRawResource(id);
    Bitmap bitmap = null;/*from w w  w  .  j a va 2 s  . c om*/
    try {
        bitmap = BitmapFactory.decodeStream(is);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            // Ignore.
        }
    }
    return bitmap;
}

From source file:com.ibm.bi.dml.runtime.io.IOUtilFunctions.java

/**
 * //  w w  w.  j  ava  2 s. c  o m
 * @param is
 */
public static void closeSilently(InputStream is) {
    try {
        if (is != null)
            is.close();
    } catch (Exception ex) {
        LOG.error("Failed to close input stream.", ex);
    }
}