01: // JpegFileResource.java
02: // $Id: JpegFileResource.java,v 1.3 2003/01/24 19:59:48 ylafon Exp $
03: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
04: // Please first read the full copyright statement in file COPYRIGHT.html
05:
06: package org.w3c.jigsaw.resources;
07:
08: import java.io.File;
09: import java.io.FileOutputStream;
10: import java.io.IOException;
11: import java.io.InputStream;
12: import java.io.InputStreamReader;
13: import java.io.Reader;
14: import java.io.Writer;
15:
16: import org.w3c.tools.resources.FileResource;
17:
18: import org.w3c.tools.jpeg.JpegCommentHandler;
19:
20: public class JpegFileResource extends ImageFileResource {
21: /**
22: * Save the given stream as the underlying file content.
23: * This method preserve the old file version in a <code>~</code> file.
24: * @param in The input stream to use as the resource entity.
25: * @return A boolean, <strong>true</strong> if the resource was just
26: * created, <strong>false</strong> otherwise.
27: * @exception IOException If dumping the content failed.
28: */
29:
30: public synchronized boolean newMetadataContent(InputStream in)
31: throws IOException {
32: File file = getFile();
33: boolean created = (!file.exists() || (file.length() == 0));
34: String name = file.getName();
35: File temp = new File(file.getParent(), "#" + name + "#");
36: String iomsg = null;
37: JpegCommentHandler jpegHandler = new JpegCommentHandler(file);
38: // We are not catching IO exceptions here, except to remove temp:
39: try {
40: FileOutputStream fout = new FileOutputStream(temp);
41: char buf[] = new char[4096];
42: Writer writer = jpegHandler.getOutputStreamWriter(fout);
43: InputStreamReader reader = new InputStreamReader(in);
44: for (int got = 0; (got = reader.read(buf)) > 0;)
45: writer.write(buf, 0, got);
46: writer.close();
47: } catch (IOException ex) {
48: iomsg = ex.getMessage();
49: } finally {
50: if (iomsg != null) {
51: temp.delete();
52: throw new IOException(iomsg);
53: } else {
54: if (getBackupFlag()) {
55: File backup = getBackupFile();
56: if (backup.exists())
57: backup.delete();
58: file.renameTo(getBackupFile());
59: }
60: // with some OSes, rename doesn't overwrite so...
61: if (file.exists())
62: file.delete();
63: temp.renameTo(file);
64: // update our attributes for this new content:
65: updateFileAttributes();
66: }
67: }
68: return created;
69: }
70: }
|