/** * 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); }
public ReportGenerator(InputStream templateStream) { this(StreamUtil.streamUtf8AsString(templateStream)); }
private FileCachedInputStream createResettableStream(InputStream is) throws IOException { return StreamUtil.createResettableStream(is, FileCachedInputStream.DEFAULT_BUFFER_SIZE); }
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); } }
public static String streamUtf8AsString(InputStream in) { return streamAsString(in, "UTF-8"); }
@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); } } }
public static String readString(File path) { try (InputStream is = new BufferedInputStream(new FileInputStream(path))) { return StreamUtil.streamUtf8AsString(is); } catch (IOException e) { throw new OkapiIOException(e); } }
createdStream = StreamUtil.createResettableStream(new ByteArrayInputStream(bytes), bytes.length+1); } catch (UnsupportedEncodingException e) { throw new OkapiUnsupportedEncodingException(String.format(
@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); } }
/** * Load the {@link IParameters} from the give {@link InputStream} * @param stream - stream of the parameter file. */ public void loadParametersFromStream(InputStream stream) { // stream parameters always overrides any other source parametersLocation = null; this.parameters.fromString(StreamUtil.streamUtf8AsString(stream)); } }
/** * 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); } } }
public static String readString(URL url) { try { return StreamUtil.streamUtf8AsString(url.openStream()); } 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); } }
StatusLine status = response.getStatusLine(); try (InputStream is = response.getEntity().getContent()) { String responseBody = StreamUtil.streamUtf8AsString(is); if (status.getStatusCode() == 200) { return responseBody;
@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; }
StatusLine status = response.getStatusLine(); try (InputStream is = response.getEntity().getContent()) { String responseBody = StreamUtil.streamUtf8AsString(is); if (status.getStatusCode() == 200) { return responseBody;
/** * 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); }