public static String readStringAndClose(InputStream is, Charset charset) { try{ return readString(is, charset); }finally{ close(is); } }
public static void writeString(File file,String string, Charset encoding){ FileOutputStream out = null; try{ out = new FileOutputStream(file); writeString(out,string,encoding); }catch(IOException e){ throw Exceptions.wrap(e); }finally{ close(out); } }
public static byte[] readByteArray(Reader reader) { ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(out); try{ copy(reader,writer); writer.flush(); return out.toByteArray(); }catch(IOException e){ throw Exceptions.wrap(e); }finally{ close(writer); } }
public static byte[] readByteArrayAndClose(InputStream is) { try{ return readByteArray(is); }finally{ close(is); } }
@Override public void free() throws SQLException { string = null; IO.close(reader); }
@Override public String convertToString(InputStream value) throws Throwable { return IO.readString(value, Charsets.UTF_8); } }
protected String loadOrGenerateSecret() { FileResource userDir = Resources.createFileResource(System.getProperty("user.dir")); FileResource targetDir = userDir.createRelative("./target"); File secretFile = targetDir.exists() ? targetDir.createRelative("./.secret").getFile() : userDir.createRelative("./.secret").getFile(); if(secretFile.exists()) { String secret = Strings.trim(IO.readString(secretFile, Charsets.UTF_8)); if(!Strings.isEmpty(secret)) { return secret; } } String secret = Randoms.nextString(32); if(getBooleanProperty("app.config.save-generated-secret", true)) { IO.writeString(secretFile, secret, Charsets.UTF_8); } return secret; }
/** * Writes the string to the output file using default charset. */ public static void writeString(File file, String string) { writeString(file, string, Charsets.defaultCharset()); }
/** * Copy chars from a <code>Reader</code> to a <code>Writer</code>. * <p> * This method buffers the input internally, so there is no need to use a <code>BufferedReader</code>. * <p> * The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}. * * @param input the <code>Reader</code> to read from * @param output the <code>Writer</code> to write to * @return the number of characters copied * @throws NullPointerException if the input or output is null * @throws NestedIOException if an I/O error occurs */ public static long copy(Reader input, Writer output) throws NestedIOException { return copy(input, output, new char[DEFAULT_BUFFER_SIZE]); }
/** * Creates a {@link Reader} for reading content of the file use the default charset {@link Charsets#defaultCharset()}. * * <p> * Note : * <strong> * You must close the retured {@link Reader}. * </strong> * * @throws NestedIOException if an I/O error occurs. */ public static Reader createReader(File file) throws NestedIOException{ return createReader(file,Charsets.defaultCharset()); }
/** * Creates a {@link Writer} for writing content to the file use default charset {@link Charsets#defaultCharset()} * * <p> * Note : * <strong> * You must close the retured {@link Writer}. * </strong> * * @throws NestedIOException if an I/O error occurs. */ public static Writer createWriter(File file) throws NestedIOException { return createWriter(file,Charsets.defaultCharset()); }
@Override public void free() throws SQLException { bytes = null; IO.close(stream); }
@Override public String convertToString(Blob value) throws Throwable { return IO.readString(value.getBinaryStream(), Charsets.UTF_8); } }
public static byte[] readByteArrayAndClose(Reader reader) { try{ return readByteArray(reader); }finally{ close(reader); } }
private void renderAndWriteFile(Mustache template, Map data, String filepath) { File file = folder.resolve(filepath).toFile(); if(filepath.indexOf('/') > 0) { Try.throwUnchecked(() -> Files.createDirectories(file.getParentFile().toPath())); } log.debug("Render template '{}' and write to '{}'", template.getName(), filepath); IO.writeString(file, template.render(data)); }
/** * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>. * <p> * This method buffers the input internally, so there is no need to use a <code>BufferedInputStream</code>. * <p> * The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}. * * @param input the <code>InputStream</code> to read from * @param output the <code>OutputStream</code> to write to * @return the number of bytes copied * @throws NullPointerException if the input or output is null * @throws NestedIOException if an I/O error occurs */ public static long copy(InputStream input, OutputStream output) throws NestedIOException { return copy(input, output, new byte[DEFAULT_BUFFER_SIZE]); }
/** * Reads the given properties file. * * @throws NestedIOException if an I/O error occurs. */ public static ExProperties load(File file,Properties defaults) throws NestedIOException { Args.notNull(file,"file"); try { ExProperties props = null == defaults ? new ExProperties(file) : new ExProperties(file,defaults); if(Strings.endsWithIgnoreCase(file.getName(), ".xml")){ try(InputStream in = new FileInputStream(file)){ props.loadFromXML(in); } }else{ try(Reader reader = IO.createReader(file,Charset.defaultCharset())){ props.load(reader); } } return props; } catch (IOException e) { throw new NestedIOException("Error loading properties file '" + file.getAbsolutePath() + "', " + e.getMessage(), e); } }
/** * Saving the given {@link Properties} to the given file use default charset {@link Charsets#defaultCharset()}. * * @throws NestedIOException if an I/O error occurs. */ public static void save(Properties props, File file) throws NestedIOException { Args.notNull(props,"properties"); Args.notNull(file,"file"); try{ if(!file.exists()){ file.createNewFile(); } if(Strings.endsWithIgnoreCase(file.getName(), ".xml")){ try(OutputStream out = new FileOutputStream(file)){ props.storeToXML(out, ""); } }else{ try(Writer writer = IO.createWriter(file)){ props.store(writer, ""); } } }catch(IOException e){ throw new NestedIOException("Error saving properties file '" + file.getAbsolutePath() + "', " + e.getMessage(), e); } }
public static String readStringAndClose(Reader reader) { try{ return readString(reader); }finally{ close(reader); } }