Example usage for java.io ObjectOutput writeObject

List of usage examples for java.io ObjectOutput writeObject

Introduction

In this page you can find the example usage for java.io ObjectOutput writeObject.

Prototype

public void writeObject(Object obj) throws IOException;

Source Link

Document

Write an object to the underlying storage or stream.

Usage

From source file:org.jfree.data.xy.junit.DefaultXYZDatasetTest.java

/**
 * Serialize an instance, restore it, and check for equality.
 *///from w  w  w .  ja va  2s.c  om
public void testSerialization() {

    DefaultXYZDataset d1 = new DefaultXYZDataset();
    DefaultXYZDataset d2 = null;

    try {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(d1);
        out.close();

        ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
        d2 = (DefaultXYZDataset) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertEquals(d1, d2);

    // try a dataset with some content...
    double[] x1 = new double[] { 1.0, 2.0, 3.0 };
    double[] y1 = new double[] { 4.0, 5.0, 6.0 };
    double[] z1 = new double[] { 7.0, 8.0, 9.0 };
    double[][] data1 = new double[][] { x1, y1, z1 };
    d1.addSeries("S1", data1);
    try {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(d1);
        out.close();

        ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
        d2 = (DefaultXYZDataset) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertEquals(d1, d2);

}

From source file:org.lexgrid.valuesets.helper.compiler.FileSystemCachingValueSetDefinitionCompilerDecorator.java

/**
 * Persist coded node set.// w  w w . j a v  a 2 s . co m
 * 
 * @param md5 the md5
 * @param cns the cns
 * 
 * @throws Exception the exception
 */
protected void persistCodedNodeSet(String md5, CodedNodeSet cns) {
    try {
        File cachedCnsFile = new File(this.getDiskStorePath() + File.separator + this.getFileName(md5));

        if (cachedCnsFile.exists()) {
            LoggerFactory.getLogger().info("Compiled Value Set Definition already cached.");

            return;
        }

        this.deleteOldestFile();

        ObjectOutput out = new ObjectOutputStream(
                new FileOutputStream(this.getDiskStorePath() + File.separator + this.getFileName(md5)));
        out.writeObject(cns);
        out.close();
    } catch (Exception e) {
        LoggerFactory.getLogger().warn("There was an error persisting the Compiled Value Set Definition."
                + " Caching will not be used for this Value Set Definition.", e);
    }
}

From source file:com.sun.j2ee.blueprints.taglibs.smart.ClientStateTag.java

public int doEndTag() throws JspTagException {
    if (imageURL == null && buttonText == null) {
        throw new JspTagException(
                "ClientStateTag error: either an " + "imageURL or buttonText attribute must be specified.");
    }// w  ww  .j  av  a2s  .  c om
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    StringBuffer buffer = new StringBuffer();
    buffer.append("<form method=\"POST\" action=\"" + targetURL + "\">");
    // insert any parameters that may have been added via sub tags
    if (parameters != null) {
        Iterator it = parameters.keySet().iterator();
        // put the request attributes stored in the session in the request
        while (it.hasNext()) {
            String key = (String) it.next();
            String value = (String) parameters.get(key);
            buffer.append(" <input type=\"hidden\" name=\"" + key + "\" value=\"" + value + "\" />");
        }
    }
    String fullURL = request.getRequestURI();
    // find the url that sent us this page
    String targetURL = null;
    int lastPathSeparator = fullURL.lastIndexOf("/") + 1;
    if (lastPathSeparator != -1) {
        targetURL = fullURL.substring(lastPathSeparator, fullURL.length());
    }
    buffer.append(" <input type=\"hidden\" name=\"referring_URL\"" + "value=\"" + targetURL + "\">");
    String referringScreen = (String) request.getSession().getAttribute(WebKeys.PREVIOUS_SCREEN);
    buffer.append(" <input type=\"hidden\" name=\"referring_screen\"" + "value=\"" + referringScreen + "\">");
    buffer.append(" <input type=\"hidden\" name=\"cacheId\"" + "value=\"" + cacheId + "\">");
    // check the request for previous parameters
    Map params = (Map) request.getParameterMap();
    if (!params.isEmpty() && encodeRequestParameters) {
        Iterator it = params.entrySet().iterator();
        // copy in the request parameters stored
        while (it.hasNext()) {
            Entry entry = (Entry) it.next();
            String key = (String) entry.getKey();
            if (!key.startsWith(cacheId)) {
                String[] values = (String[]) entry.getValue();
                String valueString = values[0];
                buffer.append(" <input type=\"hidden\" name=\"" + key + "\" value=\"" + valueString + "\" />");
            }
        }
    }
    /**
      *  Now serialize the request attributes into the page (only sealizable objects are going
      *  to be processed).
      */
    if (encodeRequestAttributes) {
        // put the request attributes into tattributres
        Enumeration enumeration = request.getAttributeNames();
        while (enumeration.hasMoreElements()) {
            String key = (String) enumeration.nextElement();
            // check if we have already serialized the items
            // also don't serialize javax items because
            if (!key.startsWith(cacheId) && !key.startsWith("javax.servlet")) {
                Object value = request.getAttribute(key);
                if (serializableClass == null) {
                    try {
                        serializableClass = Class.forName("java.io.Serializable");
                    } catch (java.lang.ClassNotFoundException cnf) {
                        logger.error("ClientStateTag caught: ", cnf);
                    }
                }
                // check if seralizable
                if (serializableClass.isAssignableFrom(value.getClass())) {
                    try {
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        ObjectOutput out = new ObjectOutputStream(bos);
                        out.writeObject(value);
                        out.close();
                        buffer.append(" <input type=\"hidden\" name=\"" + cacheId + "_attribute_" + key
                                + "\" value=\"" + new String(Base64.encode(bos.toByteArray()), "ISO-8859-1")
                                + "\" />");
                    } catch (java.io.IOException iox) {
                        logger.error("ClientStateTag caught: ", iox);
                    }
                } else {
                    logger.info(key + " not to Serializeable");
                }
            }
        }
    } // end get attributes
      // now put the link in
    if (imageURL != null) {
        buffer.append(" <input alt=\"" + altText + "\" type=\"image\" " + "src=\"" + imageURL + "\"/>");
    } else {
        buffer.append(" <input alt=\"" + altText + "\"  type=\"submit\" " + "value=\"" + buttonText + "\"/>");
    }
    buffer.append("</form>");
    // write the output to the output stream
    try {
        JspWriter out = pageContext.getOut();
        out.print(buffer.toString());
    } catch (IOException ioe) {
        logger.error("ClientStateTag: Problems with writing...", ioe);
    }
    // reset everything
    parameters = null;
    altText = "";
    buttonText = null;
    imageURL = null;
    cacheId = null;
    targetURL = null;
    encodeRequestAttributes = true;
    encodeRequestParameters = true;
    serializableClass = null;
    return EVAL_PAGE;
}

From source file:com.github.naoghuman.cm.model.glossary.GlossaryModel.java

@Override
public void writeExternal(ObjectOutput out) throws IOException {
    out.writeLong(this.getId());
    out.writeLong(this.getGenerationTime());
    out.writeObject(StringEscapeUtils.escapeHtml4(this.getTitle()));
    out.writeObject(StringEscapeUtils.escapeHtml4(this.getDescription()));
    out.writeObject(StringEscapeUtils.escapeHtml4(this.getNotes()));
}

From source file:org.jfree.data.xy.junit.TableXYDatasetTest.java

/**
 * Serialize an instance, restore it, and check for equality.
 *///from w  w w  . j a v  a2  s.  c  o m
public void testSerialization() {

    DefaultTableXYDataset d1 = new DefaultTableXYDataset();
    d1.addSeries(createSeries2());
    DefaultTableXYDataset d2 = null;

    try {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(d1);
        out.close();

        ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
        d2 = (DefaultTableXYDataset) in.readObject();
        in.close();
    } catch (Exception e) {
        System.out.println(e.toString());
    }
    assertEquals(d1, d2);

}

From source file:com.roche.iceboar.cachestorage.LocalCacheStorage.java

private void store(CacheStatus cacheStatus, String cachePath) {
    OutputStream file = null;//from  w ww  .  j a v  a  2  s. c o  m
    OutputStream buffer = null;
    ObjectOutput output = null;
    try {
        //use buffering
        file = new FileOutputStream(cachePath);
        buffer = new BufferedOutputStream(file);
        output = new ObjectOutputStream(buffer);
        output.writeObject(cacheStatus);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (buffer != null) {
            try {
                buffer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (file != null) {
            try {
                file.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:ar.edu.unicen.exa.aop.aopetstore.waf.view.taglibs.smart.ClientStateTag.java

public int doEndTag() throws JspTagException {
    HttpServletRequest request = ((HttpServletRequest) pageContext.getRequest());
    StringBuffer buffer = new StringBuffer();
    buffer.append("<form method=\"POST\" action=\"" + targetURL + "\">");
    // insert any parameters that may have been added via sub tags
    if (parameters != null) {
        Iterator<String> it = parameters.keySet().iterator();
        // put the request attributes stored in the session in the request
        while (it.hasNext()) {
            String key = it.next();
            String value = parameters.get(key);
            buffer.append(" <input type=\"hidden\" name=\"" + key + "\" value=\"" + value + "\" />");
        }/*from  www .  j a  v  a2 s  .  c  o m*/
    }
    String fullURL = request.getRequestURI();
    // find the url that sent us this page
    String targetURL = null;
    int lastPathSeparator = fullURL.lastIndexOf("/") + 1;
    if (lastPathSeparator != -1) {
        targetURL = fullURL.substring(lastPathSeparator, fullURL.length());
    }
    buffer.append(" <input type=\"hidden\" name=\"referring_URL\"" + "value=\"" + targetURL + "\">");
    String referringScreen = (String) request.getSession().getAttribute(WebKeys.PREVIOUS_SCREEN);
    buffer.append(" <input type=\"hidden\" name=\"referring_screen\"" + "value=\"" + referringScreen + "\">");
    buffer.append(" <input type=\"hidden\" name=\"cacheId\"" + "value=\"" + cacheId + "\">");
    // check the request for previous parameters
    Map<?, ?> params = (Map<?, ?>) request.getParameterMap();
    if (!params.isEmpty() && encodeRequestParameters) {
        Iterator<?> it = params.keySet().iterator();
        // copy in the request parameters stored
        while (it.hasNext()) {
            String key = (String) it.next();
            if (!key.startsWith(cacheId)) {
                String[] values = (String[]) params.get(key);
                String valueString = values[0];
                buffer.append(" <input type=\"hidden\" name=\"" + key + "\" value=\"" + valueString + "\" />");
            }
        }
    }
    /**
      *  Now serialize the request attributes into the page (only sealizable objects are going
      *  to be processed).
      */
    if (encodeRequestAttributes) {
        // put the request attributes into tattributres
        Enumeration<?> myEnumeration = request.getAttributeNames();
        while (myEnumeration.hasMoreElements()) {
            String key = (String) myEnumeration.nextElement();
            // check if we have already serialized the items
            // also don't serialize javax items because
            if (!key.startsWith(cacheId) && !key.startsWith("javax.servlet")) {
                Object value = request.getAttribute(key);
                if (serializableClass == null) {
                    try {
                        getClass();
                        serializableClass = Class.forName("java.io.Serializable");
                    } catch (java.lang.ClassNotFoundException cnf) {
                        System.err.println("ClientStateTag caught: " + cnf);
                    }
                }
                // check if seralizable
                if (serializableClass.isAssignableFrom(value.getClass())) {
                    try {
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        ObjectOutput out = new ObjectOutputStream(bos);
                        out.writeObject(value);
                        out.close();
                        buffer.append(" <input type=\"hidden\" name=\"" + cacheId + "_attribute_" + key
                                + "\" value=\""
                                + new String(Base64.encodeBase64(bos.toByteArray()), "ISO-8859-1") + "\" />");
                    } catch (java.io.IOException iox) {
                        System.err.println("ClientStateTag caught: " + iox);
                    }
                } else {
                    System.out.println(key + " not to Serializeable");
                }
            }

        }
    } // end get attributes
      // now put the link in
    if (imageURL != null) {
        buffer.append(" <input alt=\"" + altText + "\" type=\"image\" " + "src=\"" + imageURL + "\"/>");
    } else {
        buffer.append(" <input alt=\"" + altText + "\"  type=\"submit\" " + "value=\"" + buttonText + "\"/>");
    }
    buffer.append("</form>");
    // write the output to the output stream
    try {
        JspWriter out = pageContext.getOut();
        out.print(buffer.toString());
    } catch (IOException ioe) {
        System.err.println("ClientStateTag: Problems with writing...");
    }
    // reset everything
    parameters = null;
    altText = "";
    buttonText = null;
    imageURL = null;
    cacheId = null;
    targetURL = null;
    encodeRequestAttributes = true;
    encodeRequestParameters = true;
    serializableClass = null;
    return EVAL_PAGE;
}

From source file:BinaryHeapQueue.java

public void writeExternal(ObjectOutput out) throws IOException {
    out.writeObject(comparator);
    out.writeObject(elements);//from ww w  . jav  a 2  s  . co m
    out.writeInt(size);
}

From source file:net.openhft.chronicle.wire.benchmarks.Data.java

@Override
public void writeExternal(ObjectOutput out) throws IOException {
    out.writeDouble(price);//from  w ww  .j  a  v  a 2s  .co  m
    out.writeLong(longInt);
    out.writeInt(smallInt);
    out.writeBoolean(flag);
    out.writeObject(side);
    out.writeObject(getText());
}

From source file:com.github.naoghuman.abclist.model.Term.java

@Override
public void writeExternal(ObjectOutput out) throws IOException {
    out.writeLong(this.getId());
    out.writeLong(this.getGenerationTime());
    out.writeObject(this.getDescription());
    out.writeObject(this.getTitle());
}