Example usage for java.io OutputStream close

List of usage examples for java.io OutputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this output stream and releases any system resources associated with this stream.

Usage

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

/**
 * Unconditionally close an <code>OutputStream</code>.
 * <p>/*from   w  w w  .j  a  va 2  s .c  o  m*/
 * Equivalent to {@link OutputStream#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 OutputStream to close, may be null or already closed
 */
public static void closeQuietly(OutputStream x) {
    if (x == null) {
        return;
    }
    try {
        x.close();
    } catch (IOException e) {
        interruptIfNecessary(e);
    }
}

From source file:Main.java

public static void copy(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;/* ww  w  .j a va2  s.  c  o m*/
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

From source file:deodex.tools.TarGzUtils.java

/** Untar an input file into an output file.
        /*  w w  w.  jav a  2 s.c  o m*/
 * The output file is created in the output folder, having the same name
 * as the input file, minus the '.tar' extension. 
 * 
 * @param inputFile     the input .tar file
 * @param outputDir     the output directory file. 
 * @throws IOException 
 * @throws FileNotFoundException
 *  
 * @return  The {@link List} of {@link File}s with the untared content.
 * @throws ArchiveException 
 */
public static List<File> unTar(final File inputFile, final File outputDir)
        throws FileNotFoundException, IOException, ArchiveException {

    final List<File> untaredFiles = new LinkedList<File>();
    final InputStream is = new FileInputStream(inputFile);
    final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
        final File outputFile = new File(outputDir, entry.getName());
        if (entry.isDirectory()) {
            Logger.appendLog("Attempting to write output directory . " + outputFile.getAbsolutePath());
            if (!outputFile.exists()) {
                Logger.appendLog("Attempting to create output directory ." + outputFile.getAbsolutePath());
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException(
                            String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                }
            }
        } else {
            outputFile.getParentFile().mkdirs();
            final OutputStream outputFileStream = new FileOutputStream(outputFile);
            IOUtils.copy(debInputStream, outputFileStream);
            outputFileStream.close();
        }
        untaredFiles.add(outputFile);
    }
    debInputStream.close();

    return untaredFiles;
}

From source file:Main.java

public static void fileCopy(String srcStr, String dstStr) throws IOException {
    File src = new File(srcStr);
    File dst = new File(dstStr);
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;/*from  ww  w  . j  av a 2s  .  c  o  m*/
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

From source file:com.bytelightning.opensource.pokerface.ProxySpecificTest.java

/**
 * Context handler for the SunHttpServer
 *//*from ww  w.j a  va  2  s.com*/
protected static void OnRemoteTargetRequest(HttpExchange exchange) {
    Headers rspHdrs = exchange.getResponseHeaders();
    rspHdrs.set("Date", Utils.GetHTTPDateFormater().format(new Date()));
    rspHdrs.set("Content-Type", "text/html");
    try {
        exchange.sendResponseHeaders(200, 0);
        OutputStream out = exchange.getResponseBody();
        out.write("<htm><head><title>TEST</title></head><body>TEST</body></html>".getBytes("utf-8"));
        out.close();
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
}

From source file:com.google.cloud.public_datasets.nexrad2.GcsUntar.java

private static File[] unTar(final File inputFile, final File outputDir)
        throws FileNotFoundException, IOException, ArchiveException {
    // from//from  w w  w  . jav a  2 s.c o  m
    // http://stackoverflow.com/questions/315618/how-do-i-extract-a-tar-file-in-java/7556307#7556307
    log.info("tar xf " + inputFile + " to " + outputDir);
    final List<File> untaredFiles = new ArrayList<File>();
    final InputStream is = new FileInputStream(inputFile);
    final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
        final File outputFile = new File(outputDir, entry.getName());
        if (entry.isDirectory()) {
            if (!outputFile.exists()) {
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException(
                            String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                }
            }
        } else {
            final OutputStream outputFileStream = new FileOutputStream(outputFile);
            IOUtils.copy(debInputStream, outputFileStream);
            outputFileStream.close();
        }
        untaredFiles.add(outputFile);
    }
    debInputStream.close();

    return untaredFiles.toArray(new File[0]);
}

From source file:com.zenome.bundlebus.Util.java

public static void writeStringToFile(@NonNull String aAbsolutePath, @NonNull String aData) throws IOException {
    final OutputStream is = new FileOutputStream(aAbsolutePath);
    is.write(aData.getBytes());/*ww w. ja v  a2  s.c o m*/
    is.close();
}

From source file:com.github.terma.m.node.Node.java

private static void send(final String serverHost, final int serverPort, final String context,
        final List<Event> events) throws IOException {
    final HttpURLConnection connection = (HttpURLConnection) new URL("http", serverHost, serverPort,
            context + "/node").openConnection();
    connection.setDoOutput(true);//  w  ww .ja v  a  2s .  c om
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "text/json");
    connection.setRequestProperty("charset", "utf-8");
    connection.setUseCaches(false);
    connection.setInstanceFollowRedirects(false);
    connection.connect();
    OutputStream outputStream = connection.getOutputStream();
    outputStream.write(new Gson().toJson(events).getBytes());
    connection.getInputStream().read();
    outputStream.close();
}

From source file:Main.java

private static void copyAssetFile(AssetManager assetManager, String assetFilePath, String destinationFilePath)
        throws IOException {
    InputStream in = assetManager.open(assetFilePath);
    OutputStream out = new FileOutputStream(destinationFilePath);

    // Transfer bytes from in to out
    byte[] buf = new byte[8192];
    int len;/*from  w  ww.  j a  v  a2s .co m*/
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }

    in.close();
    out.close();
}

From source file:com.aqnote.shared.cryptology.asymmetric.dsa.DSAKeyPairGenTest.java

public static void generator() throws NoSuchAlgorithmException, FileNotFoundException, IOException {
    KeyPairGenerator keygen = KeyPairGenerator.getInstance(ALGORITHM);

    // ???//from  w w  w. j  a v a 2  s .c  om
    SecureRandom secrand = new SecureRandom();
    secrand.setSeed(seed); // ??
    // ??, ??keysize ?. 512  1024  64 ?
    keygen.initialize(512, secrand);
    // ?pubkey?prikey
    KeyPair keys = keygen.generateKeyPair(); // ?
    PublicKey pubkey = keys.getPublic();
    PrivateKey prikey = keys.getPrivate();

    byte[] pubkeyByteArray = Base64.encodeBase64(pubkey.getEncoded());
    OutputStream os = new FileOutputStream(new File(PUBKEY_FILE_NAME));
    ByteArrayInputStream bais = new ByteArrayInputStream(pubkeyByteArray);
    StreamUtil.io(bais, os);
    bais.close();
    os.close();

    byte[] prikeyByteArray = Base64.encodeBase64(prikey.getEncoded());
    os = new FileOutputStream(new File(PRIKEY_FILE_NAME));
    bais = new ByteArrayInputStream(prikeyByteArray);
    StreamUtil.io(bais, os);
    bais.close();
    os.close();
}