Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.util.tempdirimageaccessor;

import java.io.*;
import java.util.*;

import org.litesoft.core.typeutils.*;
import org.litesoft.util.*;

public class TempDirImageAccessor implements ImageAccessor
{
    public static final Integer ZERO = 0;

    private URLconstructor mURLconstructor;
    private File mExistingImage;
    private FileNameParts mParts;

    private TempDirImageAccessor( URLconstructor pURLconstructor, File pExistingImage, FileNameParts pParts )
    {
        Objects.assertNotNull( "URLconstructor", mURLconstructor = pURLconstructor );
        mExistingImage = pExistingImage;
        mParts = pParts;
    }

    public TempDirImageAccessor( URLconstructor pURLconstructor, File pExistingImage )
    {
        this( pURLconstructor, pExistingImage, FileNameParts.createFromFileName( pExistingImage.getName() ) );
    }

    public TempDirImageAccessor( URLconstructor pURLconstructor, //
                                 String pPrefix, String pExtraInfo, //
                                 int pImageWidth, int pImageHeight, //
                                 String pImageEncoding )
    {
        this( pURLconstructor, null, new FileNameParts( pPrefix, pExtraInfo, //
                                                        pImageWidth, pImageHeight, //
                                                        pImageEncoding ) );
    }

    public static void deleteByOpaqueStoredImageKey( String pOpaqueStoredImageKey )
    {
        File zFile = new File( tempDirectory(), pOpaqueStoredImageKey );
        zFile.delete();
    }

    public String getURL()
    {
        return mURLconstructor.createURL( mParts );
    }

    public String getOpaqueStoredImageKey()
            throws IllegalStateException
    {
        if ( !isReadable() )
        {
            throw new IllegalStateException( "Image Not Readable" );
        }
        return mExistingImage.getName();
    }

    public boolean isReadable()
    {
        return (mExistingImage != null);
    }

    public boolean isWritable()
    {
        return (mExistingImage == null);
    }

    /**
     * @return Whatever was set (null possible)
     */
    public String getPrefix()
    {
        return mParts.getPrefix();
    }

    /**
     * @return Whatever was set (null possible)
     */
    public String getImageEncoding()
    {
        return mParts.getImageEncoding();
    }

    /**
     * @return Actual Size or 0 if not readable
     */
    public int getImageSizeInBytes()
    {
        return (mExistingImage == null) ? 0 : (int) mExistingImage.length();
    }

    /**
     * @return Actual Width or 0 if not set (or set to 0)
     */
    public int getImageWidth()
    {
        return Objects.deNull( mParts.getImageWidth(), ZERO );
    }

    /**
     * @return Actual Height or 0 if not set (or set to 0)
     */
    public int getImageHeight()
    {
        return Objects.deNull( mParts.getImageHeight(), ZERO );
    }

    /**
     * @return !null
     *
     * @throws IllegalStateException    - if not Readable
     * @throws MissingResourceException - if resource not found
     * @throws IOException              - any other IOException
     */
    public InputStream readOpen()
            throws IllegalStateException, MissingResourceException, IOException
    {
        if ( !isReadable() )
        {
            throw new IllegalStateException( "Not Readable: No Image written" );
        }
        try
        {
            return new FileInputStream( mExistingImage );
        }
        catch ( FileNotFoundException e )
        {
            MissingResourceException zException = new MissingResourceException( e.getMessage(),
                                                                                TempDirImageAccessor.class.getName(),
                                                                                "Not Found" );
            zException.initCause( e );
            throw zException;
        }
    }

    /**
     * @return !null
     *
     * @throws UnsupportedOperationException - if not Writable (ie already written)
     * @throws IllegalStateException         - if something required for writing is missing
     * @throws IOException                   - any other IOException
     */
    public OutputStream writeOpen()
            throws UnsupportedOperationException, IllegalStateException, IOException
    {
        if ( !isWritable() )
        {
            throw new UnsupportedOperationException( "Not Writable" );
        }
        File zFile = mParts.createFileAt( tempDirectory() );
        zFile.deleteOnExit();
        return new MyOutputStream( zFile );
    }

    public static TempDirImageAccessor findBy( URLconstructor pURLconstructor, //
                                               String pPrefix, String pExtraInfo, String pUniquingPortion )
    {
        final String zFileNamePrefix = new FileNameParts( pPrefix, pExtraInfo ).createFileNamePrefix();
        final String zUniquePortionMustBeInFileName =
                FileNameParts.delineateUniquingPortion( pUniquingPortion );

        File[] zFiles = tempDirectory().listFiles( new FilenameFilter()
        {
            public boolean accept( File dir, String name )
            {
                return name.startsWith( zFileNamePrefix ) && name.contains( zUniquePortionMustBeInFileName );
            }
        } );
        if ( Utils.isNullOrEmpty( zFiles ) )
        {
            return null;
        }
        if ( zFiles.length > 1 )
        {
            throw new IllegalStateException( "Multiple Files found starting w/ '" + zFileNamePrefix +
                                             "', and containing '" + zUniquePortionMustBeInFileName + "'" );
        }
        return new TempDirImageAccessor( pURLconstructor, zFiles[0] );
    }

    private static File tempDirectory()
    {
        return new File( Utils.tempDirectory() );
    }

    private class MyOutputStream extends OutputStream
    {
        private File mFile;
        private OutputStream mOS;
        private int mSizeInBytes = 0;

        public MyOutputStream( File pFile )
                throws FileNotFoundException
        {
            mFile = pFile;
            mOS = new BufferedOutputStream( new FileOutputStream( pFile ) );
        }

        public void write( int b )
                throws IOException
        {
            if ( ++mSizeInBytes < 0 )
            {
                throw new IOException( "Image is too large for Web Content." );
            }
            try
            {
                mOS.write( b );
            }
            catch ( IOException e )
            {
                cleanUp();
                throw e;
            }
        }

        public void flush()
                throws IOException
        {
            try
            {
                if ( mOS != null )
                {
                    mOS.flush();
                }
            }
            catch ( IOException e )
            {
                cleanUp();
                throw e;
            }
        }

        public void close()
                throws IOException
        {
            try
            {
                if ( mOS != null )
                {
                    OutputStream zOS = mOS;
                    mOS = null;
                    zOS.close();
                }
            }
            catch ( IOException e )
            {
                cleanUp();
                throw e;
            }

            mExistingImage = mFile;
        }

        private void cleanUp()
        {
            Utils.dispose( mOS );
            mOS = null;
            try
            {
                if ( mFile != null )
                {
                    mFile.delete();
                }
            }
            catch ( Exception e )
            {
                // Whatever
            }
            mFile = null;
        }
    }
}

Commits for litesoft/trunk/Java/core/jvm1.5/src/org/litesoft/util/tempdirimageaccessor/TempDirImageAccessor.java

Diff revisions: vs.
Revision Author Commited Message
804 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 12:48:51 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000