Search in sources :

Example 16 with FilterOutputStream

use of java.io.FilterOutputStream in project poi by apache.

the class StreamHelper method saveXmlInStream.

/**
	 * Save the document object in the specified output stream.
	 *
	 * @param xmlContent
	 *            The XML document.
	 * @param outStream
	 *            The OutputStream in which the XML document will be written.
	 * @return <b>true</b> if the xml is successfully written in the stream,
	 *         else <b>false</b>.
	 */
public static boolean saveXmlInStream(Document xmlContent, OutputStream outStream) {
    try {
        Transformer trans = getIdentityTransformer();
        Source xmlSource = new DOMSource(xmlContent);
        // prevent close of stream by transformer:
        Result outputTarget = new StreamResult(new FilterOutputStream(outStream) {

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                out.write(b, off, len);
            }

            @Override
            public void close() throws IOException {
                // only flush, don't close!
                out.flush();
            }
        });
        trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty(OutputKeys.STANDALONE, "yes");
        trans.transform(xmlSource, outputTarget);
    } catch (TransformerException e) {
        return false;
    }
    return true;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) IOException(java.io.IOException) FilterOutputStream(java.io.FilterOutputStream) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 17 with FilterOutputStream

use of java.io.FilterOutputStream in project jackrabbit by apache.

the class MemoryFileSystem method getOutputStream.

public OutputStream getOutputStream(String filePath) throws FileSystemException {
    if (isFolder(filePath)) {
        throw new FileSystemException("path denotes folder: " + filePath);
    }
    String folderPath = filePath;
    if (filePath.lastIndexOf(FileSystem.SEPARATOR) > 0) {
        folderPath = filePath.substring(0, filePath.lastIndexOf("/"));
    } else {
        folderPath = "/";
    }
    assertIsFolder(folderPath);
    final MemoryFile file = new MemoryFile();
    entries.put(filePath, file);
    return new FilterOutputStream(new ByteArrayOutputStream()) {

        public void write(byte[] bytes, int off, int len) throws IOException {
            out.write(bytes, off, len);
        }

        public void close() throws IOException {
            out.close();
            file.setData(((ByteArrayOutputStream) out).toByteArray());
        }
    };
}
Also used : FileSystemException(org.apache.jackrabbit.core.fs.FileSystemException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FilterOutputStream(java.io.FilterOutputStream)

Example 18 with FilterOutputStream

use of java.io.FilterOutputStream in project jackrabbit by apache.

the class DatabaseFileSystem method getOutputStream.

/**
     * {@inheritDoc}
     */
public OutputStream getOutputStream(final String filePath) throws FileSystemException {
    if (!initialized) {
        throw new IllegalStateException("not initialized");
    }
    FileSystemPathUtil.checkFormat(filePath);
    final String parentDir = FileSystemPathUtil.getParentDir(filePath);
    final String name = FileSystemPathUtil.getName(filePath);
    if (!isFolder(parentDir)) {
        throw new FileSystemException("path not found: " + parentDir);
    }
    if (isFolder(filePath)) {
        throw new FileSystemException("path denotes folder: " + filePath);
    }
    try {
        TransientFileFactory fileFactory = TransientFileFactory.getInstance();
        final File tmpFile = fileFactory.createTransientFile("bin", null, null);
        return new FilterOutputStream(new FileOutputStream(tmpFile)) {

            public void write(byte[] bytes, int off, int len) throws IOException {
                out.write(bytes, off, len);
            }

            public void close() throws IOException {
                out.flush();
                ((FileOutputStream) out).getFD().sync();
                out.close();
                InputStream in = null;
                try {
                    if (isFile(filePath)) {
                        synchronized (updateDataSQL) {
                            long length = tmpFile.length();
                            in = new FileInputStream(tmpFile);
                            conHelper.exec(updateDataSQL, new Object[] { new StreamWrapper(in, length), new Long(System.currentTimeMillis()), new Long(length), parentDir, name });
                        }
                    } else {
                        synchronized (insertFileSQL) {
                            long length = tmpFile.length();
                            in = new FileInputStream(tmpFile);
                            conHelper.exec(insertFileSQL, new Object[] { parentDir, name, new StreamWrapper(in, length), new Long(System.currentTimeMillis()), new Long(length) });
                        }
                    }
                } catch (Exception e) {
                    IOException ioe = new IOException(e.getMessage());
                    ioe.initCause(e);
                    throw ioe;
                } finally {
                    if (in != null) {
                        in.close();
                    }
                    // temp file can now safely be removed
                    tmpFile.delete();
                }
            }
        };
    } catch (Exception e) {
        String msg = "failed to open output stream to file: " + filePath;
        log.error(msg, e);
        throw new FileSystemException(msg, e);
    }
}
Also used : FileInputStream(java.io.FileInputStream) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) SQLException(java.sql.SQLException) FileSystemException(org.apache.jackrabbit.core.fs.FileSystemException) FileSystemException(org.apache.jackrabbit.core.fs.FileSystemException) TransientFileFactory(org.apache.jackrabbit.util.TransientFileFactory) FileOutputStream(java.io.FileOutputStream) StreamWrapper(org.apache.jackrabbit.core.util.db.StreamWrapper) FilterOutputStream(java.io.FilterOutputStream) File(java.io.File)

