Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
What is the difference between Serializable and Externalizable in Java?
I've been learning how to use Serializable. I know if I create a class 'A' with different variables who implements Serializable and I add Serializable to my class, it's also Serializable. But, who is actually implementing those two methods to serialize? Does Object take care of everything or different classes overloads them when necessary?
This morning my boss and I had a long and ultimately fruitless discussion about this, in the context of trying to diagnose performance problems with a web application. We didn't really come to any conclusions. I think we're right in thinking that Serializable non-static inner classes have issues, but we're not sure precisely what the issues are or what exactly to avoid (we reasoned that we co...
I have a problem regarding Java custom serialization. I have a graph of objects and want to configure where to stop when I serialize a root object from client to server. Let's make it a bit concrete, clear by giving a sample scenario. I have Classes of type Company Employee (abstract) Manager extends Employee Secretary extends Employee Analyst extends Employee Project Here are the relations:...
I'm using Berkeley DB Java edition, via the DPL interface. I want to ask if someone knows about any GUI library that could make it easy to browse the data saved in the database. I know that BDB documentation says that the metadata about the stored entities is not saved in any place and therefore only the person who have written the data can know what are the types of the classes that would b...
I haven't tried this yet, but it seems risky. The case I'm thinking of is instrumenting simple VO classes with JiBX. These VOs are going to be serialized over AMF and possibly other schemes. Can anyone confirm or deny my suspicions that doing behind-the-back stuff like bytecode enhancement might mess something up in general, and provide some background information as to why? Also, I'm inter...
In not so technical way, what is object serialization and the purpose of it? When should it be used? (any analogies exampls welcome) Thank You
I have a question about Java serialization in scenarios where you may need to modify your serializable class and maintain backward compatibility. I come from deep C# experience, so please allow me to compare Java with .NET. In my Java scenario, I need to serialize an object with Java's runtime serialization mechanism, and store the binary data in permanent storage to reuse the objects in futu...
I have a class that implements java.io.Serializable interface.So all the variables in that class is serilaizable. But i want to make some of the variables are should not serializable. Is it possible? thanks, Ravi
I am trying to transmit objects of a particular class from one server to another. The options I'm looking at are: Serialize the data as JSON and send it over the wire using HTTP, and de-serialize at the other end. Serialize the data into some binary form and transmit using TCP sockets. What are the best practices in this area? What are the gotchas? Ideally I would like the interface to ...
If I serialize an object in Java, and then later add an extra field to the java class, I can't deserialize the object into the modified class. Is there a serialization library or some way that I can have deserialization be less strict, like if there is an extra field added to the class then it just fills that with null upon deserialization of the old version of the class?
I have a graph structure in java, ("graph" as in "edges and nodes") and I'm attempting to serialise it. However, I get "StackOverflowException", despite significantly increasing the JVM stack size. I did some googling, and apparently this is a well known limitation of java serialisation: that it doesn't work for deeply nested object graphs such as long linked lists - it uses a stack record f...
I have the following class class UserAccount implements Serializable { public String username; public String password; public UserAccount() { username = "defaultUsername"; password = "defaultPassword"; } public UserAccount(String u, String p) { username = u; password = p; } private void readObject(ObjectInputStream o) ...
I was going through a blog and one question came to my head. Is it possible to overwrite the way ObjectOutputStream is writing. Let's say i am writing to a file out.dat i.e. ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File("C:\\out.dat"))); out.writeObject(o); When i opened the file out.dat in Notepad++, i saw the binary data. Which makes sense. What if, I woul...
I am currently working on a videogame, and i want to have the user be able to save their character to a new file. I know how to use the file io (for the most part), but i have been using the 'serialize' to serialize a whole object (that contains all the variables for the character) and save it to a file. The problem is that i am constantly updating the object and making changes to it, so when i...
I have a class that is something like the following: public class Foo { static byte[] convertToArray(Foo f); static Foo convertToFoo(byte[] ba); } How can I use convertToArray and convertToFoo to allow Foo to implement java.io.Serializable? The default serialization procedure doesn't seem like a good solution for my class because it would require changing a lot of other classes to implem...
I need a list of classes that implement Serializable. Could you also tell me what kind of classes implement that interface?
I receive an error: `IOException on socket listen: java.io.NotSerializableException:` java.net.Socket"` i try to send an object via socket with this code : ObjectOutputStream outObjects = new ObjectOutputStream(socket.getOutputStream()); outObjects.writeObject(currentPlayer); output.flush(); second line gives error.... but i have serialized ( implements Serializable) the class Playe...
I am working in a highly distributed environment. A lot of network access and a lot of db access. I have some classes that are send over and over the network , and are serialized and de-serialized. Most of the classes are quite simple in their nature , like : class A{ long a; long b; } And some are more complex ,( Compound - Collections ). There are some people in the company I work tha...
I am programming towards the Bloomberg Desktop Java API where I subscribe to and recieve market data in Message objects, containing different fields and corresponding values of different types, through the API. I want to 'record' a sequence of messages so that I can conduct testing of my code that processes these objects by replaying a known sequence of messages. Ideally, I would like to subscr...
Possible Duplicate: What is the difference between Serializable and Externalizable in Java? what is the difference between serialization and externalization in java?
 /*
  * Copyright 1996-2004 Sun Microsystems, Inc.  All Rights Reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Sun designates this
  * particular file as subject to the "Classpath" exception as provided
  * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
package java.io;
Only the identity of the class of an Externalizable instance is written in the serialization stream and it is the responsibility of the class to save and restore the contents of its instances. The writeExternal and readExternal methods of the Externalizable interface are implemented by a class to give the class complete control over the format and contents of the stream for an object and its supertypes. These methods must explicitly coordinate with the supertype to save its state. These methods supersede customized implementations of writeObject and readObject methods.
Object Serialization uses the Serializable and Externalizable interfaces. Object persistence mechanisms can use them as well. Each object to be stored is tested for the Externalizable interface. If the object supports Externalizable, the writeExternal method is called. If the object does not support Externalizable and does implement Serializable, the object is saved using ObjectOutputStream.
When an Externalizable object is reconstructed, an instance is created using the public no-arg constructor, then the readExternal method called. Serializable objects are restored by reading them from an ObjectInputStream.
An Externalizable instance can designate a substitution object via the writeReplace and readResolve methods documented in the Serializable interface.

public interface Externalizable extends java.io.Serializable {
    
The object implements the writeExternal method to save its contents by calling the methods of DataOutput for its primitive values or calling the writeObject method of ObjectOutput for objects, strings, and arrays.

Parameters:
out the stream to write the object to
Throws:
IOException Includes any I/O exceptions that may occur
SerialData:
Overriding methods should use this tag to describe the data layout of this Externalizable object. List the sequence of element types and, if possible, relate the element to a public/protected field and/or method of this Externalizable class.
    void writeExternal(ObjectOutput outthrows IOException;

    
The object implements the readExternal method to restore its contents by calling the methods of DataInput for primitive types and readObject for objects, strings and arrays. The readExternal method must read the values in the same sequence and with the same types as were written by writeExternal.

Parameters:
in the stream to read data from in order to restore the object
Throws:
IOException if I/O errors occur
java.lang.ClassNotFoundException If the class for an object being restored cannot be found.
New to GrepCode? Check out our FAQ X