Example usage for java.io ObjectOutputStream flush

List of usage examples for java.io ObjectOutputStream flush

Introduction

In this page you can find the example usage for java.io ObjectOutputStream flush.

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the stream.

Usage

From source file:org.apache.axis2.engine.ObjectSaveTest.java

public void testArrayList() throws Exception {
    File theFile = null;/*w  ww . j a  v  a  2s .c om*/
    String theFilename = null;
    boolean saved = false;
    boolean restored = false;
    boolean done = false;
    boolean comparesOK = false;

    log.debug("ObjectSaveTest:testArrayList():  BEGIN ---------------");

    // ---------------------------------------------------------
    // setup the object to use
    // ---------------------------------------------------------
    ArrayList obj = new ArrayList();
    obj.add(new Integer(1));
    obj.add(new Integer(2));
    obj.add(new Integer(3));
    obj.add(new String("string1"));
    obj.add(new String("string2"));
    obj.add(System.out);
    obj.add(new Integer(4));
    obj.add(new Integer(5));
    obj.add(new Integer(6));

    int initial_size = obj.size();

    // ---------------------------------------------------------
    // setup a temporary file to use
    // ---------------------------------------------------------
    try {
        theFile = File.createTempFile("arraylistTest", null);
        theFilename = theFile.getName();
        log.debug("ObjectSaveTest:testArrayList(): temp file = [" + theFilename + "]");
    } catch (Exception ex) {
        log.debug("ObjectSaveTest:testArrayList(): error creating temp file = [" + ex.getMessage() + "]");
        theFile = null;
    }

    if (theFile != null) {
        // ---------------------------------------------------------
        // save to the temporary file
        // ---------------------------------------------------------
        try {
            // setup an output stream to a physical file
            FileOutputStream outStream = new FileOutputStream(theFile);

            // attach a stream capable of writing objects to the
            // stream connected to the file
            ObjectOutputStream outObjStream = new ObjectOutputStream(outStream);

            // try to save
            log.debug("ObjectSaveTest:testArrayList(): saving .....");
            saved = false;
            ObjectStateUtils.writeArrayList(outObjStream, obj, "testObject:ArrayList");

            // close out the streams
            outObjStream.flush();
            outObjStream.close();
            outStream.flush();
            outStream.close();

            saved = true;
            log.debug("ObjectSaveTest:testArrayList(): ....save operation completed.....");

            long filesize = theFile.length();
            log.debug("ObjectSaveTest:testArrayList(): file size after save [" + filesize + "]   temp file = ["
                    + theFilename + "]");
        } catch (Exception ex2) {
            log.debug("ObjectSaveTest:testArrayList(): error during save [" + ex2.getClass().getName() + " : "
                    + ex2.getMessage() + "]");
            ex2.printStackTrace();
        }

        assertTrue(saved);

        // ---------------------------------------------------------
        // restore from the temporary file
        // ---------------------------------------------------------
        ArrayList restored_obj = null;

        try {
            // setup an input stream to the file
            FileInputStream inStream = new FileInputStream(theFile);

            // attach a stream capable of reading objects from the
            // stream connected to the file
            ObjectInputStream inObjStream = new ObjectInputStream(inStream);

            // try to restore the options
            log.debug("ObjectSaveTest:testArrayList(): restoring .....");
            restored = false;
            restored_obj = ObjectStateUtils.readArrayList(inObjStream, "testObject:ArrayList");
            inObjStream.close();
            inStream.close();

            restored = true;
            log.debug("ObjectSaveTest:testArrayList(): ....restored operation completed.....");

        } catch (Exception ex2) {
            log.debug("ObjectSaveTest:testArrayList(): error during restore [" + ex2.getClass().getName()
                    + " : " + ex2.getMessage() + "]");
            ex2.printStackTrace();
        }

        // if the save/restore of the object succeeded,
        // then don't keep the temporary file around
        boolean removeTmpFile = saved && restored;
        if (removeTmpFile) {
            try {
                theFile.delete();
            } catch (Exception e) {
                // just absorb it
            }
        }

        assertTrue(restored);

        if (restored_obj != null) {
            int restored_size = restored_obj.size();
            if (restored_size == (initial_size - 1)) {
                comparesOK = true;
            }
        }

        // TODO: check for exact entries

        assertTrue(comparesOK);

        // indicate that the temp file was created ok
        done = true;
    }

    // this is false when there are problems with the temporary file
    assertTrue(done);

    log.debug("ObjectSaveTest:testArrayList():  END ---------------");
}

