/** * @see java.io.DataOutput#writeShort(int) */ @Override public void writeShort(int value) throws IOException { byte [] buffer = { (byte)((value >>> 8) & 0xFF), (byte)((value >>> 0) & 0xFF) }; write(buffer, 0, buffer.length); }
/** * Adds an output stream; by default the stream will be auto-closed. * * @param stream * the stream to add to the T. * @return * the object itself, for method chaining. */ public TeeOutputStream with(OutputStream stream) { return with(stream, false); }
/** * Copies all the bytes it can read from the input stream into the output * stream; input and output streams management (opening, flushing, closing) * are all up to the caller. * * @param input * an open and ready-to-be-read input stream. * @param output * an open output stream. * @return * the total number of bytes copied. * @throws IOException */ public static long copy(InputStream input, OutputStream output) throws IOException { return copy(input, output, false); }
/** * Returns an input stream from a string representing its path. * * @param filepath * a string representing the file path. * @return * a {@code FileInputStream} object. * @throws FileNotFoundException */ public static InputStream fromFile(String filepath) throws FileNotFoundException { if(Strings.isValid(filepath)) { return fromFile(new File(filepath)); } return null; }
/** * Reads a resource from an URL, specified as a string; since this class makes * use of the URL factory, URLs may also represent resources in the class * path, re (in the format "classpath:org/dihedron/resources/MyResource.png"). * * @param url * a string presenting an URL. * @return * an input stream to access the URL. * @throws IOException * if the URL is malformed or an error occurs opening the stream. */ public static InputStream fromURL(String url) throws IOException { if(Strings.isValid(url)) { return fromURL(URLFactory.makeURL(url)); } return null; }
/** * @see java.io.DataOutput#writeDouble(double) */ @Override public void writeDouble(double value) throws IOException { writeLong(Double.doubleToLongBits(value)); }
/** * @see java.io.DataOutput#writeFloat(float) */ @Override public void writeFloat(float value) throws IOException { writeInt(Float.floatToIntBits(value)); }
/** * @see org.dihedron.patterns.cache.Storage#clear() */ @Override public void clear() { logger.debug("clearing storage"); for(String resource : contents.keySet()) { Streams.safelyClose(contents.get(resource)); } contents.clear(); } }
/** * This method actually does nothing to store the binary data, it returns a * void outputStream and there is no use in writing anything to it; as a side * effect, it records the name of the resource so that it <em>looks like</em> * the resource is actually stored, but it is not: it will be retrieved from * the remote server when requested. * * @see org.dihedron.patterns.cache.Storage#store(java.lang.String) */ @Override public OutputStream store(String resource) { if(Strings.isValid(resource)) { resources.add(resource); return new NullOutputStream(); } return null; }
/** * Copies all the bytes it can read from the input stream into the output * stream; input and output streams management (opening, flushing, closing) * are all up to the caller. * * @param input * an open and ready-to-be-read input stream. * @param output * an open output stream. * @return * the total number of bytes copied. * @throws IOException */ public static long copy(InputStream input, OutputStream output) throws IOException { return copy(input, output, false); }
/** * Returns an input stream from a string representing its path. * * @param filepath * a string representing the file path. * @return * a {@code FileInputStream} object. * @throws FileNotFoundException */ public static InputStream fromFile(String filepath) throws FileNotFoundException { if(Strings.isValid(filepath)) { return fromFile(new File(filepath)); } return null; }
/** * Reads a resource from an URL, specified as a string; since this class makes * use of the URL factory, URLs may also represent resources in the class * path, re (in the format "classpath:org/dihedron/resources/MyResource.png"). * * @param url * a string presenting an URL. * @return * an input stream to access the URL. * @throws IOException * if the URL is malformed or an error occurs opening the stream. */ public static InputStream fromURL(String url) throws IOException { if(Strings.isValid(url)) { return fromURL(URLFactory.makeURL(url)); } return null; }
/** * @see java.io.DataOutput#writeChar(int) */ @Override public void writeChar(int value) throws IOException { byte [] buffer = { (byte)((value >>> 8) & 0xFF), (byte)((value >>> 0) & 0xFF) }; write(buffer, 0, buffer.length); }
/** * Copies all the bytes it can read from the input stream into the output * stream; input and output streams management (opening, flushing, closing) * are all up to the caller. * * @param input * an open and ready-to-be-read input stream. * @param output * an open output stream. * @return * the total number of bytes copied. * @throws IOException */ public static long copy(InputStream input, OutputStream output) throws IOException { return copy(input, output, false); }
/** * Returns an input stream from a string representing its path. * * @param filepath * a string representing the file path. * @return * a {@code FileInputStream} object. * @throws FileNotFoundException */ public static InputStream fromFile(String filepath) throws FileNotFoundException { if(Strings.isValid(filepath)) { return fromFile(new File(filepath)); } return null; }
/** * @see java.io.DataOutput#writeInt(int) */ @Override public void writeInt(int value) throws IOException { byte [] buffer = { (byte)((value >>> 24) & 0xFF), (byte)((value >>> 16) & 0xFF), (byte)((value >>> 8) & 0xFF), (byte)((value >>> 0) & 0xFF) }; write(buffer, 0, buffer.length); }
/** * Adds a file to the ZIP archive, given its content as an input stream. * * @param zipEntry * name of the entry in the archive. * @param input * the stream from which data will be read to be added to the ZIP archive; * the stream must be open and will not be closed once the operation is * complete, so it is up to the caller to release it. */ public void addFile(String zipEntry, InputStream input) throws IOException { if(input != null) { stream.putNextEntry(new ZipEntry(zipEntry)); Streams.copy(input, stream); } }
/** * @see java.io.DataOutput#writeLong(long) */ @Override public void writeLong(long value) throws IOException { byte buffer[] = { (byte)(value >>> 56), (byte)(value >>> 48), (byte)(value >>> 40), (byte)(value >>> 32), (byte)(value >>> 24), (byte)(value >>> 16), (byte)(value >>> 8), (byte)(value >>> 0) }; write(buffer, 0, buffer.length); }
/** * @see java.io.DataOutput#writeBytes(java.lang.String) */ @Override public void writeBytes(String string) throws IOException { byte[] buffer = new byte[string.length()]; for (int i = 0 ; i < buffer.length ; i++) { buffer[i] = (byte)string.charAt(i); } write(buffer, 0, buffer.length); }
/** * @see java.io.DataOutput#writeChars(java.lang.String) */ @Override public void writeChars(String string) throws IOException { int length = string.length(); byte [] buffer = new byte[length * 2]; for (int i = 0 ; i < length ; i+=2) { int v = string.charAt(i); buffer[i] = (byte)((v >>> 8) & 0xFF); buffer[i+1] = (byte)((v >>> 0) & 0xFF); } write(buffer, 0, buffer.length); }