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:MainClass.java

public static void main(String[] args) throws IOException {

    InputStream in = null;
    try {//from www  . j  a  va 2 s.  c o  m
        URL u = new URL("http://www.java2s.com");
        in = u.openStream();
        for (int c = in.read(); c != -1; c = in.read()) {
            System.out.write(c);
        }
        in.close();
    } catch (MalformedURLException ex) {
        System.err.println("not a URL Java understands.");
    } finally {
        if (in != null)
            in.close();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");

    Statement stmt = conn.createStatement();

    createBlobClobTables(stmt);/* w ww.ja va2s.c  o  m*/

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO BlobClob VALUES(40,?,?)");

    File file = new File("blob.txt");
    FileInputStream fis = new FileInputStream(file);
    pstmt.setBinaryStream(1, fis, (int) file.length());

    file = new File("clob.txt");
    fis = new FileInputStream(file);
    pstmt.setAsciiStream(2, fis, (int) file.length());
    fis.close();

    pstmt.execute();

    ResultSet rs = stmt.executeQuery("SELECT * FROM BlobClob WHERE id = 40");
    rs.next();

    java.sql.Blob blob = rs.getBlob(2);
    java.sql.Clob clob = rs.getClob(3);

    byte blobVal[] = new byte[(int) blob.length()];
    InputStream blobIs = blob.getBinaryStream();
    blobIs.read(blobVal);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bos.write(blobVal);
    blobIs.close();

    char clobVal[] = new char[(int) clob.length()];
    Reader r = clob.getCharacterStream();
    r.read(clobVal);
    StringWriter sw = new StringWriter();
    sw.write(clobVal);

    r.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");

    Statement stmt = conn.createStatement();

    createBlobClobTables(stmt);/*from www  .j  ava2 s .  c  o m*/

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO BlobClob VALUES(40,?,?)");

    File file = new File("blob.txt");
    FileInputStream fis = new FileInputStream(file);
    pstmt.setBinaryStream(1, fis, (int) file.length());

    file = new File("clob.txt");
    fis = new FileInputStream(file);
    pstmt.setAsciiStream(2, fis, (int) file.length());
    fis.close();

    pstmt.execute();

    ResultSet rs = stmt.executeQuery("SELECT * FROM BlobClob WHERE id = 40");
    rs.next();

    java.sql.Blob blob = rs.getBlob(2);
    java.sql.Clob clob = rs.getClob("myClobColumn");

    byte blobVal[] = new byte[(int) blob.length()];
    InputStream blobIs = blob.getBinaryStream();
    blobIs.read(blobVal);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bos.write(blobVal);
    blobIs.close();

    char clobVal[] = new char[(int) clob.length()];
    Reader r = clob.getCharacterStream();
    r.read(clobVal);
    StringWriter sw = new StringWriter();
    sw.write(clobVal);

    r.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) {
    try {//from www .  ja  v a  2  s  .  co m

        OutputStream os = new FileOutputStream("test.txt");

        InputStream is = new FileInputStream("test.txt");

        os.write(70);
        os.write(71);

        for (int i = 0; i < 2; i++) {
            System.out.print((char) is.read());
        }
        os.close();
        is.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:com.bt.aloha.batchtest.WeekendBatchTest.java

public static void main(String[] args) throws Exception {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("batchTestApplicationContext.xml");
    BatchTest batchTest = (BatchTest) ctx.getBean("batchTestBean");
    batchTest.setApplicationContext(ctx);
    batchTest.init();/*w w w. j a v a  2  s. c  o m*/
    batchTest.assignNewCollectionsToBeans();

    while (true) {
        configure(batchTest);
        if (stop)
            break;
        batchTest.run();
        logStatistics(log, batchTest);
        batchTest.reset();
        if (sleepTime > 0) {
            log.info(String.format("sleeping for %d minutes", sleepTime / 60 / 1000));
            Thread.sleep(sleepTime);
        }
    }
    // wait until all things in collection should be ready for housekeeping
    Properties batchProps = new Properties();
    InputStream is = batchTest.getClass().getResourceAsStream("/batchrun.sip.properties");
    batchProps.load(is);
    is.close();
    Thread.sleep(Long.parseLong(batchProps.getProperty("dialog.max.time.to.live", "900000"))
            + Long.parseLong(batchProps.getProperty("housekeeping.interval", "300000")));
    // housekeeping should have happend
    // log out all things still left
    logCollections(batchTest);

    batchTest.destroy();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    int c;/*from   w ww. j  a v a  2  s .  c o m*/
    URL hp = new URL("http://www.internic.net");
    URLConnection hpCon = hp.openConnection();

    long d = hpCon.getDate();
    if (d == 0)
        System.out.println("No date information.");
    else
        System.out.println("Date: " + new Date(d));

    System.out.println("Content-Type: " + hpCon.getContentType());

    d = hpCon.getExpiration();
    if (d == 0)
        System.out.println("No expiration information.");
    else
        System.out.println("Expires: " + new Date(d));

    d = hpCon.getLastModified();
    if (d == 0)
        System.out.println("No last-modified information.");
    else
        System.out.println("Last-Modified: " + new Date(d));

    int len = hpCon.getContentLength();
    if (len == -1)
        System.out.println("Content length unavailable.");
    else
        System.out.println("Content-Length: " + len);

    if (len != 0) {
        InputStream input = hpCon.getInputStream();
        int i = len;
        while (((c = input.read()) != -1)) { // && (--i > 0)) {
            System.out.print((char) c);
        }
        input.close();

    } else {
        System.out.println("No content available.");
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {

    // new input stream created
    InputStream is = new FileInputStream("C://test.txt");

    // invoke available
    int i = is.available();

    // number of bytes available is printed
    System.out.println(i);//from w  w w.ja  v  a 2  s.  c o m

    // releases any system resources associated with the stream
    is.close();

    // throws io exception on available() invocation
    i = is.available();
    System.out.println(i);

}

From source file:CertificateSigner.java

public static void main(String[] args) {
    String ksname = null; // the keystore name
    String alias = null; // the private key alias
    String inname = null; // the input file name
    String outname = null; // the output file name
    for (int i = 0; i < args.length; i += 2) {
        if (args[i].equals("-keystore"))
            ksname = args[i + 1];/*from w  ww . j av a 2 s  .c  o  m*/
        else if (args[i].equals("-alias"))
            alias = args[i + 1];
        else if (args[i].equals("-infile"))
            inname = args[i + 1];
        else if (args[i].equals("-outfile"))
            outname = args[i + 1];
        else
            usage();
    }

    if (ksname == null || alias == null || inname == null || outname == null)
        usage();

    try {
        Console console = System.console();
        if (console == null)
            error("No console");
        char[] password = console.readPassword("Keystore password: ");
        KeyStore store = KeyStore.getInstance("JKS", "SUN");
        InputStream in = new FileInputStream(ksname);
        store.load(in, password);
        Arrays.fill(password, ' ');
        in.close();

        char[] keyPassword = console.readPassword("Key password for %s: ", alias);
        PrivateKey issuerPrivateKey = (PrivateKey) store.getKey(alias, keyPassword);
        Arrays.fill(keyPassword, ' ');

        if (issuerPrivateKey == null)
            error("No such private key");

        in = new FileInputStream(inname);

        CertificateFactory factory = CertificateFactory.getInstance("X.509");

        X509Certificate inCert = (X509Certificate) factory.generateCertificate(in);
        in.close();
        byte[] inCertBytes = inCert.getTBSCertificate();

        X509Certificate issuerCert = (X509Certificate) store.getCertificate(alias);
        Principal issuer = issuerCert.getSubjectDN();
        String issuerSigAlg = issuerCert.getSigAlgName();

        FileOutputStream out = new FileOutputStream(outname);

        X509CertInfo info = new X509CertInfo(inCertBytes);
        info.set(X509CertInfo.ISSUER, new CertificateIssuerName((X500Name) issuer));

        X509CertImpl outCert = new X509CertImpl(info);
        outCert.sign(issuerPrivateKey, issuerSigAlg);
        outCert.derEncode(out);

        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Hex.java

public static void main(String[] args) {
    if (args.length != 3) {
        System.out.println("Usage: HexStrToBin enc/dec <infileName> <outfilename>");
        System.exit(1);// www. j  av a 2  s . c om
    }
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        InputStream in = new FileInputStream(args[1]);
        int len = 0;
        byte buf[] = new byte[1024];
        while ((len = in.read(buf)) > 0)
            os.write(buf, 0, len);
        in.close();
        os.close();

        byte[] data = null;
        if (args[0].equals("dec"))
            data = decode(os.toString());
        else {
            String strData = encode(os.toByteArray());
            data = strData.getBytes();
        }

        FileOutputStream fos = new FileOutputStream(args[2]);
        fos.write(data);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.glaf.activiti.extension.xml.ExtensionWriter.java

public static void main(String[] args) throws Exception {
    java.io.InputStream inputStream = new java.io.FileInputStream(args[0]);
    ExtensionReader reader = new ExtensionReader();
    List<ExtensionEntity> extensions = reader.readTasks(inputStream);
    inputStream.close();
    inputStream = null;/*ww  w  .  j a va2 s  . c  o m*/
    ExtensionWriter writer = new ExtensionWriter();
    Document doc = writer.write(extensions);
    byte[] bytes = Dom4jUtils.getBytesFromPrettyDocument(doc, "GBK");
    System.out.println(new String(bytes));
}