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.CxfRsClient.java

public static List<String> getCustomers(HttpServletRequest req) throws Failure {
    KeycloakSecurityContext session = (KeycloakSecurityContext) req
            .getAttribute(KeycloakSecurityContext.class.getName());

    HttpClient client = new HttpClientBuilder().disableTrustManager().build();
    try {// ww w .  ja  va2  s .com
        HttpGet get = new HttpGet(
                UriUtils.getOrigin(req.getRequestURL().toString()) + "/cxf/customerservice/customers");
        get.addHeader("Authorization", "Bearer " + session.getTokenString());
        try {
            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);
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
}

From source file:keel.Algorithms.Genetic_Rule_Learning.Tan_GP.Main.java

/**
 * <p>/*  w  w  w  .  jav a  2s  .co m*/
 * Configure the execution of the algorithm.
 * 
 * @param jobFilename Name of the KEEL file with properties of the
 *                    execution
 *  </p>                  
 */

private static void configureJob(String jobFilename) {

    Properties props = new Properties();

    try {
        InputStream paramsFile = new FileInputStream(jobFilename);
        props.load(paramsFile);
        paramsFile.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
        System.exit(0);
    }

    // Files training and test
    String trainFile;
    String testFile;
    StringTokenizer tokenizer = new StringTokenizer(props.getProperty("inputData"));
    tokenizer.nextToken();
    trainFile = tokenizer.nextToken();
    trainFile = trainFile.substring(1, trainFile.length() - 1);
    testFile = tokenizer.nextToken();
    testFile = testFile.substring(1, testFile.length() - 1);

    tokenizer = new StringTokenizer(props.getProperty("outputData"));
    String reportTrainFile = tokenizer.nextToken();
    reportTrainFile = reportTrainFile.substring(1, reportTrainFile.length() - 1);
    String reportTestFile = tokenizer.nextToken();
    reportTestFile = reportTestFile.substring(1, reportTestFile.length() - 1);
    String reportRulesFile = tokenizer.nextToken();
    reportRulesFile = reportRulesFile.substring(1, reportRulesFile.length() - 1);

    // Algorithm auxiliar configuration
    XMLConfiguration algConf = new XMLConfiguration();
    algConf.setRootElementName("experiment");
    algConf.addProperty("process[@algorithm-type]",
            "net.sourceforge.jclec.problem.classification.tan.TanAlgorithm");
    algConf.addProperty("process.rand-gen-factory[@type]", "net.sourceforge.jclec.util.random.RanecuFactory");
    algConf.addProperty("process.rand-gen-factory[@seed]", Integer.parseInt(props.getProperty("seed")));
    algConf.addProperty("process.population-size", Integer.parseInt(props.getProperty("population-size")));
    algConf.addProperty("process.max-of-generations", Integer.parseInt(props.getProperty("max-generations")));
    algConf.addProperty("process.max-deriv-size", Integer.parseInt(props.getProperty("max-deriv-size")));
    algConf.addProperty("process.dataset[@type]", "net.sourceforge.jclec.util.dataset.KeelDataSet");
    algConf.addProperty("process.dataset.train-data.file-name", trainFile);
    algConf.addProperty("process.dataset.test-data.file-name", testFile);
    algConf.addProperty("process.species[@type]",
            "net.sourceforge.jclec.problem.classification.tan.TanSyntaxTreeSpecies");
    algConf.addProperty("process.evaluator[@type]",
            "net.sourceforge.jclec.problem.classification.tan.TanEvaluator");
    algConf.addProperty("process.evaluator.w1", Double.parseDouble(props.getProperty("w1")));
    algConf.addProperty("process.evaluator.w2", Double.parseDouble(props.getProperty("w2")));
    algConf.addProperty("process.provider[@type]", "net.sourceforge.jclec.syntaxtree.SyntaxTreeCreator");
    algConf.addProperty("process.parents-selector[@type]", "net.sourceforge.jclec.selector.RouletteSelector");
    algConf.addProperty("process.recombinator[@type]",
            "net.sourceforge.jclec.syntaxtree.SyntaxTreeRecombinator");
    algConf.addProperty("process.recombinator[@rec-prob]", Double.parseDouble(props.getProperty("rec-prob")));
    algConf.addProperty("process.recombinator.base-op[@type]",
            "net.sourceforge.jclec.problem.classification.tan.TanCrossover");
    algConf.addProperty("process.mutator[@type]", "net.sourceforge.jclec.syntaxtree.SyntaxTreeMutator");
    algConf.addProperty("process.mutator[@mut-prob]", Double.parseDouble(props.getProperty("mut-prob")));
    algConf.addProperty("process.mutator.base-op[@type]",
            "net.sourceforge.jclec.problem.classification.tan.TanMutator");
    algConf.addProperty("process.copy-prob", Double.parseDouble(props.getProperty("copy-prob")));
    algConf.addProperty("process.elitist-prob", Double.parseDouble(props.getProperty("elitist-prob")));
    algConf.addProperty("process.support", Double.parseDouble(props.getProperty("support")));
    algConf.addProperty("process.listener[@type]",
            "net.sourceforge.jclec.problem.classification.tan.KeelTanPopulationReport");
    algConf.addProperty("process.listener.report-dir-name", "./");
    algConf.addProperty("process.listener.train-report-file", reportTrainFile);
    algConf.addProperty("process.listener.test-report-file", reportTestFile);
    algConf.addProperty("process.listener.rules-report-file", reportRulesFile);
    algConf.addProperty("process.listener.global-report-name", "resumen");
    algConf.addProperty("process.listener.report-frequency", 50);

    try {
        algConf.save(new File("configure.txt"));
    } catch (ConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    net.sourceforge.jclec.RunExperiment.main(new String[] { "configure.txt" });
}

From source file:convertonline.ConvertOnline.java

/**
 * /*from w  w  w . j  a  va 2  s  .c o  m*/
 * @param url - url request for json
 * @return - JSONObject of the returned query
 * @throws IOException
 * @throws JSONException 
 */
public static JSONObject readFromURL(String url) throws IOException, JSONException {
    String jsonText = "";
    InputStream in = new URL(url).openStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8")));

    String line;
    while ((line = rd.readLine()) != null) {
        jsonText += line;
    }
    in.close();

    return new JSONObject(jsonText);
}

From source file:de.innovationgate.wgpublisher.design.OverlayData.java

public static OverlayData load(FileObject overlayDataFile) throws Exception {
    InputStream in = new BufferedInputStream(overlayDataFile.getContent().getInputStream());
    OverlayData overlayData = OverlayData.read(in);
    in.close();
    return overlayData;

}

From source file:Main.java

public static String stream2StringQuiet(String charset, InputStream inStream) {
    String result = null;/*from   w  w w .j a  v  a2  s. c  o m*/
    try {
        result = stream2String(charset, inStream);
    } catch (IOException e) {
        // nothing to do
    } finally {
        try {
            inStream.close();
        } catch (IOException e) {
            // nothing to do
        } finally {
            inStream = null;
        }
    }
    return result;
}

From source file:Main.java

public static String getStringFromInput(InputStream in) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int c = 0;//from  w  w w  .  j av a 2 s.co  m
    byte[] buffer = new byte[1024];
    try {
        while ((c = in.read(buffer)) != -1) {
            bos.write(buffer, 0, c);
        }
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new String(bos.toByteArray());
}

From source file:Main.java

public static void copyDB(Context context, String fileName) throws IOException {
    String filePath = context.getFilesDir().getAbsolutePath() + "/" + fileName;
    if (new File(filePath).exists()) {
        return;/*from  ww w. j a va 2  s. co  m*/
    }
    FileOutputStream fos = new FileOutputStream(new File(filePath));
    InputStream is = context.getResources().getAssets().open(fileName);
    byte[] buffer = new byte[1024 * 500];
    int count = 0;
    while ((count = is.read(buffer)) > 0) {
        fos.write(buffer, 0, count);
    }
    fos.close();
    is.close();
}

From source file:com.linguamathematica.translate4j.HTTPService.java

private static void close(final InputStream content, final StatusLine statusLine) {
    if (content != null) {
        try {/*  ww w  .j  a  v  a2 s.co m*/
            content.close();
        } catch (final IOException exception) {
            throw new ResponseException("Unable to close response stream", statusLine.getStatusCode(),
                    statusLine.getReasonPhrase());
        }
    }
}

From source file:de.huxhorn.sulky.io.IOUtilities.java

/**
 * Unconditionally close an <code>InputStream</code>.
 * <p>/*from  w  w  w  .j  a v a 2 s. c o  m*/
 * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
 * {@link InterruptedIOException} is handled correctly by {@link #interruptIfNecessary(Throwable)}.
 * This is typically used in finally blocks.
 *
 * @param x  the InputStream to close, may be null or already closed
 */
public static void closeQuietly(InputStream x) {
    if (x == null) {
        return;
    }
    try {
        x.close();
    } catch (IOException e) {
        interruptIfNecessary(e);
    }
}

From source file:Main.java

/**
 * Load an asset from a resource and return the content as byte array.
 *//*from w ww .j av  a 2  s.c  o  m*/
public static byte[] assetAsByteArray(Resources res, String path) throws IOException {
    InputStream is = res.getAssets().open(path);

    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    byte[] temp = new byte[1024];
    int read;

    while ((read = is.read(temp)) > 0) {
        buf.write(temp, 0, read);
    }
    is.close();
    return buf.toByteArray();
}