changeset 587:720d061a1b30 stream

more work on stream input
author robcast
date Thu, 06 Jan 2011 11:57:32 +0100
parents 41a8d293b798
children aee436f0549d
files servlet/src/digilib/image/ImageLoaderDocuImage.java servlet/src/digilib/io/ImageInput.java servlet/src/digilib/io/ImageStream.java
diffstat 3 files changed, 85 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/servlet/src/digilib/image/ImageLoaderDocuImage.java	Wed Jan 05 14:46:19 2011 +0100
+++ b/servlet/src/digilib/image/ImageLoaderDocuImage.java	Thu Jan 06 11:57:32 2011 +0100
@@ -68,6 +68,8 @@
 	/** File that was read */
 	protected File imgFile;
 
+	private ImageInput input;
+
 	/* loadSubimage is supported. */
 	public boolean isSubimageSupported() {
 		return true;
@@ -167,17 +169,31 @@
 	 * 
 	 * @return
 	 */
-	public ImageReader getReader(ImageFile f) throws IOException {
-		logger.debug("preloadImage " + f.getFile());
-		if (reader != null) {
-			logger.debug("Reader was not null!");
+	public ImageReader getReader(ImageInput input) throws IOException {
+		logger.debug("get ImageReader for " + input);
+		if (this.reader != null) {
+			if (this.input == input) {
+				// it was the same input
+				logger.debug("reusing Reader");
+				return reader;
+			}
 			// clean up old reader
+			logger.debug("cleaning Reader!");
 			dispose();
 		}
-		RandomAccessFile rf = new RandomAccessFile(f.getFile(), "r");
-		ImageInputStream istream = new FileImageInputStream(rf);
+		ImageInputStream istream = null;
+		if (input.hasImageInputStream()) {
+			// stream input
+			istream = input.getImageInputStream();
+		} else if (input.hasFile()) {
+			// file only input
+			RandomAccessFile rf = new RandomAccessFile(input.getFile(), "r");
+			istream = new FileImageInputStream(rf);
+		} else {
+			throw new FileOpException("Unable to get data from ImageInput");
+		}
 		Iterator<ImageReader> readers;
-		String mt = f.getMimetype();
+		String mt = input.getMimetype();
 		if (mt == null) {
 			logger.debug("No mime-type. Trying automagic.");
 			readers = ImageIO.getImageReaders(istream);
@@ -186,7 +202,6 @@
 			readers = ImageIO.getImageReadersByMIMEType(mt);
 		}
 		if (!readers.hasNext()) {
-		    rf.close();
 			throw new FileOpException("Can't find Reader to load File!");
 		}
 		reader = readers.next();
@@ -196,7 +211,7 @@
 			logger.debug("ImageIO: next reader: " + readers.next().getClass());
 		} */
 		reader.setInput(istream);
-		imgFile = f.getFile();
+		imgFile = input.getFile();
 		return reader;
 	}
 
--- a/servlet/src/digilib/io/ImageInput.java	Wed Jan 05 14:46:19 2011 +0100
+++ b/servlet/src/digilib/io/ImageInput.java	Thu Jan 06 11:57:32 2011 +0100
@@ -21,6 +21,10 @@
 
 package digilib.io;
 
+import java.io.File;
+
+import javax.imageio.stream.ImageInputStream;
+
 import digilib.image.ImageSize;
 
 public abstract class ImageInput {
@@ -77,4 +81,37 @@
 		return (pixelSize != null) ? pixelSize.getAspect() : 0f;
 	}
 	
+	/** Returns if the input can be returned as ImageInputStream.
+	 * 
+	 * @return
+	 */
+	public boolean hasImageInputStream() {
+		return false;
+	}
+	
+	/** Returns the input as ImageInputStream (if available)
+	 * 
+	 * @return
+	 */
+	public ImageInputStream getImageInputStream() {
+		return null;
+	}
+	
+	/** Returns if the input can be returned as File.
+	 * 
+	 * @return
+	 */
+	public boolean hasFile() {
+		return false;
+	}
+	
+	/** Returns the input as File (if available)
+	 * 
+	 * @return
+	 */
+	public File getFile() {
+		return null;
+	}
+	
+	
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/servlet/src/digilib/io/ImageStream.java	Thu Jan 06 11:57:32 2011 +0100
@@ -0,0 +1,24 @@
+/**
+ * 
+ */
+package digilib.io;
+
+import java.io.InputStream;
+
+/**
+ * @author casties
+ *
+ */
+public class ImageStream extends ImageInput {
+
+	protected InputStream stream = null;
+	
+	public ImageStream(InputStream stream, String mimeType) {
+		this.stream = stream;
+		this.mimetype = mimeType;
+	}
+	
+	public InputStream getStream() {
+		return stream;
+	}
+}