/** Close closeable object from current thread storage then remove the storage. */ @Override public void clean() { Files.close(threadLocal.get()); threadLocal.remove(); } }
/** Close closeable object from current thread storage then remove the storage. */ @Override public void clean() { Files.close(threadLocal.get()); threadLocal.remove(); } }
/** Close bytes stream from current thread storage then remove the storage. */ @Override public void clean() { Files.close(threadLocal.get()); threadLocal.remove(); }
/** Close stream from current thread storage then remove the storage. */ @Override public void clean() { Files.close(threadLocal.get()); threadLocal.remove(); }
/** * Load string from character stream then closes it. * * @param reader source character stream. * @param maxCount optional maximum character count to load, default to entire file. * @return loaded string, possible empty but never null. * @throws IOException if read operation fails. */ public static String load(Reader reader, Integer... maxCount) throws IOException { long maxCountValue = maxCount.length > 0 ? maxCount[0] : Long.MAX_VALUE; StringWriter writer = new StringWriter(); try { char[] buffer = new char[1024]; for(;;) { int readChars = reader.read(buffer, 0, (int)Math.min(buffer.length, maxCountValue)); if(readChars <= 0) { break; } writer.write(buffer, 0, readChars); maxCountValue -= readChars; } } finally { Files.close(reader); Files.close(writer); } return writer.toString(); }
/** * Retrieve binary resource as array of bytes. Uses {@link #getResourceAsStream(String)} to read binary resource and * return bytes array. * * @param name resource name. * @return binary resource content as array of bytes. * @throws NoSuchBeingException if resource not found. * @throws IOException if resource reading fails. */ public static byte[] getResourceAsBytes(String name) throws IOException { InputStream is = getResourceAsStream(name); ByteArrayOutputStream os = new ByteArrayOutputStream(); try { byte[] buffer = new byte[4096]; int length = 0; while((length = is.read(buffer)) != -1) { os.write(buffer, 0, length); } } finally { Files.close(is); Files.close(os); } return os.toByteArray(); }
/** * Handy method for value object serialization to JSON formatted string. * * @param value primitive or aggregate value. * @return value JSON string representation. */ @Override public String stringify(Object value) { StringWriter writer = new StringWriter(); try { Serializer serializer = new Serializer(); serializer.serialize(writer, value); } catch (IOException e) { // there is no reason to have IO write operation fail on string beside hardware or system resources shortage throw new RuntimeException(e); } finally { Files.close(writer); } return writer.toString(); }
Files.close(outputStream);
Files.close(inputStream); filesArchive.closeEntry();
@Override public void forEach(FormHandler handler) throws IOException { for (Part part : this) { if (part instanceof UploadStream) { UploadStreamImpl uploadStream = (UploadStreamImpl) part; InputStream inputStream = uploadStream.openStream(); try { handler.stream(uploadStream.getName(), uploadStream.getFileName(), uploadStream.getContentType(), inputStream); } catch (IOException e) { throw e; } catch (Throwable t) { throw new RuntimeException(t); } finally { Files.close(uploadStream); } } else { FormField formField = (FormField) part; try { handler.field(formField.getName(), formField.getValue()); } catch (Throwable t) { throw new RuntimeException(t); } } } }