Mercurial > hg > digilib-old
annotate servlet/src/digilib/io/ImageFile.java @ 590:69bc69381ac4 stream
more work on stream input and more cleanup
author | robcast |
---|---|
date | Thu, 06 Jan 2011 20:42:29 +0100 |
parents | aee436f0549d |
children | 2b58d2783ef0 |
rev | line source |
---|---|
159 | 1 /* ImageFile.java -- digilib image file class. |
2 | |
3 Digital Image Library servlet components | |
4 | |
5 Copyright (C) 2003 Robert Casties (robcast@mail.berlios.de) | |
6 | |
7 This program is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 2 of the License, or (at your | |
10 option) any later version. | |
11 | |
12 Please read license.txt for the full details. A copy of the GPL | |
13 may be found at http://www.gnu.org/copyleft/lgpl.html | |
14 | |
15 You should have received a copy of the GNU General Public License | |
16 along with this program; if not, write to the Free Software | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 * Created on 25.02.2003 | |
20 */ | |
21 | |
22 package digilib.io; | |
23 | |
24 import java.io.File; | |
590 | 25 import java.io.FileNotFoundException; |
585 | 26 import java.io.IOException; |
590 | 27 import java.io.RandomAccessFile; |
28 | |
29 import javax.imageio.stream.FileImageInputStream; | |
30 import javax.imageio.stream.ImageInputStream; | |
159 | 31 |
32 import digilib.image.ImageSize; | |
585 | 33 import digilib.servlet.DigilibConfiguration; |
159 | 34 |
35 /** | |
36 * @author casties | |
37 */ | |
568 | 38 public class ImageFile extends ImageInput { |
159 | 39 |
583 | 40 // file |
41 private File file = null; | |
159 | 42 // file name |
583 | 43 private String name = null; |
159 | 44 // parent directory |
45 private Directory dir = null; | |
46 | |
583 | 47 /** Constructor with File. |
48 * | |
49 * @param f | |
50 * @param parent | |
51 * @param dir | |
52 */ | |
53 public ImageFile(File f, ImageSet parent, Directory dir) { | |
54 this.file = f; | |
55 this.name = f.getName(); | |
159 | 56 this.parent = parent; |
57 this.dir = dir; | |
58 } | |
59 | |
583 | 60 /** Constructor with filename (without path). |
61 * @param fn | |
62 * @param parent | |
63 * @param dir | |
64 */ | |
65 public ImageFile(String fn, ImageSet parent, Directory dir) { | |
66 this.name = fn; | |
67 this.dir = dir; | |
68 this.file = new File(this.dir.getDir(), fn); | |
69 this.parent = parent; | |
159 | 70 } |
71 | |
585 | 72 |
73 /** Checks the image and sets size and type. | |
74 * | |
75 */ | |
76 public void check() { | |
77 if (pixelSize == null) { | |
78 try { | |
79 // use the configured toolkit to identify the image | |
80 DigilibConfiguration.identifyDocuImage(this); | |
81 } catch (IOException e) { | |
82 // nothing much to do... | |
83 } | |
84 } | |
85 } | |
86 | |
87 /* (non-Javadoc) | |
88 * @see digilib.io.ImageInput#getSize() | |
89 */ | |
90 @Override | |
91 public ImageSize getSize() { | |
92 check(); | |
93 return pixelSize; | |
94 } | |
95 | |
96 /* (non-Javadoc) | |
97 * @see digilib.io.ImageInput#getMimetype() | |
98 */ | |
99 @Override | |
100 public String getMimetype() { | |
101 check(); | |
102 return mimetype; | |
103 } | |
104 | |
105 /* (non-Javadoc) | |
106 * @see digilib.io.ImageInput#getAspect() | |
107 */ | |
108 @Override | |
109 public float getAspect() { | |
110 check(); | |
111 return (pixelSize != null) ? pixelSize.getAspect() : 0f; | |
112 } | |
113 | |
114 /** Returns the file name (without path). | |
159 | 115 * |
116 * @return | |
117 */ | |
118 public String getName() { | |
583 | 119 return name; |
159 | 120 } |
121 | |
590 | 122 |
123 /* (non-Javadoc) | |
124 * @see digilib.io.ImageInput#hasImageInputStream() | |
125 */ | |
126 @Override | |
127 public boolean hasImageInputStream() { | |
128 return true; | |
129 } | |
130 | |
131 /* (non-Javadoc) | |
132 * @see digilib.io.ImageInput#getImageInputStream() | |
133 */ | |
134 @Override | |
135 public ImageInputStream getImageInputStream() { | |
136 try { | |
137 RandomAccessFile rf = new RandomAccessFile(file, "r"); | |
138 return new FileImageInputStream(rf); | |
139 } catch (IOException e) { | |
140 // what now? | |
141 } | |
142 return null; | |
143 } | |
144 | |
145 /* (non-Javadoc) | |
146 * @see digilib.io.ImageInput#hasFile() | |
147 */ | |
148 @Override | |
149 public boolean hasFile() { | |
150 return true; | |
151 } | |
152 | |
153 /* (non-Javadoc) | |
154 * @see digilib.io.ImageInput#getFile() | |
159 | 155 */ |
156 public File getFile() { | |
583 | 157 return file; |
159 | 158 } |
159 | |
566 | 160 /* (non-Javadoc) |
568 | 161 * @see digilib.io.ImageInput#setSize(digilib.image.ImageSize) |
159 | 162 */ |
568 | 163 public void setSize(ImageSize imageSize) { |
164 this.pixelSize = imageSize; | |
165 // pass on to parent | |
166 if (this.parent != null) { | |
167 this.parent.setAspect(imageSize); | |
168 } | |
566 | 169 } |
170 | |
576
dad720e9b12b
try: DocuDirent as interface, ImageFile inherits from ImageInput and implements DocuDirent
robcast
parents:
574
diff
changeset
|
171 /* (non-Javadoc) |
dad720e9b12b
try: DocuDirent as interface, ImageFile inherits from ImageInput and implements DocuDirent
robcast
parents:
574
diff
changeset
|
172 * @see java.lang.Object#toString() |
dad720e9b12b
try: DocuDirent as interface, ImageFile inherits from ImageInput and implements DocuDirent
robcast
parents:
574
diff
changeset
|
173 */ |
dad720e9b12b
try: DocuDirent as interface, ImageFile inherits from ImageInput and implements DocuDirent
robcast
parents:
574
diff
changeset
|
174 @Override |
dad720e9b12b
try: DocuDirent as interface, ImageFile inherits from ImageInput and implements DocuDirent
robcast
parents:
574
diff
changeset
|
175 public String toString() { |
dad720e9b12b
try: DocuDirent as interface, ImageFile inherits from ImageInput and implements DocuDirent
robcast
parents:
574
diff
changeset
|
176 // try to use File.toString |
583 | 177 if (file != null) { |
178 return file.toString(); | |
576
dad720e9b12b
try: DocuDirent as interface, ImageFile inherits from ImageInput and implements DocuDirent
robcast
parents:
574
diff
changeset
|
179 } |
dad720e9b12b
try: DocuDirent as interface, ImageFile inherits from ImageInput and implements DocuDirent
robcast
parents:
574
diff
changeset
|
180 return super.toString(); |
dad720e9b12b
try: DocuDirent as interface, ImageFile inherits from ImageInput and implements DocuDirent
robcast
parents:
574
diff
changeset
|
181 } |
dad720e9b12b
try: DocuDirent as interface, ImageFile inherits from ImageInput and implements DocuDirent
robcast
parents:
574
diff
changeset
|
182 |
dad720e9b12b
try: DocuDirent as interface, ImageFile inherits from ImageInput and implements DocuDirent
robcast
parents:
574
diff
changeset
|
183 |
159 | 184 } |