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;
}
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());
}
};
}
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);
}
}
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);
}
}
}
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);
}
}
Aggregations