@Override public void close() throws IOException { if (_currentInputStream != null) { FileHelper.safeClose(_currentInputStream); } } }
@Override public String getTemporaryStorageDirectory() { return FileHelper.getTempDir().getAbsolutePath(); }
public static void writeString(OutputStream outputStream, String string, String encoding) throws IllegalStateException { final Writer writer = getWriter(outputStream, encoding); writeString(writer, string, encoding); }
public static String readInputStreamAsString(InputStream inputStream, String encoding) throws IllegalStateException { Reader reader = getReader(inputStream, encoding); return readAsString(reader); }
public static void copy(File from, File to) throws IllegalStateException { assert from.exists(); final InputStream in = getInputStream(from); try { final OutputStream out = getOutputStream(to); try { copy(in, out); } finally { safeClose(out); } } finally { safeClose(in); } }
private Set<String> loadEntries() { Set<String> entries = new HashSet<String>(); BufferedReader reader = null; try { reader = FileHelper.getBufferedReader(getFile(), _encoding); for (String line = reader.readLine(); line != null; line = reader.readLine()) { entries.add(line); } } catch (Exception e) { throw new IllegalStateException(e); } finally { FileHelper.safeClose(reader); } return entries; }
private static File createFileFromInputStream(InputStream inputStream, String encoding) { final File file; final File tempDir = FileHelper.getTempDir(); final BufferedWriter writer = FileHelper.getBufferedWriter(file, encoding); final BufferedReader reader = FileHelper.getBufferedReader(inputStream, encoding); throw new IllegalStateException(e); } finally { FileHelper.safeClose(writer, reader);
private Object convertValue(Object value) { if (value instanceof Clob) { try { Reader reader = ((Clob) value).getCharacterStream(); try { value = FileHelper.readAsString(reader); } finally { FileHelper.safeClose(reader); } } catch (SQLException e) { logger.error("Failed to convert CLOB to String", e); value = null; } } else if (value instanceof Blob) { try { InputStream inputStream = ((Blob) value).getBinaryStream(); try { value = FileHelper.readAsBytes(inputStream); } finally { FileHelper.safeClose(inputStream); } } catch (SQLException e) { logger.error("Failed to convert BLOB to byte[]", e); value = null; } } return value; }
@Override public void execute() throws MetaModelException { final File tempFile = FileHelper.createTempFile("metamodel_deletion", "csv"); final InputStream in = FileHelper.getInputStream(tempFile); try { _updateCallback.getResource().write(new Action<OutputStream>() { FileHelper.safeClose(in);
final InputStream inputStream = FileHelper.getInputStream(file); try { scanInputStream(inputStream, classLoader, strictClassLoader); logger.error("Could not read file", e); } finally { FileHelper.safeClose(inputStream);
public static Writer getWriter(File file, String encoding, boolean append) throws IllegalStateException { boolean insertBom = !append; return getWriter(file, encoding, append, insertBom); }
@Override public void run(InputStream in) throws Exception { FileHelper.copy(in, out); } });
public void generateCoreFile() throws IOException { final File coreSiteFile = new File(confFolder, "conf-site.xml"); try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("core-site-template.xml")) { final BufferedReader reader = FileHelper.getBufferedReader(inputStream, FileHelper.UTF_8_ENCODING); try (Writer writer = FileHelper.getWriter(coreSiteFile)) { String line = reader.readLine(); while (line != null) { line = line.replace("${PATH}", path); writer.write(line); line = reader.readLine(); } writer.flush(); } } }
@Override InputStream openStream(final int index) throws IOException { return FileHelper.getInputStream(_files[index]); } }
final Blob blob = resultSet.getBlob(columnIndex); final InputStream inputStream = blob.getBinaryStream(); final byte[] bytes = FileHelper.readAsBytes(inputStream); return bytes; } else if (type.isBinary()) { final Clob clob = resultSet.getClob(columnIndex); final Reader reader = clob.getCharacterStream(); final String result = FileHelper.readAsString(reader); return result; } else if (type.isBoolean()) {
public static BufferedReader getBufferedReader(File file) throws IllegalStateException { return getBufferedReader(file, DEFAULT_ENCODING); }
@Override public Object evaluate(Row row, Object[] parameters, SelectItem item) { final Object value = row.getValue(item); if (value == null || value instanceof String) { return value; } if (value instanceof Clob) { final Clob clob = (Clob) value; try { final Reader reader = clob.getCharacterStream(); final String result = FileHelper.readAsString(reader); return result; } catch (SQLException e) { throw new MetaModelException("Failed to read CLOB to String", e); } } return String.valueOf(value); }
public static Writer getWriter(File file, String encoding, boolean append, boolean insertBom) throws IllegalStateException { if (append && insertBom) { throw new IllegalArgumentException("Can not insert BOM into appending writer"); } final OutputStream outputStream = getOutputStream(file, append); return getWriter(outputStream, encoding, insertBom); }
public static String readAsString(Reader reader) throws IllegalStateException { final BufferedReader br = getBufferedReader(reader); try { StringBuilder sb = new StringBuilder(); boolean firstLine = true; for (String line = br.readLine(); line != null; line = br.readLine()) { if (firstLine) { firstLine = false; } else { sb.append('\n'); } sb.append(line); } return sb.toString(); } catch (Exception e) { throw new IllegalStateException(e); } finally { safeClose(br, reader); } }