Example usage for java.io FilterOutputStream close

List of usage examples for java.io FilterOutputStream close

Introduction

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

Prototype

@Override
public void close() throws IOException 

Source Link

Document

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

Usage

From source file:Main.java

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

    // create input streams
    OutputStream os = new FileOutputStream("C://test.txt");
    FilterOutputStream fos = new FilterOutputStream(os);

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

    // writes byte to the output stream
    fos.write(65);//w  ww.  j av  a  2s  .  c  o  m

}

From source file:Main.java

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

    OutputStream os = new FileOutputStream("C://test.txt");
    FilterOutputStream fos = new FilterOutputStream(os);

    fos.write(65);/*from   w  w w .  j a  va  2s  .  c om*/

    // forces byte contents to written out to the stream
    fos.flush();

    // create output streams
    FileInputStream fis = new FileInputStream("C://test.txt");

    // read byte
    int i = fis.read();

    // convert integer to characters
    char c = (char) i;

    System.out.print("Character read: " + c);
    fos.close();
    fis.close();
}

From source file:Main.java

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

    OutputStream os = new FileOutputStream("C://test.txt");
    FilterOutputStream fos = new FilterOutputStream(os);

    // writes buffer to the output stream
    fos.write(65);/*w  w  w. j a v  a  2 s .c o m*/

    // forces byte contents to written out to the stream
    fos.flush();

    // create input streams
    FileInputStream fis = new FileInputStream("C://test.txt");

    // get byte from the file
    int i = fis.read();

    // convert integer to character
    char c = (char) i;

    System.out.print("Character read: " + c);
    fos.close();
    fis.close();
}

From source file:Main.java

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

    byte[] buffer = { 65, 66, 67, 68, 69 };
    int i = 0;/*from  w  w w  . ja  v a2 s. c  o  m*/

    OutputStream os = new FileOutputStream("C://test.txt");
    FilterOutputStream fos = new FilterOutputStream(os);

    // writes buffer to the output stream
    fos.write(buffer);

    // forces byte contents to written out to the stream
    fos.flush();

    // create input streams
    FileInputStream fis = new FileInputStream("C://test.txt");

    while ((i = fis.read()) != -1) {
        // converts integer to the character
        char c = (char) i;
        System.out.println("Character read: " + c);
    }
    fos.close();
    fis.close();
}

From source file:Main.java

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

    byte[] buffer = { 65, 66, 67, 68, 69 };
    int i = 0;/*from   w w  w.j av  a 2s.c om*/
    OutputStream os = new FileOutputStream("C://test.txt");
    FilterOutputStream fos = new FilterOutputStream(os);

    // writes buffer to the output stream
    fos.write(buffer, 2, 3);

    // forces byte contents to written out to the stream
    fos.flush();

    // create input streams
    FileInputStream fis = new FileInputStream("C://test.txt");

    while ((i = fis.read()) != -1) {
        // converts integer to the character
        char c = (char) i;

        System.out.println("Character read: " + c);
    }
    fos.close();
    fis.close();
}

From source file:sit.web.client.HttpHelper.java

public HTTPResponse doHttpUrlConnectionRequest(URL url, String method, String contentType, byte[] payload,
        String unamePword64) throws MalformedURLException, ProtocolException, IOException, URISyntaxException {

    if (payload == null) { //make sure payload is initialized
        payload = new byte[0];
    }//w  ww  . j av  a  2s  .  c o m

    boolean isHTTPS = url.getProtocol().equalsIgnoreCase("https");
    HttpURLConnection connection;

    if (isHTTPS) {
        connection = (HttpsURLConnection) url.openConnection();
    } else {
        connection = (HttpURLConnection) url.openConnection();
    }

    connection.setRequestMethod(method);
    connection.setRequestProperty("Host", url.getHost());
    connection.setRequestProperty("Content-Type", contentType);
    connection.setRequestProperty("Content-Length", String.valueOf(payload.length));

    if (isHTTPS) {
        connection.setRequestProperty("Authorization", "Basic " + unamePword64);
    }

    Logger.getLogger(HttpHelper.class.getName()).log(Level.FINER,
            "trying to connect:\n" + method + " " + url + "\nhttps:" + isHTTPS + "\nContentType:" + contentType
                    + "\nContent-Length:" + String.valueOf(payload.length));

    connection.setDoInput(true);
    if (payload.length > 0) {
        // open up the output stream of the connection
        connection.setDoOutput(true);
        FilterOutputStream output = new FilterOutputStream(connection.getOutputStream());

        // write out the data
        output.write(payload);
        output.close();
    }

    HTTPResponse response = new HTTPResponse(method + " " + url.toString(), payload, Charset.defaultCharset()); //TODO forward charset ot this method

    response.code = connection.getResponseCode();
    response.message = connection.getResponseMessage();

    Logger.getLogger(HttpHelper.class.getName()).log(Level.FINE,
            "received response: " + response.message + " with code: " + response.code);

    if (response.code != 500) {
        byte[] buffer = new byte[20480];

        ByteBuilder bytes = new ByteBuilder();

        // get ready to read the response from the cgi script
        DataInputStream input = new DataInputStream(connection.getInputStream());
        boolean done = false;
        while (!done) {
            int readBytes = input.read(buffer);
            done = (readBytes == -1);

            if (!done) {
                bytes.append(buffer, readBytes);
            }
        }
        input.close();
        response.reply = bytes.toString(Charset.defaultCharset());
    }
    return response;
}

