blob.java

来自「Java的面向对象数据库系统的源代码」· Java 代码 · 共 119 行

JAVA
119
字号
// $Id: BLOB.java,v 1.2 2003/11/23 14:16:53 per_nyfelt Exp $import java.io.*;import java.util.zip.*;import org.ozoneDB.*;import org.ozoneDB.blob.*;public class BLOB {            /** */    public static void main( String[] _args ) throws Exception {        String host = "localhost";        int port = 3333;        int SIZE = 1000 * 1024;                // make a database connection        RemoteDatabase db = new RemoteDatabase();        try {            db.open( host, port );        } catch (Exception e) {            System.out.println( "No ozone server found!" );            System.exit( 0 );        }         db.reloadClasses();        System.out.println( "connected..." );                // create a new BLOB        System.out.println( "creating BLOB..." );        String name = "BLOB1";        BLOBContainer blob = (BLOBContainer)db.createObject( BLOBContainerImpl.class.getName(), 0, name );        blob.init( 32 * 1024 );                        // block-write        BLOBOutputStream out = new BLOBOutputStream( blob );        CRC32 checksumOut = new CRC32();        CheckedOutputStream checkedOut = new CheckedOutputStream( out, checksumOut );                System.out.println( "\nblock-writing BLOB data... (size: " + SIZE + ")" );        byte[] bufferOut = new byte[SIZE];        for (int i = 0; i < SIZE; i++) {            bufferOut[i] = (byte)i;        }                 long start = System.currentTimeMillis();        checkedOut.write( bufferOut );        long stop = System.currentTimeMillis();        System.out.println( "   time: " + (stop - start) + "msec" );                        // block-read and verify        System.out.print( "\nblock-reading BLOB data..." );        BLOBInputStream in = new BLOBInputStream( blob );        CRC32 checksumIn = new CRC32();        CheckedInputStream checkedIn = new CheckedInputStream( in, checksumIn );                start = System.currentTimeMillis();        byte[] bufferIn = new byte[64 * 1024];        int got;        while ((got = checkedIn.read( bufferIn )) != -1) {            System.out.print( "." );            System.out.flush();        }         stop = System.currentTimeMillis();        System.out.println( "" );        System.out.println( "   CRC-Out: " + checksumOut.getValue() );        System.out.println( "   CRC-In:  " + checksumIn.getValue() );        System.out.println( "   time: " + (stop - start) + "msec" );                        // buffered write        out = new BLOBOutputStream( blob );        BufferedOutputStream bout = new BufferedOutputStream( out, 32 * 1024 );        DataOutputStream dataOut = new DataOutputStream( bout );                System.out.println( "\nbuffered byte-writing BLOB data... (size: " + SIZE + ")" );        start = System.currentTimeMillis();                ExternalTransaction tx = db.newTransaction();        tx.begin();        for (int i = 0; i < SIZE; i++) {            dataOut.writeByte( i );        }         dataOut.flush();        tx.commit();        stop = System.currentTimeMillis();        System.out.println( "   time: " + (stop - start) + "msec" );                        // buffered read and verify data        System.out.println( "\nbuffered byte-reading BLOB data..." );        in = new BLOBInputStream( blob );        BufferedInputStream bin = new BufferedInputStream( in, 33 * 1024 );        DataInputStream dataIn = new DataInputStream( bin );                // read the BLOB data        start = System.currentTimeMillis();        tx = db.newTransaction();        tx.begin();        for (int i = 0; i < SIZE; i++) {            int val = dataIn.readByte();            if ((byte)val != (byte)i) {                System.out.println( "   ERROR: position: " + i );            }         }         tx.commit();        stop = System.currentTimeMillis();        System.out.println( "   time: " + (stop - start) + "msec" );                        // delete the BLOB        System.out.println( "\ndeleting BLOB..." );        db.deleteObject( blob );    } }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?