From source file:org.apache.axis2.engine.ObjectSaveTest.java

public void testLinkedList() throws Exception {
    File theFile = null;//  ww w .  j a  v a 2s.  com
    String theFilename = null;
    boolean saved = false;
    boolean restored = false;
    boolean done = false;
    boolean comparesOK = false;

    log.debug("ObjectSaveTest:testLinkedList():  BEGIN ---------------");

    // ---------------------------------------------------------
    // setup the object to use
    // ---------------------------------------------------------
    LinkedList obj = new LinkedList();
    obj.add(new Integer(1));
    obj.add(new Integer(2));
    obj.add(new Integer(3));
    obj.add(new String("string1"));
    obj.add(new String("string2"));
    obj.add(System.in);
    obj.add(new Integer(4));
    obj.add(new Integer(5));
    obj.add(new Integer(6));

    int initial_size = obj.size();

    // ---------------------------------------------------------
    // setup a temporary file to use
    // ---------------------------------------------------------
    try {
        theFile = File.createTempFile("linkedlistTest", null);
        theFilename = theFile.getName();
        log.debug("ObjectSaveTest:testLinkedList(): temp file = [" + theFilename + "]");
    } catch (Exception ex) {
        log.debug("ObjectSaveTest:testLinkedList(): error creating temp file = [" + ex.getMessage() + "]");
        theFile = null;
    }

    if (theFile != null) {
        // ---------------------------------------------------------
        // save to the temporary file
        // ---------------------------------------------------------
        try {
            // setup an output stream to a physical file
            FileOutputStream outStream = new FileOutputStream(theFile);

            // attach a stream capable of writing objects to the
            // stream connected to the file
            ObjectOutputStream outObjStream = new ObjectOutputStream(outStream);

            // try to save
            log.debug("ObjectSaveTest:testLinkedList(): saving .....");
            saved = false;
            ObjectStateUtils.writeLinkedList(outObjStream, obj, "testObject:LinkedList");

            // close out the streams
            outObjStream.flush();
            outObjStream.close();
            outStream.flush();
            outStream.close();

            saved = true;
            log.debug("ObjectSaveTest:testLinkedList(): ....save operation completed.....");

            long filesize = theFile.length();
            log.debug("ObjectSaveTest:testLinkedList(): file size after save [" + filesize + "]   temp file = ["
                    + theFilename + "]");
        } catch (Exception ex2) {
            log.debug("ObjectSaveTest:testLinkedList(): error during save [" + ex2.getClass().getName() + " : "
                    + ex2.getMessage() + "]");
            ex2.printStackTrace();
        }

        assertTrue(saved);

        // ---------------------------------------------------------
        // restore from the temporary file
        // ---------------------------------------------------------
        LinkedList restored_obj = null;

        try {
            // setup an input stream to the file
            FileInputStream inStream = new FileInputStream(theFile);

            // attach a stream capable of reading objects from the
            // stream connected to the file
            ObjectInputStream inObjStream = new ObjectInputStream(inStream);

            // try to restore the options
            log.debug("ObjectSaveTest:testLinkedList(): restoring .....");
            restored = false;
            restored_obj = ObjectStateUtils.readLinkedList(inObjStream, "testObject:LinkedList");
            inObjStream.close();
            inStream.close();

            restored = true;
            log.debug("ObjectSaveTest:testLinkedList(): ....restored operation completed.....");

        } catch (Exception ex2) {
            log.debug("ObjectSaveTest:testLinkedList(): error during restore [" + ex2.getClass().getName()
                    + " : " + ex2.getMessage() + "]");
            ex2.printStackTrace();
        }

        // if the save/restore of the object succeeded,
        // then don't keep the temporary file around
        boolean removeTmpFile = saved && restored;
        if (removeTmpFile) {
            try {
                theFile.delete();
            } catch (Exception e) {
                // just absorb it
            }
        }

        assertTrue(restored);

        if (restored_obj != null) {
            int restored_size = restored_obj.size();
            if (restored_size == (initial_size - 1)) {
                comparesOK = true;
            }
        }

        // TODO: check for exact entries

        assertTrue(comparesOK);

        // indicate that the temp file was created ok
        done = true;
    }

    // this is false when there are problems with the temporary file
    assertTrue(done);

    log.debug("ObjectSaveTest:testLinkedList():  END ---------------");
}