From source file:com.lizardtech.expresszip.model.Job.java

private void writeZipFile(File baseDir, File archive, List<String> files)
        throws FileNotFoundException, IOException {
    FilterOutputStream out = null;
    ZipOutputStream stream = new ZipOutputStream(new FileOutputStream(archive));
    stream.setLevel(Deflater.DEFAULT_COMPRESSION);
    out = stream;//ww w .j a va  2 s  . co  m

    byte data[] = new byte[18024];

    for (String f : files) {
        logger.info(String.format("Writing %s to ZIP archive %s", f, archive));
        ((ZipOutputStream) out).putNextEntry(new ZipEntry(f));

        BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(baseDir, f)));

        int len = 0;
        while ((len = in.read(data)) > 0) {
            out.write(data, 0, len);
        }

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

    out.close();
}

From source file:org.drugis.addis.util.jaxb.JAXBHandlerTest.java

@Test
public void doNotSerializeInvalidCharsTest() throws JAXBException, ConversionException, IOException {
    DomainImpl domainImpl = new DomainImpl();
    GUIFactory.suppressErrors(true);//from w w w .  j  ava2 s  . c o  m

    List<Character> invalidXMLChars = Main.XMLStreamFilter.getCharacters();
    Drug drug = new Drug(StringUtils.join(invalidXMLChars, " "), "#");
    domainImpl.getDrugs().add(drug);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    FilterOutputStream os = new Main.XMLStreamFilter(out);

    JAXBHandler.marshallAddisData(JAXBConvertor.convertDomainToAddisData(domainImpl), os);

    os.close();

    JAXBHandler.unmarshallAddisData(new ByteArrayInputStream(out.toByteArray()));
    GUIFactory.suppressErrors(false);
}

From source file:org.exist.xquery.value.BinaryValue.java

/**
 * Streams the encoded binary data/* www.ja  va 2s  .  c  o m*/
 */
public void streamTo(OutputStream os) throws IOException {

    //we need to create a safe output stream that cannot be closed
    final OutputStream safeOutputStream = new CloseShieldOutputStream(os);

    //get the encoder
    final FilterOutputStream fos = getBinaryValueType().getEncoder(safeOutputStream);

    //stream with the encoder
    streamBinaryTo(fos);

    //we do have to close the encoders output stream though
    //to ensure that all bytes have been written, this is
    //particularly nessecary for Apache Commons Codec stream encoders
    try {
        fos.close();
    } catch (final IOException ioe) {
        LOG.error("Unable to close stream: " + ioe.getMessage(), ioe);
    }
}

From source file:org.zuinnote.hadoop.office.format.common.writer.msexcel.internal.EncryptedZipEntrySource.java

public void setInputStream(InputStream is) throws IOException {
    this.tmpFile = TempFile.createTempFile("hadoopoffice-protected", ".zip");

    ZipArchiveInputStream zis = new ZipArchiveInputStream(is);
    FileOutputStream fos = new FileOutputStream(tmpFile);
    ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos);
    ZipArchiveEntry ze;// w w  w  .j  a  v  a2  s .  co m
    while ((ze = (ZipArchiveEntry) zis.getNextEntry()) != null) {
        // rewrite zip entries to match the size of the encrypted data (with padding)
        ZipArchiveEntry zeNew = new ZipArchiveEntry(ze.getName());
        zeNew.setComment(ze.getComment());
        zeNew.setExtra(ze.getExtra());
        zeNew.setTime(ze.getTime());
        zos.putArchiveEntry(zeNew);
        FilterOutputStream fos2 = new FilterOutputStream(zos) {
            // do not close underlyzing ZipOutputStream
            @Override
            public void close() {
            }
        };
        OutputStream nos;
        if (this.ciEncoder != null) { // encrypt if needed
            nos = new CipherOutputStream(fos2, this.ciEncoder);
        } else { // do not encrypt
            nos = fos2;
        }
        IOUtils.copy(zis, nos);
        nos.close();
        if (fos2 != null) {
            fos2.close();
        }
        zos.closeArchiveEntry();

    }
    zos.close();
    fos.close();
    zis.close();
    IOUtils.closeQuietly(is);
    this.zipFile = new ZipFile(this.tmpFile);

}