litesoft
@ HEAD
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 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.util.tempdirimageaccessor; import org.litesoft.commonfoundation.base.*; import org.litesoft.server.file.*; import org.litesoft.util.*; import java.io.*; import java.util.*; public class TempDirImageAccessor implements ImageAccessor { private URLconstructor mURLconstructor; private File mExistingImage; private FileNameParts mParts; private TempDirImageAccessor( URLconstructor pURLconstructor, File pExistingImage, FileNameParts pParts ) { Confirm.isNotNull( "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(); } @Override public String getURL() { return mURLconstructor.createURL( mParts ); } @Override public String getOpaqueStoredImageKey() throws IllegalStateException { if ( !isReadable() ) { throw new IllegalStateException( "Image Not Readable" ); } return mExistingImage.getName(); } @Override public boolean isReadable() { return (mExistingImage != null); } @Override public boolean isWritable() { return (mExistingImage == null); } /** * @return Whatever was set (null possible) */ public String getPrefix() { return mParts.getPrefix(); } /** * @return Whatever was set (null possible) */ @Override public String getImageEncoding() { return mParts.getImageEncoding(); } /** * @return Actual Size or 0 if not readable */ @Override public int getImageSizeInBytes() { return (mExistingImage == null) ? 0 : (int) mExistingImage.length(); } /** * @return Actual Width or 0 if not set (or set to 0) */ @Override public int getImageWidth() { return ConstrainTo.notNull( mParts.getImageWidth() ); } /** * @return Actual Height or 0 if not set (or set to 0) */ @Override public int getImageHeight() { return ConstrainTo.notNull( mParts.getImageHeight() ); } /** * @return !null * * @throws IllegalStateException - if not Readable * @throws MissingResourceException - if resource not found * @throws IOException - any other IOException */ @Override 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 */ @Override 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() { @Override public boolean accept( File dir, String name ) { return name.startsWith( zFileNamePrefix ) && name.contains( zUniquePortionMustBeInFileName ); } } ); if ( Currently.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( Directories.temp() ); } 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 ) ); } @Override 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; } } @Override public void flush() throws IOException { try { if ( mOS != null ) { mOS.flush(); } } catch ( IOException e ) { cleanUp(); throw e; } } @Override 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
Revision | Author | Commited | Message |
---|---|---|---|
958 Diff | GeorgeS | Mon 14 Jul, 2014 15:29:35 +0000 | Embrace OSS code bases. |
950 Diff | GeorgeS | Thu 19 Jun, 2014 17:57:04 +0000 | New Lines |
948 Diff | GeorgeS | Sat 07 Jun, 2014 23:42:39 +0000 | Jusefuls Formatter Updated to New Code Format |
947 Diff | GeorgeS | Fri 06 Jun, 2014 23:36:56 +0000 | Correct Spelling of package! |
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
823 Diff | GeorgeS | Sun 19 Aug, 2012 16:10:13 +0000 | |
809 Diff | GeorgeS | Thu 16 Aug, 2012 04:10:46 +0000 | |
804 Diff | GeorgeS | Wed 15 Aug, 2012 12:48:51 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |