Source code: org/ydp/jai/PropertySet.java
1 /*
2 * The contents of this file are subject to the JAVA ADVANCED IMAGING
3 * SAMPLE INPUT-OUTPUT CODECS AND WIDGET HANDLING SOURCE CODE License
4 * Version 1.0 (the "License"); You may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 * http://www.sun.com/software/imaging/JAI/index.html
7 *
8 * Software distributed under the License is distributed on an "AS IS"
9 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
10 * the License for the specific language governing rights and limitations
11 * under the License.
12 *
13 * The Original Code is JAVA ADVANCED IMAGING SAMPLE INPUT-OUTPUT CODECS
14 * AND WIDGET HANDLING SOURCE CODE.
15 * The Initial Developer of the Original Code is: Sun Microsystems, Inc..
16 * Portions created by: _______________________________________
17 * are Copyright (C): _______________________________________
18 * All Rights Reserved.
19 * Contributor(s): _______________________________________
20 */
21
22 package org.ydp.jai;
23
24 import java.io.IOException;
25 import java.util.Date;
26 import java.util.Hashtable;
27
28
29 class Property {
30
31 private int type;
32 private int offset;
33
34 public Property(int type, int offset) {
35 this.type = type;
36 this.offset = offset;
37 }
38
39 public int getType() {
40 return type;
41 }
42
43 public int getOffset() {
44 return offset;
45 }
46 }
47
48 class PropertySet {
49
50 private static final int TYPE_VT_EMPTY = -1;
51 private static final int TYPE_VT_NULL = -1;
52 private static final int TYPE_VT_I2 = 2;
53 private static final int TYPE_VT_I4 = 3;
54 private static final int TYPE_VT_R4 = -1;
55 private static final int TYPE_VT_R8 = -1;
56 private static final int TYPE_VT_CY = -1;
57 private static final int TYPE_VT_DATE = -1;
58 private static final int TYPE_VT_BSTR = -1;
59 private static final int TYPE_VT_ERROR = -1;
60 private static final int TYPE_VT_BOOL = -1;
61 private static final int TYPE_VT_VARIANT = -1;
62 private static final int TYPE_VT_UI1 = -1;
63 private static final int TYPE_VT_UI2 = -1;
64 private static final int TYPE_VT_UI4 = 19;
65 private static final int TYPE_VT_I8 = -1;
66 private static final int TYPE_VT_UI8 = -1;
67 private static final int TYPE_VT_LPSTR = 30;
68 private static final int TYPE_VT_LPWSTR = 31;
69 private static final int TYPE_VT_FILETIME = 64;
70 private static final int TYPE_VT_BLOB = 65;
71 private static final int TYPE_VT_STREAM = -1;
72 private static final int TYPE_VT_STORAGE = -1;
73 private static final int TYPE_VT_STREAMED_OBJECT = -1;
74 private static final int TYPE_VT_STORED_OBJECT = -1;
75 private static final int TYPE_VT_BLOB_OBJECT = -1;
76 private static final int TYPE_VT_CF = 71;
77 private static final int TYPE_VT_CLSID = 72;
78 private static final int TYPE_VT_VECTOR = 4096;
79
80 SeekableStream stream;
81 Hashtable properties = new Hashtable();
82
83 public PropertySet(SeekableStream stream) throws IOException {
84 this.stream = stream;
85
86 stream.seek(44);
87 int sectionOffset = stream.readIntLE();
88
89 stream.seek(sectionOffset);
90 int sectionSize = stream.readIntLE();
91 int sectionCount = stream.readIntLE();
92
93 for (int i = 0; i < sectionCount; i++) {
94 stream.seek(sectionOffset + 8*i + 8);
95 int pid = stream.readIntLE();
96 int offset = stream.readIntLE();
97
98 stream.seek(sectionOffset + offset);
99 int type = stream.readIntLE();
100
101 Property p = new Property(type, sectionOffset + offset + 4);
102 properties.put(new Integer(pid), p);
103 }
104 }
105
106 public boolean hasProperty(int id) {
107 Property p = (Property)properties.get(new Integer(id));
108 return (p != null);
109 }
110
111 public int getI4(int id) {
112 Property p = (Property)properties.get(new Integer(id));
113 try {
114 int offset = p.getOffset();
115 stream.seek(offset);
116 return stream.readIntLE();
117 } catch (IOException e) {
118 e.printStackTrace();
119 }
120
121 return -1;
122 }
123
124 public int getUI1(int id) {
125 Property p = (Property)properties.get(new Integer(id));
126 try {
127 int offset = p.getOffset();
128 stream.seek(offset);
129 return stream.readUnsignedByte();
130 } catch (IOException e) {
131 e.printStackTrace();
132 }
133
134 return -1;
135 }
136
137 public int getUI2(int id) {
138 Property p = (Property)properties.get(new Integer(id));
139 try {
140 int offset = p.getOffset();
141 stream.seek(offset);
142 return stream.readUnsignedShortLE();
143 } catch (IOException e) {
144 e.printStackTrace();
145 }
146
147 return -1;
148 }
149
150 public long getUI4(int id) {
151 Property p = (Property)properties.get(new Integer(id));
152 try {
153 int offset = p.getOffset();
154 stream.seek(offset);
155 return stream.readUnsignedIntLE();
156 } catch (IOException e) {
157 e.printStackTrace();
158 }
159
160 return -1;
161 }
162
163 public long getUI4(int id, long defaultValue) {
164 Property p = (Property)properties.get(new Integer(id));
165 if (p == null) {
166 return defaultValue;
167 }
168
169 try {
170 int offset = p.getOffset();
171 stream.seek(offset);
172 return stream.readUnsignedIntLE();
173 } catch (IOException e) {
174 e.printStackTrace();
175 }
176
177 return -1;
178 }
179
180 public String getLPSTR(int id) {
181 Property p = (Property)properties.get(new Integer(id));
182 if (p == null) {
183 return null;
184 }
185
186 try {
187 int offset = p.getOffset();
188
189 stream.seek(offset);
190 int length = stream.readIntLE();
191 StringBuffer sb = new StringBuffer(length);
192 for (int i = 0; i < length; i++) {
193 sb.append((char)stream.read());
194 }
195
196 return sb.toString();
197 } catch (IOException e) {
198 e.printStackTrace();
199 return null;
200 }
201 }
202
203 public String getLPWSTR(int id) {
204 Property p = (Property)properties.get(new Integer(id));
205 try {
206 int offset = p.getOffset();
207
208 stream.seek(offset);
209 int length = stream.readIntLE();
210 StringBuffer sb = new StringBuffer(length);
211 for (int i = 0; i < length; i++) {
212 sb.append(stream.readCharLE());
213 }
214
215 return sb.toString();
216 } catch (IOException e) {
217 e.printStackTrace();
218 return null;
219 }
220 }
221
222 public float getR4(int id) {
223 Property p = (Property)properties.get(new Integer(id));
224 try {
225 int offset = p.getOffset();
226 stream.seek(offset);
227 return stream.readFloatLE();
228 } catch (IOException e) {
229 e.printStackTrace();
230 return -1.0F;
231 }
232 }
233
234 public Date getDate(int id) {
235 throw new RuntimeException(JaiI18N.getString("PropertySet0"));
236 }
237
238 public Date getFiletime(int id) {
239 throw new RuntimeException(JaiI18N.getString("PropertySet0"));
240 }
241
242 public byte[] getBlob(int id) {
243 Property p = (Property)properties.get(new Integer(id));
244 try {
245 int offset = p.getOffset();
246 stream.seek(offset);
247 int length = stream.readIntLE();
248
249 byte[] buf = new byte[length];
250 stream.seek(offset + 4);
251 stream.readFully(buf);
252
253 return buf;
254 } catch (IOException e) {
255 e.printStackTrace();
256 return null;
257 }
258 }
259
260 public int[] getUI1Vector(int id) {
261 throw new RuntimeException(JaiI18N.getString("PropertySet0"));
262 }
263
264 public int[] getUI2Vector(int id) {
265 throw new RuntimeException(JaiI18N.getString("PropertySet0"));
266 }
267
268 public long[] getUI4Vector(int id) {
269 throw new RuntimeException(JaiI18N.getString("PropertySet0"));
270 }
271
272 public float[] getR4Vector(int id) {
273 throw new RuntimeException(JaiI18N.getString("PropertySet0"));
274 }
275
276 public String[] getLPWSTRVector(int id) {
277 throw new RuntimeException(JaiI18N.getString("PropertySet0"));
278 }
279 }