From source file:com.izforge.izpack.compiler.packager.impl.MultiVolumePackager.java

/**
 * Writes the pack files./*  www . j  a  v  a2 s . c o  m*/
 * <p/>
 * The file data is written to <tt>volumes</tt>, whilst the meta-data is written to <tt>packStream</tt>.
 *
 * @param packInfo   the pack information
 * @param volumes    the volumes to write to
 * @param pack       the pack
 * @param packStream the stream to write the pack meta-data to
 * @param targetDir  the target directory for loose files
 * @throws IOException for any I/O error
 */
private void writePackFiles(PackInfo packInfo, FileSpanningOutputStream volumes, Pack pack,
        ObjectOutputStream packStream, File targetDir) throws IOException {
    // write the file meta-data
    Set<PackFile> files = packInfo.getPackFiles();
    packStream.writeInt(files.size());

    for (PackFile packfile : files) {
        XPackFile pf = new XPackFile(packfile);
        File file = packInfo.getFile(packfile);
        logger.fine("Next file: " + file.getAbsolutePath());

        if (!pf.isDirectory()) {
            if (!pack.isLoose()) {
                writePackFile(file, volumes, pf);
            } else {
                // just copy the file to the target directory
                FileUtils.copyFile(file, new File(targetDir, pf.getRelativeSourcePath()));
            }
        }

        // write pack file meta-data
        packStream.writeObject(pf);
        packStream.flush(); // make sure it is written
        // even if not written, it counts towards pack size
        pack.addFileSize(pf.length());
    }

    if (pack.getFileSize() > pack.getSize()) {
        pack.setSize(pack.getFileSize());
    }
}

From source file:org.apache.axis2.engine.ObjectSave2Test.java

