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:com.shadwelldacunha.byteswipe.core.Utilities.java

public static void copyResource(String resource, String destination) throws IOException {
    ClassLoader classLoader = Utilities.class.getClassLoader();
    InputStream resStreamIn = classLoader.getResourceAsStream(resource);
    File resDestFile = new File(destination);
    OutputStream resStreamOut = new FileOutputStream(resDestFile);
    int readBytes;
    byte[] buffer = new byte[1024];
    while ((readBytes = resStreamIn.read(buffer)) > 0) {
        resStreamOut.write(buffer, 0, readBytes);
    }//from w w w.  j a v a2 s.  c  o m
    resStreamIn.close();
    resStreamOut.close();
}

From source file:com.evilisn.DAO.CertMapper.java

private static void closeOutputStream(OutputStream out) {

    try {/*from   w  ww.  j a  v a 2  s .co  m*/

        if (out != null)

            out.close();

    } catch (IOException e) {

        e.printStackTrace();

    }

}

From source file:org.apiwatch.util.IO.java

public static void putAPIData(APIScope scope, String format, String encoding, String location, String username,
        String password) throws SerializationError, IOException, HttpException {
    if (URL_RX.matcher(location).matches()) {
        DefaultHttpClient client = new DefaultHttpClient();
        if (username != null && password != null) {
            client.getCredentialsProvider().setCredentials(new AuthScope(null, -1),
                    new UsernamePasswordCredentials(username, password));
        }//  w w  w .ja v a2s  . co  m
        HttpPost req = new HttpPost(location);
        StringWriter writer = new StringWriter();
        Serializers.dumpAPIScope(scope, writer, format);
        HttpEntity entity = new StringEntity(writer.toString(), encoding);
        req.setEntity(entity);
        req.setHeader("content-type", format);
        req.setHeader("content-encoding", encoding);
        HttpResponse response = client.execute(req);
        client.getConnectionManager().shutdown();
        if (response.getStatusLine().getStatusCode() >= 400) {
            throw new HttpException(response.getStatusLine().getReasonPhrase());
        }
        LOGGER.info("Sent results to URL: " + location);
    } else {
        File dir = new File(location);
        dir.mkdirs();
        File file = new File(dir, "api." + format);
        OutputStream out = new FileOutputStream(file);
        Writer writer = new OutputStreamWriter(out, encoding);
        Serializers.dumpAPIScope(scope, writer, format);
        writer.flush();
        writer.close();
        out.close();
        LOGGER.info("Wrote results to file: " + file);
    }
}

From source file:com.ibm.watson.developer_cloud.professor_languo.ingestion.indexing.StackExchangeThreadSerializer.java

/**
 * Serialize a StackExchangeThread into a binary file
 * /*ww w  . java2s.co m*/
 * @param threadToSerialize - The StackExchangeThread to be serialized
 * @return the path(relative to the resource folder) of the serialized binary file
 * @throws IngestionException
 */
public static String serializeThreadToBinFile(StackExchangeThread threadToSerialize, String dirPath)
        throws IngestionException {
    String binFileName = threadToSerialize.getId() + StackExchangeConstants.BIN_FILE_SUFFIX;
    try {
        String binFilePath = dirPath + binFileName;
        File serFile = new File(binFilePath);
        if (serFile.getParentFile() != null)
            serFile.getParentFile().mkdirs();
        if (!serFile.exists())
            serFile.createNewFile();
        OutputStream binOut = new FileOutputStream(serFile);
        ObjectOutputStream out = new ObjectOutputStream(binOut);
        out.writeObject(threadToSerialize);
        out.close();
        binOut.close();
    } catch (IOException e) {
        throw new IngestionException(e);
    }
    return binFileName;
}

From source file:eu.swiec.bearballin.common.io.FileIO.java

@Deprecated
public static void writeStringtoFileSource(String fileName, String stringToWrite, String encoding)
        throws IOException {

    File outputFile = new File(fileName);

    OutputStream outStream = new FileOutputStream(outputFile);
    OutputStreamWriter osw = new OutputStreamWriter(outStream, encoding);

    osw.write(stringToWrite);/*from w  w w.java 2  s . com*/
    osw.close();
    outStream.close();
}

From source file:eu.swiec.bearballin.common.io.FileIO.java

@Deprecated
public static void writePageSource(String fileName, String pageSource) throws IOException {
    File pageFile = new File(fileName);
    if (pageFile.createNewFile()) {
        OutputStream outStrem = new FileOutputStream(pageFile);
        outStrem.write(pageSource.getBytes("UTF-16"));
        // outStrem.write(pageSource.getBytes(), 0, pageSource.length());
        outStrem.close();
    }/*from  ww w  .jav  a  2 s  . c  o  m*/

}

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

