com.codename1.io.Externalizable Java Examples

The following examples show how to use com.codename1.io.Externalizable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProxyServerHelper.java    From codenameone-demos with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns an OK response to the client
 * @param resp the response object
 * @param def the method definition
 * @param response the result from the method
 */
public static void writeResponse(HttpServletResponse resp, WSDefinition def, Externalizable response) throws IOException {
    resp.setStatus(HttpServletResponse.SC_OK);
    DataOutputStream dos = new DataOutputStream(resp.getOutputStream());
    Util.writeObject(response, dos);
    dos.close();
}
 
Example #2
Source File: PropertyIndex.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns an externalizable object for serialization of this business object, unlike regular
 * externalizables this implementation is robust to changes, additions and removals of 
 * properties
 * @return an externalizable instance
 */
public Externalizable asExternalizable() {
    return new Externalizable() {
        public int getVersion() {
            return 1;
        }

        public void externalize(DataOutputStream out) throws IOException {
            out.writeInt(getSize());
            for(PropertyBase b : PropertyIndex.this) {
                out.writeUTF(b.getName());
                if(b instanceof CollectionProperty) {
                    out.writeByte(2);
                    Util.writeObject(((CollectionProperty)b).asList(), out);
                    continue;
                }
                if(b instanceof MapProperty) {
                    out.writeByte(3);
                    Util.writeObject(((MapProperty)b).asMap(), out);
                    continue;
                }
                if(b instanceof Property) {
                    out.writeByte(1);
                    Util.writeObject(((Property)b).get(), out);
                    continue;
                }
            }
        }

        public void internalize(int version, DataInputStream in) throws IOException {
            int size = in.readInt();
            for(int iter = 0 ; iter < size ; iter++) {
                String pname = in.readUTF();
                int type = in.readByte();
                Object data = Util.readObject(in);
                PropertyBase pb = get(pname);
                switch(type) {
                    case 1: // Property
                        if(pb instanceof Property) {
                            ((Property)pb).set(data);
                        }
                        break;
                        
                    case 2: // CollectionProperty
                        if(pb instanceof CollectionProperty) {
                            ((CollectionProperty)pb).set((List)data);
                        }
                        break;
                        
                    case 3: // MapProperty
                        if(pb instanceof MapProperty) {
                            ((MapProperty)pb).setMap((Map)data);
                        }
                        break;
                }
            }
        }

        public String getObjectId() {
            return getName();
        }
    };
}
 
Example #3
Source File: Util.java    From CodenameOne with GNU General Public License v2.0 2 votes vote down vote up
/**
 * <p>Registers this externalizable so readObject will be able to load such objects.</p>
 * <p>
 * The sample below demonstrates the usage and registration of the {@link com.codename1.io.Externalizable} interface:
 * </p>
 * <script src="https://gist.github.com/codenameone/858d8634e3cf1a82a1eb.js"></script>
 *
 *
 * @param e the externalizable instance
 */
public static void register(Externalizable e) {
    externalizables.put(e.getObjectId(), e.getClass());
}