/** * Copies a {@link ReadableByteChannel} to a {@link WritableByteChannel}. * @param inChannel the input Channel. * @param outChannel the output Channel. * @throws OkapiIOException if an error occurs. */ public static void copy(ReadableByteChannel inChannel, WritableByteChannel outChannel) { copy(inChannel, outChannel, true); }
@Override public void doPostMerge() { if (movedFiles == null || movedFiles.size() == 0) return; for (Entry<String, String> e : movedFiles.entrySet()) { if (new File(e.getKey()).exists()) continue; StreamUtil.copy(e.getValue(), e.getKey(), true); } } }
@Override public void close () { try { docOriginal = null; if ( xmlOutStream == null ) return; // Was closed already // Close the output xmlOutStream.close(); xmlOutStream = null; buffer = null; // If it was in a temporary file, copy it over the existing one // If the IFilter.close() is called before IFilterWriter.close() // this should allow to overwrite the input. if ( tempFile != null ) { StreamUtil.copy(new FileInputStream(tempFile), outputPath); } } catch ( IOException e ) { throw new OkapiIOException("Error closing ICML outpiut.\n"+e.getMessage(), e); } }
/** * Copies an {@link InputStream} to an {@link OutputStream}. * @param is the input stream. * @param outputStream the output stream. * @throws OkapiIOException if an error occurs. */ public static void copy(InputStream is, OutputStream outputStream) { ReadableByteChannel inChannel = null; WritableByteChannel outChannel = null; try { inChannel = Channels.newChannel(is); outChannel = Channels.newChannel(outputStream); // channels are closed here copy(inChannel, outChannel); } finally { try { if (is != null) is.close(); if (outputStream != null) outputStream.close(); } catch ( IOException e ) { throw new OkapiIOException(e); } } }
@Override public void close() { if (zipOutputStream == null) { return; } try { if (zipOriginal != null) { zipOriginal.close(); zipOriginal = null; } // Close the output zipOutputStream.close(); zipOutputStream = null; // If it was in a temporary file, copy it over the existing one // If the IFilter.close() is called before IFilterWriter.close() // this should allow to overwrite the input. if (tempFile != null) { StreamUtil.copy(new FileInputStream(tempFile), outputPath); tempFile.delete(); } buffer = null; } catch (IOException e) { throw new OkapiIOException("Error closing IDML output.\n" + e.getMessage(), e); } }
@Override public Event handleRawDocument (Event event) { File file = new File(event.getRawDocument().getInputURI()); File output = new File(outputURI); if ( params.getCopyOption().equals("overwrite") ) { StreamUtil.copy(file.getPath(), output.getPath(), params.isMove()); } else if ( params.getCopyOption().equals("backup") ) { if ( output.exists() ) { if ( !output.renameTo(new File(outputURI.toString() + ".bak")) ) { StreamUtil.copy(output.getPath(), output.getPath().replace(".txt", ".txt.bak"), true); } } StreamUtil.copy(file.getPath(), output.getPath(), params.isMove()); } else { // skip copy/move file if ( !output.exists() ) { StreamUtil.copy(file.getPath(), output.getPath(), params.isMove()); } } return event; }
public static void writeString(String str, OutputStream os) { try (InputStream is = StreamUtil.stringAsStream(str, "UTF-8")) { StreamUtil.copy(is, os); } catch (IOException e) { throw new OkapiIOException(e); } }
/** * Opens an input stream for filtering * * @param input an input stream to open and filter */ void open(InputStream input) { // Create a temp file for the stream content tempFile = FileUtil.createTempFile("~okapi-23_IDMLFilter_"); StreamUtil.copy(input, tempFile); open(toURI(tempFile.getAbsolutePath())); }
public static FileCachedInputStream createResettableStream(InputStream is, int bufferSize) throws IOException { FileCachedInputStream ifcis; try { ifcis = new FileCachedInputStream(bufferSize); copy(Channels.newChannel(is), ifcis); } catch (IOException e) { is.close(); throw new OkapiIOException("Error copying inputstream to FileCachedInputStream", e); } finally { is.close(); } // create a mark on the first byte // this is expected by some callers // that reset the stream themselves ifcis.mark(Integer.MAX_VALUE); return ifcis; }
/** * Opens an input stream for filtering * @param input an input stream to open and filter */ public void open (InputStream input) { // // Not supported for this filter // throw new UnsupportedOperationException( // "Method is not supported for this filter.");\ // Create a temp file for the stream content tempFile = FileUtil.createTempFile("~okapi-23_OpenXMLFilter_"); StreamUtil.copy(input, tempFile); open(Util.toURI(tempFile.getAbsolutePath())); }
@Override public void prepareForMerge(String dir) { movedFiles = new HashMap<String, String>(); if (!dir.endsWith(File.separator)) dir = dir + File.separator; String tmDir = dir + "tm" + File.separator; Util.createDirectories(tmDir); String projSave = dir + "omegat" + File.separator + "project_save.tmx"; if (new File(projSave).isFile()) { String moveTo = uniqueName(tmDir + "project_save.tmx", "-orig"); StreamUtil.copy(projSave, moveTo, true); movedFiles.put(projSave, moveTo); } for (String file : RENAME_FILES) { if (! new File(dir + file).isFile()) continue; String moveFrom = dir + file; String moveTo = uniqueName(dir + file, "-orig"); StreamUtil.copy(moveFrom, moveTo, true); movedFiles.put(moveFrom, moveTo); } File manifest = new File(dir + "manifest.rkm"); if (manifest.isFile()) manifest.delete(); Util.deleteDirectory(dir + "original", false); Util.deleteDirectory(dir + "source", false); Util.deleteDirectory(dir + "target", false); }
@Override public Event handleRawDocument (Event event) { try (RawDocument rawDoc = (RawDocument)event.getResource();) { StreamUtil.copy(rawDoc.getStream(), outStream); } catch ( Throwable e ) { throw new OkapiIOException("Error copying a RawDocument to output stream.", e); } // this steps writes RawDocument then eats the event return Event.NOOP_EVENT; } }
@Override public Event handleRawDocument (Event event) { RawDocument rawDoc = null; try { rawDoc = (RawDocument)event.getResource(); File outFile = new File(outputURI); Util.createDirectories(outFile.getAbsolutePath()); StreamUtil.copy(rawDoc.getStream(), outFile); } catch ( Throwable e ) { throw new OkapiIOException("Error writing or copying a RawDocument.", e); } finally { if (rawDoc != null) { rawDoc.close(); } } // this steps writes RawDocument then eats the event return Event.NOOP_EVENT; }
protected void processStartDocument (Event event) { StartDocument sd = event.getStartDocument(); String docPath = sd.getName()==null ? "" : sd.getName(); if (System.getProperty("os.name").startsWith("Windows") && Pattern.matches("^/[A-Z]:.*$", docPath)) docPath = docPath.substring(1); String ori = manifest.getTempOriginalDirectory(); if ( Util.isEmpty(ori) ) return; // No copy to be done // Else: copy the original MergingInfo info = manifest.getItem(docId); String inputPath = exists(docPath) ? docPath : manifest.getInputRoot() + info.getRelativeInputPath(); String outputPath = ori + info.getRelativeInputPath(); StreamUtil.copy(inputPath, outputPath, false); }
protected void processRawDocument (Event event) { String ori = manifest.getTempOriginalDirectory(); if ( Util.isEmpty(ori) ) return; // No copy to be done // Else: copy the original MergingInfo info = manifest.getItem(docId); String inputPath = manifest.getInputRoot() + info.getRelativeInputPath(); String outputPath = ori + info.getRelativeInputPath(); StreamUtil.copy(inputPath, outputPath, false); }
String destPath = manifest.getTempPackageRoot() + (destDir.isEmpty() ? "" : destDir+"/") + destFn; StreamUtil.copy(origDir+"/"+origFn, destPath, false);
StreamUtil.copy(input.getStream(), tempFile); docURI = Util.toURI(tempFile.getAbsolutePath());
StreamUtil.copy(input.getStream(), fromStreamTmpFile); this.input = new RawDocument(fromStreamTmpFile.toURI(), input.getEncoding(), input.getSourceLocale(), input.getTargetLocale(),