litesoft
@ 811
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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.orsup.nonpublic; import java.io.*; import java.sql.*; import java.util.*; import org.litesoft.core.typeutils.*; import org.litesoft.core.util.*; import org.litesoft.db.*; import org.litesoft.logger.*; import org.litesoft.orsup.base.*; import org.litesoft.orsup.otherattributeaccessors.*; import org.litesoft.orsup.selection.*; import org.litesoft.orsup.transact.*; import org.litesoft.util.*; public class Blob extends PersistentObjectImpl<Blob> implements SizeableStreamAccessor { private static final Logger LOGGER = LoggerFactory.getLogger( Blob.class ); private static final ConstructionControl CONSTRUCTION_CONTROL = new ConstructionControl(); public static SimpleFromIdentifier from() { return MyMetaData.INSTANCE; } public Blob( Transaction pTransaction ) // Only used for New { super( MyMetaData.INSTANCE, pTransaction ); mID = getNextSequenceNumber( getClass() ); registerWithTransaction(); } public Blob( ConstructionControl pConstructionControl ) { super( MyMetaData.INSTANCE, CONSTRUCTION_CONTROL, pConstructionControl ); } @Override protected void transactionDisposed() { if ( isStreamOpen() ) { closeStream(); } if ( isNew() && hasParts() ) { removeParts( null ); } super.transactionDisposed(); } @Override public Serializable aboutToCommit( Timestamp pTransactionTimestamp ) { if ( isStreamOpen() ) { throw new IllegalStateException( "Blob Stream Not Closed" ); } if ( !isDeleteRequested() && (getSizeInBytes() < 0) ) { throw new IllegalStateException( "Blob with No Data" ); } return super.aboutToCommit( pTransactionTimestamp ); } @Override protected void justDeletedInTransaction( DBconnection pDBconnection ) { if ( hasParts() ) { removeParts( pDBconnection ); } super.justDeletedInTransaction( pDBconnection ); } public static final AttributeAccessorSCD CD_ID = new Blob.AttributeAccessor_ID(); private Long mID = null; public Long getID() { return mID; } protected void LLsetID( Long pID ) { verifyMutability( CD_ID, mID, pID ); mID = pID; } private static class AttributeAccessor_ID extends AttributeAccessorSCDsimplePersistedReadOnly<Blob> { public AttributeAccessor_ID() { super( "ID", "id", true, _Long ); } @Override public Object getValueOnPO( Blob pPO ) { return pPO.getID(); } @Override public void db_setValueOnPO( Blob pPO, Object pValue ) { pPO.LLsetID( to_Long( pValue ) ); } } public static final AttributeAccessorSCD CD_SizeInBytes = new AttributeAccessor_SizeInBytes(); private Long mSizeInBytes; /** * From: SizeableStreamAccessor * * @return Actual Size or -1 if not readable */ @Override public long getSizeInBytes() { return (mSizeInBytes == null) ? -1 : mSizeInBytes; } private void LLsetSizeInBytes( long pSizeInBytes ) { verifyMutability( CD_SizeInBytes, mSizeInBytes, pSizeInBytes ); mSizeInBytes = pSizeInBytes; } private void addBytes( int pBytesAdded ) { mSizeInBytes = (mSizeInBytes == null) ? (long) pBytesAdded : mSizeInBytes + pBytesAdded; } private static class AttributeAccessor_SizeInBytes extends AttributeAccessorSCDsimplePersistedReadOnly<Blob> { public AttributeAccessor_SizeInBytes() { super( "SizeInBytes", "SizeInBytes", true, _Long ); } @Override public Object getValueOnPO( Blob pPO ) { return pPO.getSizeInBytes(); } @Override public void db_setValueOnPO( Blob pPO, Object pValue ) { pPO.LLsetSizeInBytes( to_long( pValue ) ); } } public static class MyMetaData extends PersistentObjectImplMetaData<Blob> { public static final String OBJECT_NAME = "Blob"; public static final String TABLE_NAME = "Blob"; public static final MyMetaData INSTANCE = new MyMetaData(); private MyMetaData() { super( MyMetaData.OBJECT_NAME, MyMetaData.TABLE_NAME, // new AttributeAccessorKeySet( CD_ID ), // CD_ID, CD_SizeInBytes ); } @Override public Class getPOclass() { return Blob.class; } @Override public Blob createNew( Transaction pTransaction ) { return new Blob( pTransaction ); } @Override public Blob createPOfromFinder() { return new Blob( CONSTRUCTION_CONTROL ); } @Override public String getDisplayValueFormat() { return "${ID}"; } } protected boolean hasParts() { return (getSizeInBytes() > 0); } protected void closeStream() { if ( isStreamOpen() ) { Exception ex; try { mStream.close(); return; } catch ( RuntimeException e ) { ex = e; } catch ( IOException e ) { ex = e; } LOGGER.error.log( ex, "Blob.closeStream()" ); } } protected boolean isStreamOpen() { return (mStream != null); } protected void removeParts( DBconnection pDBconnection ) { WhereClause wc = WhereClauseFactory.INSTANCE.isEqual( Parts4Blobs.CD_BlobID, getID() ); getFinder().getPersistenceHelper().deleteRows( pDBconnection, Parts4Blobs.MyMetaData.INSTANCE, wc ); } @Override public boolean isReadable() { return (getSizeInBytes() >= 0) && !isStreamOpen(); } @Override public boolean isWritable() { return (getSizeInBytes() < 0) && !isStreamOpen() && isNew(); } /** * @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 ( getSizeInBytes() < 0 ) { throw new IllegalStateException( "Not Readable: No Data written" ); } if ( isStreamOpen() ) { throw new IllegalStateException( "Already Reading" ); } InputStream zStream = (getSizeInBytes() == 0) ? new NoPartsInputStream() : new PartsInputStream(); mStream = zStream; return zStream; } /** * @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 ( getSizeInBytes() >= 0 ) { throw new UnsupportedOperationException( "Already Written" ); } if ( isStreamOpen() ) { throw new UnsupportedOperationException( "Already Writing" ); } if ( !isNew() ) { throw new IllegalStateException( "Not New!" ); } OutputStream zStream = new PartsOutputStream(); mStream = zStream; return zStream; } private Closeable mStream = null; private class NoPartsInputStream extends InputStream { @Override public int read() throws IOException { return -1; } @Override public void close() throws IOException { mStream = null; super.close(); } } private static byte[] fromHex( String pHex ) { String zHex = Strings.noEmpty( pHex ); int bLen = zHex.length() / 2; if ( (bLen == 0) || ((bLen + bLen) != zHex.length()) ) { throw new RuntimeException( "fromHex bad length: " + zHex.length() ); } byte[] zBytes = new byte[bLen]; int from = 0; for ( int i = 0; i < bLen; i++ ) { int zByte = Hex.fromCharChecked( zHex.charAt( from++ ) ) << 4; zByte += Hex.fromCharChecked( zHex.charAt( from++ ) ); zBytes[i] = (byte) zByte; } return zBytes; } private static String toHex( byte[] pBytes, int pOffset, int pCount ) { StringBuilder sb = new StringBuilder( pCount + pCount ); while ( pCount-- > 0 ) { int zByte = (pBytes[pOffset++] & 0xFF); sb.append( Hex.toChar( zByte >> 4 ) ); sb.append( Hex.toChar( zByte ) ); } return sb.toString(); } private class PartsInputStream extends InputStream { private POQueryIterator<Parts4Blobs> mIterator; private byte[] mBuffer = new byte[0]; private int mNextReadOffset = 0; private int bufRead() { return (mBuffer[mNextReadOffset++] & 0xFF); } public PartsInputStream() throws IOException { WhereClause wc = WhereClauseFactory.INSTANCE.isEqual( Parts4Blobs.CD_BlobID, getID() ); OrderBy zOrderBy = new OrderBy( Parts4Blobs.CD_Ordinal ); try { mIterator = getNonTransactionalFinder().findAllCursored( Parts4Blobs.class, wc, zOrderBy ); } catch ( RuntimeException e ) { IOException zException = new IOException(); zException.initCause( e ); throw zException; } } @Override public int read() throws IOException { if ( mNextReadOffset < mBuffer.length ) { return bufRead(); } if ( mIterator != null ) { try { if ( mIterator.hasNext() ) { String zHexed2kPart = mIterator.next().getHexed2kPart(); mBuffer = fromHex( zHexed2kPart ); mNextReadOffset = 0; return bufRead(); } } catch ( RuntimeException e ) { releaseIterator(); IOException zException = new IOException(); zException.initCause( e ); throw zException; } releaseIterator(); } return -1; } @Override public void close() throws IOException { releaseIterator(); mStream = null; super.close(); } private void releaseIterator() { if ( mIterator != null ) { mIterator.releaseResources(); mIterator = null; } } @Override protected void finalize() throws Throwable { releaseIterator(); super.finalize(); } } private class PartsOutputStream extends OutputStream { private static final int BUFFER_PER_TRANSACTION = 16 * 1024; private byte[] mBuffer = new byte[BUFFER_PER_TRANSACTION]; private int mNextWriteOffset = 0; private int mNextOridinal = 0; private void bufWrite( int pByte ) { mBuffer[mNextWriteOffset++] = (byte) pByte; } private void newBuffer() { mNextWriteOffset = 0; } @Override public void write( int b ) throws IOException { if ( mNextWriteOffset >= mBuffer.length ) { flush(); newBuffer(); } bufWrite( b ); } @Override public void flush() throws IOException { if ( mNextWriteOffset != 0 ) { Transaction zTrans = getFinder().createTransaction(); int zReadOffset = 0; while ( (zReadOffset + Parts4Blobs.MAX_BYTES_PER_PART) <= mNextWriteOffset ) { new Parts4Blobs( zTrans, getID(), mNextOridinal++, // toHex( mBuffer, zReadOffset, Parts4Blobs.MAX_BYTES_PER_PART ) ); zReadOffset += Parts4Blobs.MAX_BYTES_PER_PART; } if ( zReadOffset < mNextWriteOffset ) { new Parts4Blobs( zTrans, getID(), mNextOridinal++, // toHex( mBuffer, zReadOffset, mNextWriteOffset - zReadOffset ) ); } int zBytesAdded = mNextWriteOffset; newBuffer(); try { zTrans.commit(); } catch ( RuntimeException e ) { IOException zException = new IOException(); zException.initCause( e ); throw zException; } addBytes( zBytesAdded ); } } @Override public void close() throws IOException { if ( mBuffer != null ) { mBuffer = null; mStream = null; } super.close(); } } } |
Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/orsup/nonpublic/Blob.java
Revision | Author | Commited | Message |
---|---|---|---|
811 Diff | GeorgeS | Sat 18 Aug, 2012 13:45:18 +0000 | |
801 Diff | GeorgeS | Wed 15 Aug, 2012 03:59:02 +0000 | |
151 Diff | GeorgeS | Thu 17 Mar, 2011 04:16:22 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
24 Diff | GeorgeS | Wed 24 Feb, 2010 01:51:38 +0000 | |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |