Example
An example follows, which uses the classes shown in the following figure.
/*
*
* Lobber is an example of storing BLOBs. BlobClass has three BLOBs:
* imablob which is LobLocator, and imasmallblob, and imanothersmallblob
*
* which are
*
*/


public class Lobber {

private static final int size = 1000;
private static BlobClass atr = null;
private static Enumeration lob_files;

public static void goLobs( String lob_dir ) {

try {

// create a BlobClass object

lob_files = getLobFiles( lob_dir );

// loop thru the files in this directory
if ( lob_files != null ) {
if ( lob_files.hasMoreElements() ) {
try {
String lobIt = (String)lob_files.nextElement();
File lobFile = new File(lobIt);

long len = lobFile.length();
System.out.println( "file length "+len );
InputStream aIs = new FileInputStream(lobFile);
System.out.println( "bytes available on this
InputStream "+aIs.available());

atr = createBlobClassRec(lobIt);

Transaction trx = new Transaction();
trx.start();
// save the class containing the LOB
atr = (BlobClass)
PersistenceHelper.manager.save(atr);

// Save a LOB of unknown length. Note that
// PersistenceServerHelper is used
// rather than PersistenceHelper. This
// is server side only code.

long theLength =
(long)PersistenceServerHelper.manager.updateLob(
(Persistable)atr,
atr.getImablob(),
aIs,
false );
len = lobFile.length();
// save a LOB of known length
System.out.println( "file length "+len );
aIs = new FileInputStream(lobFile);
System.out.println( "bytes available on
this InputStream "+aIs.available());

PersistenceServerHelper.manager.updateLob(
(Persistable)atr,
atr.getImablob(),
aIs,
len,
false );
trx.commit();
trx = null;
aIs.close();

}
catch ( FileNotFoundException fnf )
{ fnf.printStackTrace();}
catch ( IOException ioe )
{ioe.printStackTrace();}
finally {
if ( trx != null ) {
trx.rollback();
throw new RuntimeException("Error
processing lobs");
}
}
}
}

}
catch (WTException WTe) {
WTe.printStackTrace();
}
}

private static Enumeration getLobFiles( String ldir ) {

String[] s_files = null;
Vector aVec = new Vector();
File lob_dir = new File( ldir );

if ( !lob_dir.isDirectory() )
return null;

s_files = lob_dir.list();
for ( int i=0; i
for ( int i=0; i
War dies hilfreich?