Example 19 with FilterOutputStream

use of java.io.FilterOutputStream in project tika by apache.

the class NetworkParser method parse.

private void parse(TikaInputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
    if ("telnet".equals(uri.getScheme())) {
        try (Socket socket = new Socket(uri.getHost(), uri.getPort())) {
            new ParsingTask(stream, new FilterOutputStream(socket.getOutputStream()) {

                @Override
                public void close() throws IOException {
                    socket.shutdownOutput();
                }
            }).parse(socket.getInputStream(), handler, metadata, context);
        }
    } else {
        URL url = uri.toURL();
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        connection.connect();
        try (InputStream input = connection.getInputStream()) {
            new ParsingTask(stream, connection.getOutputStream()).parse(new CloseShieldInputStream(input), handler, metadata, context);
        }
    }
}
Also used : CloseShieldInputStream(org.apache.tika.io.CloseShieldInputStream) TikaInputStream(org.apache.tika.io.TikaInputStream) InputStream(java.io.InputStream) FilterOutputStream(java.io.FilterOutputStream) Socket(java.net.Socket) URL(java.net.URL) URLConnection(java.net.URLConnection) CloseShieldInputStream(org.apache.tika.io.CloseShieldInputStream)

Example 20 with FilterOutputStream

use of java.io.FilterOutputStream in project apex-malhar by apache.

the class AbstractFileOutputOperator method processTuple.

/**
 * This method processes received tuples.
 * Tuples are written out to the appropriate files as determined by the getFileName method.
 * If the output port is connected incoming tuples are also converted and emitted on the appropriate output port.
 * @param tuple An incoming tuple which needs to be processed.
 */
protected void processTuple(INPUT tuple) {
    String fileName = getFileName(tuple);
    if (Strings.isNullOrEmpty(fileName)) {
        return;
    }
    try {
        FilterOutputStream fsOutput = streamsCache.get(fileName).getFilterStream();
        byte[] tupleBytes = getBytesForTuple(tuple);
        long start = System.currentTimeMillis();
        fsOutput.write(tupleBytes);
        totalWritingTime += System.currentTimeMillis() - start;
        totalBytesWritten += tupleBytes.length;
        MutableLong currentOffset = endOffsets.get(fileName);
        if (currentOffset == null) {
            currentOffset = new MutableLong(0);
            endOffsets.put(fileName, currentOffset);
        }
        currentOffset.add(tupleBytes.length);
        if (rotationWindows > 0) {
            getRotationState(fileName).notEmpty = true;
        }
        if (rollingFile && currentOffset.longValue() > maxLength) {
            LOG.debug("Rotating file {} {} {}", fileName, openPart.get(fileName), currentOffset.longValue());
            rotate(fileName);
        }
        MutableLong count = counts.get(fileName);
        if (count == null) {
            count = new MutableLong(0);
            counts.put(fileName, count);
        }
        count.add(1);
    } catch (IOException | ExecutionException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : MutableLong(org.apache.commons.lang.mutable.MutableLong) IOException(java.io.IOException) FilterOutputStream(java.io.FilterOutputStream) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

FilterOutputStream (java.io.FilterOutputStream)36 IOException (java.io.IOException)27 Support_OutputStream (tests.support.Support_OutputStream)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ByteArrayInputStream (java.io.ByteArrayInputStream)9 FileOutputStream (java.io.FileOutputStream)7 OutputStream (java.io.OutputStream)7 File (java.io.File)4 InputStream (java.io.InputStream)4 Test (org.junit.Test)4 BufferedOutputStream (java.io.BufferedOutputStream)3 DataOutputStream (java.io.DataOutputStream)3 FileInputStream (java.io.FileInputStream)3 FilterInputStream (java.io.FilterInputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 ByteSink (com.google.common.io.ByteSink)2 BufferedWriter (java.io.BufferedWriter)2 FileWriter (java.io.FileWriter)2 Writer (java.io.Writer)2