Example usage for java.io ObjectOutputStream writeUTF

List of usage examples for java.io ObjectOutputStream writeUTF

Introduction

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

Prototype

public void writeUTF(String str) throws IOException 

Source Link

Document

Primitive data write of this String in modified UTF-8 format.

Usage

From source file:org.hibernate.internal.SessionFactoryImpl.java

/**
 * Custom serialization hook used during Session serialization.
 * //from  w  ww. j a v  a  2 s  .  c o m
 * @param oos
 *            The stream to which to write the factory
 * @throws IOException
 *             Indicates problems writing out the serial data stream
 */
void serialize(ObjectOutputStream oos) throws IOException {
    oos.writeUTF(uuid);
    oos.writeBoolean(name != null);
    if (name != null) {
        oos.writeUTF(name);
    }
}

From source file:com.ecyrd.jspwiki.ReferenceManager.java

/**
 *  Serializes hashmaps to disk.  The format is private, don't touch it.
 *//*from w ww. j a v a 2s.  co  m*/
private synchronized void serializeAttrsToDisk(WikiPage p) {
    ObjectOutputStream out = null;
    StopWatch sw = new StopWatch();
    sw.start();

    try {
        File f = new File(m_engine.getWorkDir(), SERIALIZATION_DIR);

        if (!f.exists())
            f.mkdirs();

        //
        //  Create a digest for the name
        //
        f = new File(f, getHashFileName(p.getName()));

        // FIXME: There is a concurrency issue here...
        Set entries = p.getAttributes().entrySet();

        if (entries.size() == 0) {
            //  Nothing to serialize, therefore we will just simply remove the
            //  serialization file so that the next time we boot, we don't
            //  deserialize old data.
            f.delete();
            return;
        }

        out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(f)));

        out.writeLong(serialVersionUID);
        out.writeLong(System.currentTimeMillis()); // Timestamp

        out.writeUTF(p.getName());
        out.writeLong(entries.size());

        for (Iterator i = entries.iterator(); i.hasNext();) {
            Map.Entry e = (Map.Entry) i.next();

            if (e.getValue() instanceof Serializable) {
                out.writeUTF((String) e.getKey());
                out.writeObject(e.getValue());
            }
        }

        out.close();

    } catch (IOException e) {
        log.error("Unable to serialize!");

        try {
            if (out != null)
                out.close();
        } catch (IOException ex) {
        }
    } catch (NoSuchAlgorithmException e) {
        log.fatal("No MD5 algorithm!?!");
    } finally {
        sw.stop();

        log.debug("serialization for " + p.getName() + " done - took " + sw);
    }
}

From source file:nl.opengeogroep.filesetsync.client.SyncJobStatePersistence.java

public static void persist() {
    File f = new File(SyncConfig.getInstance().getVarDir() + File.separator + "syncjobstate.dat");
    FileOutputStream fos = null;/* www. j a v  a 2  s  .c  o m*/
    try {
        fos = new FileOutputStream(f);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeUTF(Version.getProjectVersion());
        oos.writeObject(instance);
        oos.close();
    } catch (Exception e) {
        log.error("Error writing sync job state to " + f.getAbsolutePath(), e);
    } finally {
        IOUtils.closeQuietly(fos);
    }
}

From source file:org.apache.hadoop.hbase.io.hfile.bucket.BucketCache.java

private void persistToFile() throws IOException {
    assert !cacheEnabled;
    FileOutputStream fos = null;//from  w  w  w  . j  a  va 2s. c  om
    ObjectOutputStream oos = null;
    try {
        if (!ioEngine.isPersistent())
            throw new IOException("Attempt to persist non-persistent cache mappings!");
        fos = new FileOutputStream(persistencePath, false);
        oos = new ObjectOutputStream(fos);
        oos.writeLong(cacheCapacity);
        oos.writeUTF(ioEngine.getClass().getName());
        oos.writeUTF(backingMap.getClass().getName());
        oos.writeObject(deserialiserMap);
        oos.writeObject(backingMap);
    } finally {
        if (oos != null)
            oos.close();
        if (fos != null)
            fos.close();
    }
}

