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:org.keycloak.example.oauth.ProductDatabaseClient.java

public static List<String> getProducts(HttpServletRequest request, String accessToken) throws Failure {
    KeycloakSecurityContext session = (KeycloakSecurityContext) request
            .getAttribute(KeycloakSecurityContext.class.getName());

    // The ServletOAuthClient is obtained by getting a context attribute
    // that is set in the Bootstrap context listener in this project.
    // You really should come up with a better way to initialize
    // and obtain the ServletOAuthClient.  I actually suggest downloading the ServletOAuthClient code
    // and take a look how it works. You can also take a look at third-party-cdi example
    ServletOAuthClient oAuthClient = (ServletOAuthClient) request.getServletContext()
            .getAttribute(ServletOAuthClient.class.getName());
    HttpClient client = new DefaultHttpClient();

    HttpGet get = new HttpGet(UriUtils.getOrigin(request.getRequestURL().toString()) + "/database/products");
    get.addHeader("Authorization", "Bearer " + accessToken);
    try {//from w ww.  j  a  va 2s  .  c om
        HttpResponse response = client.execute(get);
        if (response.getStatusLine().getStatusCode() != 200) {
            throw new Failure(response.getStatusLine().getStatusCode());
        }
        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();
        try {
            return JsonSerialization.readValue(is, TypedList.class);
        } finally {
            is.close();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:brut.util.OS.java

public static void cpdir(File src, File dest) throws BrutException {
    dest.mkdirs();/* ww  w.j  a  v  a2 s .c  o  m*/
    File[] files = src.listFiles();
    for (int i = 0; i < files.length; i++) {
        File file = files[i];
        File destFile = new File(dest.getPath() + File.separatorChar + file.getName());
        if (file.isDirectory()) {
            cpdir(file, destFile);
            continue;
        }
        try {
            InputStream in = new FileInputStream(file);
            OutputStream out = new FileOutputStream(destFile);
            IOUtils.copy(in, out);
            in.close();
            out.close();
        } catch (IOException ex) {
            throw new BrutException("Could not copy file: " + file, ex);
        }
    }
}

From source file:com.core.ServerConnector.java

public static String GET(String endpoint, Map<String, String> params, Map<String, String> header)
        throws Exception {
    String result = null;//w w  w  .  j  ava2 s  .  c om
    StringBuilder bodyBuilder = new StringBuilder(endpoint).append("?");
    Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
    // constructs the POST body using the parameters
    while (iterator.hasNext()) {
        Entry<String, String> param = iterator.next();
        bodyBuilder.append(param.getKey()).append('=').append(param.getValue());
        if (iterator.hasNext()) {
            bodyBuilder.append('&');
        }
    }
    String body = bodyBuilder.toString();
    //Log.i("SCANTRANX", "URL:"+body);
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
    HttpConnectionParams.setSoTimeout(httpParameters, 10000);
    HttpClient client = new DefaultHttpClient(httpParameters);
    HttpGet request = new HttpGet(body);

    for (Entry<String, String> mm : header.entrySet()) {
        request.addHeader(mm.getKey(), mm.getValue());
    }
    System.out.println("Raw Request:" + request);
    HttpResponse response = client.execute(request);
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        result = SystemUtil.convertStreamToString(instream);
        instream.close();
    }

    return result;
}

From source file:gui.TesteHackathon.java

public static void enviaImagem() {
    String server = "www.ejec.co";
    int port = 21;
    String user = "ejec";
    String pass = "cPanel2015";

    FTPClient ftpClient = new FTPClient();
    try {/*  w w  w.  j a va  2s. c o  m*/

        ftpClient.connect(server, port);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();

        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        // APPROACH #1: uploads first file using an InputStream
        File firstLocalFile = new File("image.jpg");
        String firstRemoteFile = "public_html/virtualfit/image.jpg";
        InputStream inputStream = new FileInputStream(firstLocalFile);

        System.out.println("Start uploading first file");
        boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
        inputStream.close();
        if (done) {
            System.out.println("The first file is uploaded successfully.");
        }

    } catch (IOException ex) {
        System.out.println("Error: " + ex.getMessage());
        ex.printStackTrace();
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:Main.java

public static String runCommand(String[] command, String workdirectory) {
    String result = "";
    //AbLogUtil.d(AbAppUtil.class, "#"+command);
    try {//w  w  w.  j a  va2s .c o m
        ProcessBuilder builder = new ProcessBuilder(command);
        // set working directory
        if (workdirectory != null) {
            builder.directory(new File(workdirectory));
        }
        builder.redirectErrorStream(true);
        Process process = builder.start();
        InputStream in = process.getInputStream();
        byte[] buffer = new byte[1024];
        while (in.read(buffer) != -1) {
            String str = new String(buffer);
            result = result + str;
        }
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static String convertStreamToString(InputStream is) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;//from  w  w w  . j  av a 2s .  com

    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }

    is.close();

    return sb.toString();
}

From source file:it.unimi.di.big.mg4j.io.IOFactories.java

public static Properties loadProperties(final IOFactory ioFactory, final String filename)
        throws ConfigurationException, IOException {
    final Properties properties = new Properties();
    final InputStream propertiesInputStream = ioFactory.getInputStream(filename);
    properties.load(propertiesInputStream);
    propertiesInputStream.close();
    return properties;
}

From source file:jp.primecloud.auto.ui.util.ViewProperties.java

private static Properties getProperties() {
    Properties properties = null;
    try {/*  www.j  a v a2  s  .c o m*/
        String path = PATH + BASE_NAME + "_" + LANG + "_" + COUNTRY + "_" + VARIANT + EXTENSION;
        File file = new File(path);
        if (!file.exists()) {
            path = PATH + BASE_NAME + "_" + LANG + "_" + COUNTRY + EXTENSION;
            file = new File(path);
        }
        if (!file.exists()) {
            path = PATH + BASE_NAME + "_" + LANG + EXTENSION;
            file = new File(path);
        }
        if (!file.exists()) {
            path = PATH + BASE_NAME + EXTENSION;
            file = new File(path);
        }
        InputStream input = new FileInputStream(file);
        properties = new Properties();
        properties.load(input);
        input.close();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return properties;
}

From source file:Main.java

public static String readFromStream(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int len = 0;//  w  w w. ja v  a 2  s.  co  m
    byte[] buf = new byte[1024];
    if ((len = in.read(buf)) != -1) {
        out.write(buf, 0, len);
    }
    String result = out.toString();
    in.close();
    out.close();
    return result;
}

From source file:com.googlecode.fascinator.common.MimeTypeUtil.java

/**
 * Gets the MIME type for the specified file
 * //from ww w. ja v a 2  s  .c  o m
 * @param file a file
 * @return MIME type
 */
public static String getMimeType(File file) {
    InputStream in = null;
    try {
        in = new FileInputStream(file);
        byte[] inBytes = IOUtil.readBytes(in, identifier.getMinArrayLength());
        in.close();
        return identifier.identify(inBytes, file.getName(), null);
    } catch (IOException ioe) {
        log.warn("Failed to detect MIME type (File): {}", toPrintable(ioe));
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ioe) {
            }
        }
    }
    return DEFAULT_MIME_TYPE;
}