public void testObjectSerializable() throws Exception {
    File theFile = null;/*from   ww w  . j a v a 2 s  .c o  m*/
    String theFilename = null;
    boolean saved = false;
    boolean restored = false;
    boolean done = false;

    log.debug("ObjectSaveTest:testObjectSerializable():  BEGIN ---------------");

    // ---------------------------------------------------------
    // setup an object to use
    // ---------------------------------------------------------
    MetaDataEntry obj = new MetaDataEntry("object_1", "object_1");

    // ---------------------------------------------------------
    // setup a temporary file to use
    // ---------------------------------------------------------
    try {
        theFile = File.createTempFile("objectTest", null);
        theFilename = theFile.getName();
        log.debug("ObjectSaveTest:testObjectSerializable(): temp file = [" + theFilename + "]");
    } catch (Exception ex) {
        log.debug("ObjectSaveTest:testObjectSerializable(): error creating temp file = [" + ex.getMessage()
                + "]");
        theFile = null;
    }

    if (theFile != null) {
        // ---------------------------------------------------------
        // save to the temporary file
        // ---------------------------------------------------------
        try {
            // setup an output stream to a physical file
            FileOutputStream outStream = new FileOutputStream(theFile);

            // attach a stream capable of writing objects to the
            // stream connected to the file
            ObjectOutputStream outObjStream = new ObjectOutputStream(outStream);
            SafeObjectOutputStream out = SafeObjectOutputStream.install(outObjStream);

            // try to save
            log.debug("ObjectSaveTest:testObjectSerializable(): saving .....");
            saved = false;
            out.writeObject(obj);

            // close out the streams
            outObjStream.flush();
            outObjStream.close();
            outStream.flush();
            outStream.close();

            saved = true;
            log.debug("ObjectSaveTest:testObjectSerializable(): " + "....save operation completed.....");

            long filesize = theFile.length();
            log.debug("ObjectSaveTest:testObjectSerializable(): file size after save [" + filesize
                    + "]   temp file = [" + theFilename + "]");
        } catch (Exception ex2) {
            log.debug("ObjectSaveTest:testObjectSerializable(): error during save [" + ex2.getClass().getName()
                    + " : " + ex2.getMessage() + "]");
            ex2.printStackTrace();
        }

        assertTrue(saved);

        // ---------------------------------------------------------
        // restore from the temporary file
        // ---------------------------------------------------------
        try {
            // setup an input stream to the file
            FileInputStream inStream = new FileInputStream(theFile);

            // attach a stream capable of reading objects from the
            // stream connected to the file
            ObjectInputStream inObjStream = new ObjectInputStream(inStream);
            SafeObjectInputStream in = SafeObjectInputStream.install(inObjStream);

            // try to restore the options
            log.debug("ObjectSaveTest:testObjectSerializable(): restoring .....");
            restored = false;
            MetaDataEntry restored_obj = (MetaDataEntry) in.readObject();
            inObjStream.close();
            inStream.close();

            restored = true;
            log.debug("ObjectSaveTest:testObjectSerializable(): ....restored operation completed.....");

        } catch (Exception ex2) {
            log.debug("ObjectSaveTest:testObjectSerializable(): error during restore ["
                    + ex2.getClass().getName() + " : " + ex2.getMessage() + "]");
            ex2.printStackTrace();
        }

        assertTrue(restored);

        // if the save/restore of the object succeeded,
        // then don't keep the temporary file around
        boolean removeTmpFile = saved && restored;
        if (removeTmpFile) {
            try {
                theFile.delete();
            } catch (Exception e) {
                // just absorb it
            }
        }

        // indicate that the temp file was created ok
        done = true;
    }

    // this is false when there are problems with the temporary file
    assertTrue(done);

    log.debug("ObjectSaveTest:testObjectSerializable():  END ---------------");
}

From source file:com.izforge.izpack.compiler.packager.impl.MultiVolumePackager.java

/**
 * Writes a pack./*from w  ww.  j  av a2s.com*/
 * <p/>
 * Pack information is written to the installer jar, while the actual files are written to the volumes.
 *
 * @param packInfo  the pack information
 * @param volumes   the volumes
 * @param targetDir the target directory for loosefiles
 * @throws IOException for any I/O error
 */
private void writePack(PackInfo packInfo, FileSpanningOutputStream volumes, File targetDir) throws IOException {
    Pack pack = packInfo.getPack();
    pack.setFileSize(0);

    String name = pack.getName();
    sendMsg("Writing Pack: " + name, PackagerListener.MSG_VERBOSE);
    logger.fine("Writing Pack: " + name);
    ZipEntry entry = new ZipEntry(RESOURCES_PATH + "packs/pack-" + name);

    JarOutputStream installerJar = getInstallerJar();
    installerJar.putNextEntry(entry);
    ObjectOutputStream packStream = new ObjectOutputStream(installerJar);

    writePackFiles(packInfo, volumes, pack, packStream, targetDir);

    // Write out information about parsable files
    packStream.writeInt(packInfo.getParsables().size());
    for (ParsableFile file : packInfo.getParsables()) {
        packStream.writeObject(file);
    }

    // Write out information about executable files
    packStream.writeInt(packInfo.getExecutables().size());
    for (ExecutableFile file : packInfo.getExecutables()) {
        packStream.writeObject(file);
    }

    // Write out information about update check files
    packStream.writeInt(packInfo.getUpdateChecks().size());
    for (UpdateCheck check : packInfo.getUpdateChecks()) {
        packStream.writeObject(check);
    }

    // Cleanup
    packStream.flush();
}