From source file:org.apache.hadoop.hive.ql.parse.spark.SparkPartitionPruningSinkOperator.java

private void flushToFile() throws IOException {
    // write an intermediate file to the specified path
    // the format of the path is: tmpPath/targetWorkId/sourceWorkId/randInt
    Path path = conf.getPath();// w  w w .j a  va2s .c  o  m
    FileSystem fs = path.getFileSystem(this.getConfiguration());
    fs.mkdirs(path);

    while (true) {
        path = new Path(path, String.valueOf(Utilities.randGen.nextInt()));
        if (!fs.exists(path)) {
            break;
        }
    }

    short numOfRepl = fs.getDefaultReplication(path);

    ObjectOutputStream out = null;
    FSDataOutputStream fsout = null;

    try {
        fsout = fs.create(path, numOfRepl);
        out = new ObjectOutputStream(new BufferedOutputStream(fsout, 4096));
        out.writeUTF(conf.getTargetColumnName());
        buffer.writeTo(out);
    } catch (Exception e) {
        try {
            fs.delete(path, false);
        } catch (Exception ex) {
            LOG.warn("Exception happened while trying to clean partial file.");
        }
        throw e;
    } finally {
        if (out != null) {
            LOG.info("Flushed to file: " + path);
            out.close();
        } else if (fsout != null) {
            fsout.close();
        }
    }
}

From source file:org.apache.hc.client5.http.impl.auth.BasicScheme.java

private void writeObject(final ObjectOutputStream out) throws IOException {
    out.defaultWriteObject();// ww w  . j  a v a2s  . c om
    out.writeUTF(this.charset.name());
}

From source file:org.apache.lens.driver.hive.TestRemoteHiveDriver.java

/**
 * Persist context.//  www  .  ja  v  a2  s  .c  o  m
 *
 * @param ctx the ctx
 * @return the byte[]
 * @throws IOException Signals that an I/O exception has occurred.
 */
private byte[] persistContext(QueryContext ctx) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(baos);
    try {
        out.writeObject(ctx);
        boolean isDriverAvailable = (ctx.getSelectedDriver() != null);
        out.writeBoolean(isDriverAvailable);
        if (isDriverAvailable) {
            out.writeUTF(ctx.getSelectedDriver().getFullyQualifiedName());
        }
    } finally {
        out.flush();
        out.close();
        baos.close();
    }

    return baos.toByteArray();
}

From source file:org.apache.openejb.log.commonslogging.OpenEJBCommonsLog.java

private void writeObject(final ObjectOutputStream out) throws IOException {
    out.writeUTF(category);
}

From source file:org.globus.examples.services.filebuy.seller.impl.FileResource.java

public synchronized void store() throws ResourceException {
    /* We will use these two variables to write the resource to disk */
    FileOutputStream fos = null;/*  w w w. ja  va  2  s.com*/
    File tmpFile = null;

    logger.info("Attempting to store resource " + this.getID());
    try {
        /* We start by creating a temporary file */
        tmpFile = File.createTempFile("math", ".tmp", getPersistenceHelper().getStorageDirectory());
        /* We open the file for writing */
        fos = new FileOutputStream(tmpFile);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        /* We write the RPs in the file */
        oos.writeFloat(this.price);
        oos.writeUTF(this.name);
        oos.writeUTF(this.location);
        oos.flush();
        logger.info("Successfully stored resource with Name=" + name);
    } catch (Exception e) {
        /* Delete the temporary file if something goes wrong */
        tmpFile.delete();
        throw new ResourceException("Failed to store resource", e);
    } finally {
        /* Clean up */
        if (fos != null) {
            try {
                fos.close();
            } catch (Exception ee) {
            }
        }
    }

    /*
     * We have successfully created a temporary file with our resource's
     * RPs. Now, if there is a previous copy of our resource on disk, we
     * first have to delete it. Next, we rename the temporary file to the
     * file representing our resource.
     */
    File file = getKeyAsFile(this.key);
    if (file.exists()) {
        file.delete();
    }
    if (!tmpFile.renameTo(file)) {
        tmpFile.delete();
        throw new ResourceException("Failed to store resource");
    }
}