* @param target
* the target {@link File}
*
+ * @return the number of bytes written
+ *
* @throws IOException
* in case of I/O error
*/
- public static void write(InputStream in, File target) throws IOException {
+ public static long write(InputStream in, File target) throws IOException {
OutputStream out = new FileOutputStream(target);
try {
- write(in, out);
+ return write(in, out);
} finally {
out.close();
}
* @param out
* the target {@link OutputStream}
*
+ * @return the number of bytes written
+ *
* @throws IOException
* in case of I/O error
*/
- public static void write(InputStream in, OutputStream out)
+ public static long write(InputStream in, OutputStream out)
throws IOException {
+ long written = 0;
byte buffer[] = new byte[4096];
int len = in.read(buffer);
while (len > -1) {
out.write(buffer, 0, len);
+ written += len;
len = in.read(buffer);
}
+
+ return written;
}
/**
static private long count = 0;
static private Object lock = new Object();
+ private Object instanceLock = new Object();
private File data;
+ private long size;
/**
* Do not use -- for serialisation purposes only.
ByteArrayInputStream in = new ByteArrayInputStream(data);
try {
this.data = getTemporaryFile();
- IOUtils.write(in, this.data);
+ size = IOUtils.write(in, this.data);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
*/
public Image(InputStream in) throws IOException {
data = getTemporaryFile();
- IOUtils.write(in, data);
+ size = IOUtils.write(in, data);
+ }
+
+ /**
+ * The size of the enclosed image in bytes.
+ *
+ * @return the size
+ */
+ public long getSize() {
+ return size;
}
/**
*/
@Override
public void close() throws IOException {
- data.delete();
- synchronized (lock) {
- count--;
- if (count <= 0) {
- count = 0;
- tmpRepository.close();
- tmpRepository = null;
+ synchronized (instanceLock) {
+ if (size >= 0) {
+ size = -1;
+ data.delete();
+ data = null;
+
+ synchronized (lock) {
+ count--;
+ if (count <= 0) {
+ count = 0;
+ tmpRepository.close();
+ tmpRepository = null;
+ }
+ }
}
}
}
package be.nikiroo.utils.android;
import android.graphics.Bitmap;
+import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.util.stream.Stream;
import javax.imageio.ImageIO;
@Override
protected boolean check() {
// If we can get the class, it means we have access to it
- @SuppressWarnings("unused")
- Object test = Bitmap.class;
+ Config.ALPHA_8;
return true;
}
-
+
@Override
public void saveAsImage(Image img, File target, String format)
throws IOException {
* in case of IO error
*/
static public Bitmap fromImage(Image img) throws IOException {
- byte[] array = img.getData();
- int size = array.length;
- // TODO: check if we can use a stream, too
- Bitmap image = BitmapFactory.decodeByteArray(array, 0, size);
- if (image == null) {
- String ssize = StringUtils.formatNumber(size);
- throw new IOException(
- "Failed to convert input to image, size was: " + ssize);
+ InputStream stream = img.newInputStream();
+ try {
+ Bitmap image = BitmapFactory.decodeStream(stream);
+ if (image == null) {
+ String ssize = StringUtils.formatNumber(img.getSize());
+ throw new IOException(
+ "Failed to convert input to image, size was: " + ssize);
+ }
+ } finally {
+ stream.close();
}
-
+
return image;
}
}
public class ImageUtilsAwt extends ImageUtils {
@Override
protected boolean check() {
- // If we can get the class, it means we have access to it
- @SuppressWarnings("unused")
- Object test = ImageIO.class;
+ // Will not work if ImageIO is not available
+ ImageIO.getCacheDirectory();
return true;
}