From source file:org.kepler.objectmanager.cache.LocalRepositoryManager.java

/**
 * Serialize the local save repository to a file on disk so it can be loaded
 * the next time Kepler starts./*from  www .  j a  v  a 2 s  . c om*/
 */
private void serializeLocalSaveRepo() {
    if (isDebugging)
        log.debug("serializeLocalSaveRepo()");

    File localSaveRepoFile = new File(_localSaveRepoFileName);
    if (localSaveRepoFile.exists()) {
        if (isDebugging)
            log.debug("delete " + localSaveRepoFile);
        localSaveRepoFile.delete();
    }
    try {
        OutputStream os = new FileOutputStream(localSaveRepoFile);
        ObjectOutputStream oos = new ObjectOutputStream(os);
        oos.writeObject(_localSaveRepo.getDefaultDirectory());
        oos.flush();
        oos.close();
        if (isDebugging) {
            log.debug("wrote " + localSaveRepoFile);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.owasp.dependencycheck.maven.DependencyCheckMojo.java

/**
 * Writes the scan data to disk. This is used to serialize the scan data between the "check" and "aggregate" phase.
 *
 * @return the File object referencing the data file that was written
 */// w ww.  j a  va 2 s. com
@Override
protected File writeDataFile() {
    File file = null;
    if (engine != null && getProject().getContextValue(this.getDataFileContextKey()) == null) {
        file = new File(getProject().getBuild().getDirectory(), getDataFileName());
        OutputStream os = null;
        OutputStream bos = null;
        ObjectOutputStream out = null;
        try {
            os = new FileOutputStream(file);
            bos = new BufferedOutputStream(os);
            out = new ObjectOutputStream(bos);
            out.writeObject(engine.getDependencies());
            out.flush();

            //call reset to prevent resource leaks per
            //https://www.securecoding.cert.org/confluence/display/java/SER10-J.+Avoid+memory+and+resource+leaks+during+serialization
            out.reset();

        } catch (IOException ex) {
            LOGGER.log(Level.WARNING, "Unable to create data file used for report aggregation; "
                    + "if report aggregation is being used the results may be incomplete.");
            LOGGER.log(Level.FINE, ex.getMessage(), ex);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException ex) {
                    LOGGER.log(Level.FINEST, "ignore", ex);
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException ex) {
                    LOGGER.log(Level.FINEST, "ignore", ex);
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException ex) {
                    LOGGER.log(Level.FINEST, "ignore", ex);
                }
            }
        }
    }
    return file;
}

From source file:org.apache.axis2.engine.ObjectSave2Test.java

public void testArrayList() throws Exception {
    File theFile = null;//  www.  ja v  a 2 s . com
    String theFilename = null;
    boolean saved = false;
    boolean restored = false;
    boolean done = false;
    boolean comparesOK = false;

    log.debug("ObjectSaveTest:testArrayList():  BEGIN ---------------");

    // ---------------------------------------------------------
    // setup the object to use
    // ---------------------------------------------------------
    ArrayList obj = new ArrayList();
    obj.add(new Integer(1));
    obj.add(new Integer(2));
    obj.add(new Integer(3));
    obj.add(new String("string1"));
    obj.add(new String("string2"));
    obj.add(System.out);
    obj.add(new Integer(4));
    obj.add(new Integer(5));
    obj.add(new Integer(6));

    int initial_size = obj.size();

    // ---------------------------------------------------------
    // setup a temporary file to use
    // ---------------------------------------------------------
    try {
        theFile = File.createTempFile("arraylistTest", null);
        theFilename = theFile.getName();
        log.debug("ObjectSaveTest:testArrayList(): temp file = [" + theFilename + "]");
    } catch (Exception ex) {
        log.debug("ObjectSaveTest:testArrayList(): error creating temp file = [" + ex.getMessage() + "]");
        theFile = null;
    }

    if (theFile != null) {
        // ---------------------------------------------------------
        // save to the temporary file
        // ---------------------------------------------------------
        try {
            // setup an output stream to a physical file
            FileOutputStream outStream = new FileOutputStream(theFile);

            // attach a stream capable of writing objects to the
            // stream connected to the file
            ObjectOutputStream outObjStream = new ObjectOutputStream(outStream);
            SafeObjectOutputStream out = SafeObjectOutputStream.install(outObjStream);

            // try to save
            log.debug("ObjectSaveTest:testArrayList(): saving .....");
            saved = false;
            out.writeList(obj);

            // close out the streams
            outObjStream.flush();
            outObjStream.close();
            outStream.flush();
            outStream.close();

            saved = true;
            log.debug("ObjectSaveTest:testArrayList(): ....save operation completed.....");

            long filesize = theFile.length();
            log.debug("ObjectSaveTest:testArrayList(): file size after save [" + filesize + "]   temp file = ["
                    + theFilename + "]");
        } catch (Exception ex2) {
            log.debug("ObjectSaveTest:testArrayList(): error during save [" + ex2.getClass().getName() + " : "
                    + ex2.getMessage() + "]");
            ex2.printStackTrace();
        }

        assertTrue(saved);

        // ---------------------------------------------------------
        // restore from the temporary file
        // ---------------------------------------------------------
        ArrayList restored_obj = null;

        try {
            // setup an input stream to the file
            FileInputStream inStream = new FileInputStream(theFile);

            // attach a stream capable of reading objects from the
            // stream connected to the file
            ObjectInputStream inObjStream = new ObjectInputStream(inStream);
            SafeObjectInputStream in = SafeObjectInputStream.install(inObjStream);

            // try to restore the options
            log.debug("ObjectSaveTest:testArrayList(): restoring .....");
            restored = false;
            restored_obj = in.readArrayList();
            inObjStream.close();
            inStream.close();

            restored = true;
            log.debug("ObjectSaveTest:testArrayList(): ....restored operation completed.....");

        } catch (Exception ex2) {
            log.debug("ObjectSaveTest:testArrayList(): error during restore [" + ex2.getClass().getName()
                    + " : " + ex2.getMessage() + "]");
            ex2.printStackTrace();
        }

        // if the save/restore of the object succeeded,
        // then don't keep the temporary file around
        boolean removeTmpFile = saved && restored;
        if (removeTmpFile) {
            try {
                theFile.delete();
            } catch (Exception e) {
                // just absorb it
            }
        }

        assertTrue(restored);

        if (restored_obj != null) {
            int restored_size = restored_obj.size();
            if (restored_size == (initial_size - 1)) {
                comparesOK = true;
            }
        }

        // TODO: check for exact entries

        assertTrue(comparesOK);

        // indicate that the temp file was created ok
        done = true;
    }

    // this is false when there are problems with the temporary file
    assertTrue(done);

    log.debug("ObjectSaveTest:testArrayList():  END ---------------");
}

From source file:org.apache.axis2.engine.ObjectSave2Test.java

public void testHashMap() throws Exception {
    File theFile = null;//w w  w .  ja v a 2s . c  o m
    String theFilename = null;
    boolean saved = false;
    boolean restored = false;
    boolean done = false;
    boolean comparesOK = false;

    log.debug("ObjectSaveTest:testHashMap():  BEGIN ---------------");

    // ---------------------------------------------------------
    // setup the object to use
    // ---------------------------------------------------------
    HashMap obj = new HashMap();
    obj.put(new String("key1"), new Integer(1));
    obj.put(new String("key2"), new Integer(2));
    obj.put(new String("key3"), new String("value1"));
    obj.put(new String("key4"), System.out);
    obj.put(new String("key5"), new Integer(3));
    obj.put(new String("key6"), new Integer(4));
    obj.put(new String("key7"), System.err);
    obj.put(new String("key8"), new Integer(5));
    obj.put(new String("key9"), new Integer(6));
    obj.put(new NotSerializableObject("TestForHashMapKey"), new Integer(7));
    obj.put(new String("key10"), new Integer(8));

    int initial_size = obj.size();

    // ---------------------------------------------------------
    // setup a temporary file to use
    // ---------------------------------------------------------
    try {
        theFile = File.createTempFile("hashmapTest", null);
        theFilename = theFile.getName();
        log.debug("ObjectSaveTest:testHashMap(): temp file = [" + theFilename + "]");
    } catch (Exception ex) {
        log.debug("ObjectSaveTest:testHashMap(): error creating temp file = [" + ex.getMessage() + "]");
        theFile = null;
    }

    if (theFile != null) {
        // ---------------------------------------------------------
        // save to the temporary file
        // ---------------------------------------------------------
        try {
            // setup an output stream to a physical file
            FileOutputStream outStream = new FileOutputStream(theFile);

            // attach a stream capable of writing objects to the
            // stream connected to the file
            ObjectOutputStream outObjStream = new ObjectOutputStream(outStream);
            SafeObjectOutputStream out = SafeObjectOutputStream.install(outObjStream);

            // try to save
            log.debug("ObjectSaveTest:testHashMap(): saving .....");
            saved = false;
            out.writeMap(obj);

            // close out the streams
            outObjStream.flush();
            outObjStream.close();
            outStream.flush();
            outStream.close();

            saved = true;
            log.debug("ObjectSaveTest:testHashMap(): ....save operation completed.....");

            long filesize = theFile.length();
            log.debug("ObjectSaveTest:testHashMap(): file size after save [" + filesize + "]   temp file = ["
                    + theFilename + "]");
        } catch (Exception ex2) {
            log.debug("ObjectSaveTest:testHashMap(): error during save [" + ex2.getClass().getName() + " : "
                    + ex2.getMessage() + "]");
            ex2.printStackTrace();
        }

        assertTrue(saved);

        // ---------------------------------------------------------
        // restore from the temporary file
        // ---------------------------------------------------------
        HashMap restored_obj = null;

        try {
            // setup an input stream to the file
            FileInputStream inStream = new FileInputStream(theFile);

            // attach a stream capable of reading objects from the
            // stream connected to the file
            ObjectInputStream inObjStream = new ObjectInputStream(inStream);
            SafeObjectInputStream in = SafeObjectInputStream.install(inObjStream);

            // try to restore the options
            log.debug("ObjectSaveTest:testHashMap(): restoring .....");
            restored = false;
            restored_obj = in.readHashMap();
            inObjStream.close();
            inStream.close();

            restored = true;
            log.debug("ObjectSaveTest:testHashMap(): ....restored operation completed.....");

        } catch (Exception ex2) {
            log.debug("ObjectSaveTest:testHashMap(): error during restore [" + ex2.getClass().getName() + " : "
                    + ex2.getMessage() + "]");
            ex2.printStackTrace();
        }

        // if the save/restore of the object succeeded,
        // then don't keep the temporary file around
        boolean removeTmpFile = saved && restored;
        if (removeTmpFile) {
            try {
                theFile.delete();
            } catch (Exception e) {
                // just absorb it
            }
        }

        assertTrue(restored);

        if (restored_obj != null) {
            int restored_size = restored_obj.size();
            if (restored_size == (initial_size - 3)) {
                // there are entries in the map that are not serializable
                comparesOK = true;
            }
        }

        // TODO: check for exact entries

        assertTrue(comparesOK);

        // indicate that the temp file was created ok
        done = true;
    }

    // this is false when there are problems with the temporary file
    assertTrue(done);

    log.debug("ObjectSaveTest:testHashMap():  END ---------------");
}

From source file:org.apache.axis2.engine.ObjectSave2Test.java

public void testLinkedList() throws Exception {
    File theFile = null;/*from   w w w . j a  v a2  s. co m*/
    String theFilename = null;
    boolean saved = false;
    boolean restored = false;
    boolean done = false;
    boolean comparesOK = false;

    log.debug("ObjectSaveTest:testLinkedList():  BEGIN ---------------");

    // ---------------------------------------------------------
    // setup the object to use
    // ---------------------------------------------------------
    LinkedList obj = new LinkedList();
    obj.add(new Integer(1));
    obj.add(new Integer(2));
    obj.add(new Integer(3));
    obj.add(new String("string1"));
    obj.add(new String("string2"));
    obj.add(System.in);
    obj.add(new Integer(4));
    obj.add(new Integer(5));
    obj.add(new Integer(6));

    int initial_size = obj.size();

    // ---------------------------------------------------------
    // setup a temporary file to use
    // ---------------------------------------------------------
    try {
        theFile = File.createTempFile("linkedlistTest", null);
        theFilename = theFile.getName();
        log.debug("ObjectSaveTest:testLinkedList(): temp file = [" + theFilename + "]");
    } catch (Exception ex) {
        log.debug("ObjectSaveTest:testLinkedList(): error creating temp file = [" + ex.getMessage() + "]");
        theFile = null;
    }

    if (theFile != null) {
        // ---------------------------------------------------------
        // save to the temporary file
        // ---------------------------------------------------------
        try {
            // setup an output stream to a physical file
            FileOutputStream outStream = new FileOutputStream(theFile);

            // attach a stream capable of writing objects to the
            // stream connected to the file
            ObjectOutputStream outObjStream = new ObjectOutputStream(outStream);
            SafeObjectOutputStream out = SafeObjectOutputStream.install(outObjStream);

            // try to save
            log.debug("ObjectSaveTest:testLinkedList(): saving .....");
            saved = false;
            out.writeList(obj);

            // close out the streams
            outObjStream.flush();
            outObjStream.close();
            outStream.flush();
            outStream.close();

            saved = true;
            log.debug("ObjectSaveTest:testLinkedList(): ....save operation completed.....");

            long filesize = theFile.length();
            log.debug("ObjectSaveTest:testLinkedList(): file size after save [" + filesize + "]   temp file = ["
                    + theFilename + "]");
        } catch (Exception ex2) {
            log.debug("ObjectSaveTest:testLinkedList(): error during save [" + ex2.getClass().getName() + " : "
                    + ex2.getMessage() + "]");
            ex2.printStackTrace();
        }

        assertTrue(saved);

        // ---------------------------------------------------------
        // restore from the temporary file
        // ---------------------------------------------------------
        LinkedList restored_obj = null;

        try {
            // setup an input stream to the file
            FileInputStream inStream = new FileInputStream(theFile);

            // attach a stream capable of reading objects from the
            // stream connected to the file
            ObjectInputStream inObjStream = new ObjectInputStream(inStream);
            SafeObjectInputStream in = SafeObjectInputStream.install(inObjStream);

            // try to restore the options
            log.debug("ObjectSaveTest:testLinkedList(): restoring .....");
            restored = false;
            restored_obj = in.readLinkedList();
            inObjStream.close();
            inStream.close();

            restored = true;
            log.debug("ObjectSaveTest:testLinkedList(): ....restored operation completed.....");

        } catch (Exception ex2) {
            log.debug("ObjectSaveTest:testLinkedList(): error during restore [" + ex2.getClass().getName()
                    + " : " + ex2.getMessage() + "]");
            ex2.printStackTrace();
        }

        // if the save/restore of the object succeeded,
        // then don't keep the temporary file around
        boolean removeTmpFile = saved && restored;
        if (removeTmpFile) {
            try {
                theFile.delete();
            } catch (Exception e) {
                // just absorb it
            }
        }

        assertTrue(restored);

        if (restored_obj != null) {
            int restored_size = restored_obj.size();
            if (restored_size == (initial_size - 1)) {
                comparesOK = true;
            }
        }

        // TODO: check for exact entries

        assertTrue(comparesOK);

        // indicate that the temp file was created ok
        done = true;
    }

    // this is false when there are problems with the temporary file
    assertTrue(done);

    log.debug("ObjectSaveTest:testLinkedList():  END ---------------");
}