KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > internal > image > FileFormat


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.internal.image;
12
13
14 import java.io.*;
15 import org.eclipse.swt.*;
16 import org.eclipse.swt.graphics.*;
17
18 /**
19  * Abstract factory class for loading/unloading images from files or streams
20  * in various image file formats.
21  *
22  */

23 public abstract class FileFormat {
24     static final String JavaDoc FORMAT_PACKAGE = "org.eclipse.swt.internal.image"; //$NON-NLS-1$
25
static final String JavaDoc FORMAT_SUFFIX = "FileFormat"; //$NON-NLS-1$
26
static final String JavaDoc[] FORMATS = {"WinBMP", "WinBMP", "GIF", "WinICO", "JPEG", "PNG", "TIFF", "OS2BMP"}; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$//$NON-NLS-5$ //$NON-NLS-6$//$NON-NLS-7$//$NON-NLS-8$
27

28     LEDataInputStream inputStream;
29     LEDataOutputStream outputStream;
30     ImageLoader loader;
31     int compression;
32
33 /**
34  * Return whether or not the specified input stream
35  * represents a supported file format.
36  */

37 abstract boolean isFileFormat(LEDataInputStream stream);
38
39 abstract ImageData[] loadFromByteStream();
40
41 /**
42  * Read the specified input stream, and return the
43  * device independent image array represented by the stream.
44  */

45 public ImageData[] loadFromStream(LEDataInputStream stream) {
46     try {
47         inputStream = stream;
48         return loadFromByteStream();
49     } catch (Exception JavaDoc e) {
50         if (e instanceof IOException) {
51             SWT.error(SWT.ERROR_IO, e);
52         } else {
53             SWT.error(SWT.ERROR_INVALID_IMAGE, e);
54         }
55         return null;
56     }
57 }
58
59 /**
60  * Read the specified input stream using the specified loader, and
61  * return the device independent image array represented by the stream.
62  */

63 public static ImageData[] load(InputStream is, ImageLoader loader) {
64     FileFormat fileFormat = null;
65     LEDataInputStream stream = new LEDataInputStream(is);
66     boolean isSupported = false;
67     for (int i = 1; i < FORMATS.length; i++) {
68         if (FORMATS[i] != null) {
69             try {
70                 Class JavaDoc clazz = Class.forName(FORMAT_PACKAGE + '.' + FORMATS[i] + FORMAT_SUFFIX);
71                 fileFormat = (FileFormat) clazz.newInstance();
72                 if (fileFormat.isFileFormat(stream)) {
73                     isSupported = true;
74                     break;
75                 }
76             } catch (ClassNotFoundException JavaDoc e) {
77                 FORMATS[i] = null;
78             } catch (Exception JavaDoc e) {
79             }
80         }
81     }
82     if (!isSupported) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
83     fileFormat.loader = loader;
84     return fileFormat.loadFromStream(stream);
85 }
86
87 /**
88  * Write the device independent image array stored in the specified loader
89  * to the specified output stream using the specified file format.
90  */

91 public static void save(OutputStream os, int format, ImageLoader loader) {
92     if (format < 0 || format >= FORMATS.length) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
93     if (FORMATS[format] == null) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
94     if (loader.data == null || loader.data.length < 1) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
95
96     LEDataOutputStream stream = new LEDataOutputStream(os);
97     FileFormat fileFormat = null;
98     try {
99         Class JavaDoc clazz = Class.forName(FORMAT_PACKAGE + '.' + FORMATS[format] + FORMAT_SUFFIX);
100         fileFormat = (FileFormat) clazz.newInstance();
101     } catch (Exception JavaDoc e) {
102         SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
103     }
104     if (format == SWT.IMAGE_BMP_RLE) {
105         switch (loader.data[0].depth) {
106             case 8: fileFormat.compression = 1; break;
107             case 4: fileFormat.compression = 2; break;
108         }
109     }
110     fileFormat.unloadIntoStream(loader, stream);
111 }
112
113 abstract void unloadIntoByteStream(ImageLoader loader);
114
115 /**
116  * Write the device independent image array stored in the specified loader
117  * to the specified output stream.
118  */

119 public void unloadIntoStream(ImageLoader loader, LEDataOutputStream stream) {
120     try {
121         outputStream = stream;
122         unloadIntoByteStream(loader);
123         outputStream.flush();
124     } catch (Exception JavaDoc e) {
125         try {outputStream.flush();} catch (Exception JavaDoc f) {}
126         SWT.error(SWT.ERROR_IO, e);
127     }
128 }
129 }
130
Popular Tags