public static List<File> unTar(@NonNull final File tarFile, @NonNull final File outputDir)
        throws FileNotFoundException, IOException, ArchiveException {
    //        Log.d(TAG, "tar filename : " + tarFile);
    //        Log.d(TAG, "output folder : " + outputDir);

    final List<File> untaredFiles = new LinkedList<File>();
    final InputStream is = new FileInputStream(tarFile);
    final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream("tar", is);

    TarArchiveEntry entry = null;/*w w  w.  j a  v a2  s. c om*/
    while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
        String name = entry.getName();
        if (name.startsWith("./")) {
            name = entry.getName().substring(2);
        }

        final File outputFile = new File(outputDir, name);
        if (entry.isDirectory()) {
            //                Log.d(TAG, "Attempting to write output directory " + outputFile.getAbsolutePath());
            if (!outputFile.exists()) {
                Log.d(TAG, "Attempting to create output directory " + outputFile.getAbsolutePath());
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException(
                            "Couldn't create directory " + outputFile.getAbsolutePath());
                }
            }
        } else {
            //                Log.d(TAG, "Creating output file " + outputFile.getAbsolutePath());
            final OutputStream outputFileStream = new FileOutputStream(outputFile);
            Log.d(TAG, "IOUtils.copy : " + IOUtils.copy(debInputStream, outputFileStream));
            outputFileStream.close();
        }

        //            Log.d(TAG, "Filename : " + outputFile);
        //            Log.d(TAG, "is exist : " + outputFile.exists());
        untaredFiles.add(outputFile);
    }
    debInputStream.close();

    return untaredFiles;
}

From source file:com.wso2telco.services.bw.FileUtil.java

public static void copy(String src, String dst) throws IOException {

    String fileName = src.substring(src.lastIndexOf("/") + 1);

    File fsrc = new File(src);
    File fdst = new File(dst + "/" + fileName);

    InputStream in = new FileInputStream(fsrc);
    OutputStream out = new FileOutputStream(fdst);

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

From source file:Main.java

static public boolean copyFileTo(Context c, String orifile, String desfile) throws IOException {
    InputStream myInput;//  ww w .  j a  va 2  s.  c  om
    OutputStream myOutput = new FileOutputStream(desfile);
    myInput = c.getAssets().open(orifile);
    byte[] buffer = new byte[1024];
    int length = myInput.read(buffer);
    while (length > 0) {
        myOutput.write(buffer, 0, length);
        length = myInput.read(buffer);
    }

    myOutput.flush();
    myInput.close();
    myOutput.close();

    return true;
}

From source file:org.objectweb.proactive.extensions.timitspmd.util.charts.Utilities.java

/**
 * Exports a JFreeChart to a SVG file.// w  w w . ja  va 2  s  .  c  om
 *
 * @param chart
 *            JFreeChart to export
 * @param bounds
 *            the dimensions of the viewport
 * @param svgFile
 *            the output file.
 * @throws IOException
 *             if writing the svgFile fails.
 */
public static void saveChartAsSVG(JFreeChart chart, Rectangle bounds, File svgFile) throws IOException {
    try {
        Class<?> GDI = Class.forName("org.apache.batik.dom.GenericDOMImplementation");

        // Get a DOMImplementation and create an XML document
        Method getDOMImplementation = GDI.getMethod("getDOMImplementation", new Class<?>[0]);
        DOMImplementation domImpl = (DOMImplementation) getDOMImplementation.invoke(null, new Object[0]);

        org.w3c.dom.Document document = domImpl.createDocument(null, "svg", null);

        // Create an instance of the SVG Generator
        Class<?> SG2D = Class.forName("org.apache.batik.svggen.SVGGraphics2D");
        Method streamMethod = SG2D.getMethod("stream", new Class<?>[] { Writer.class, boolean.class });
        Constructor<?> SG2DConstr = SG2D.getConstructor(new Class<?>[] { org.w3c.dom.Document.class });
        Object svgGenerator = SG2DConstr.newInstance(document);

        // draw the chart in the SVG generator
        chart.draw((Graphics2D) svgGenerator, bounds);

        // Write svg file
        OutputStream outputStream = new FileOutputStream(svgFile);
        Writer out = new OutputStreamWriter(outputStream, "UTF-8");
        streamMethod.invoke(svgGenerator, new Object[] { out, true /* use css */ });
        outputStream.flush();
        outputStream.close();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}