Example usage for java.io ObjectInputStream readObject

List of usage examples for java.io ObjectInputStream readObject

Introduction

In this page you can find the example usage for java.io ObjectInputStream readObject.

Prototype

public final Object readObject() throws IOException, ClassNotFoundException 

Source Link

Document

Read an object from the ObjectInputStream.

Usage

From source file:je3.rmi.MudClient.java

/**
 * This main() method defines the standalone program that starts up a MUD
 * server.  If invoked with a single argument, it treats that argument as
 * the name of a file containing the serialized and compressed state of an
 * existing MUD, and recreates it.  Otherwise, it expects four command-line
 * arguments: the name of the MUD, the password, the name of the entrance
 * place for the MUD, and a description of that entrance place.
 * Besides creating the MudServer object, this program sets an appropriate
 * security manager, and uses the default rmiregistry to register the
 * the MudServer under its given name.//from www  . ja  va 2s  .  c  o  m
 **/
public static void main(String[] args) {
    try {
        MudServer server;
        if (args.length == 1) {
            // Read the MUD state in from a file
            FileInputStream f = new FileInputStream(args[0]);
            ObjectInputStream in =
      new ObjectInputStream(new GZIPInputStream(f));
            server = (MudServer) in.readObject();
        }
        // Otherwise, create an initial MUD from scratch
        else server = new MudServer(args[0], args[1], args[2], args[3]);
       
        Naming.rebind(Mud.mudPrefix + server.mudname, server);
    }
    // Display an error message if anything goes wrong.
    catch (Exception e) {
        System.out.println(e);
        System.out.println("Usage: java MudServer <savefile>\n" +
            "   or: java MudServer <mudname> <password> " + 
            "<placename> <description>");
        System.exit(1);
    }
}

From source file:cacheservice.CacheServer.java

/**
 * @param args the command line arguments
 *//*from www  . ja va  2  s .c  o  m*/
public static void main(String[] args) {
    // COMUNICACIN CON EL CLIENTE
    ServerSocket serverSocket;
    Socket socketCliente;
    DataInputStream in; //Flujo de datos de entrada
    DataOutputStream out; //Flujo de datos de salida
    String mensaje;
    int laTengoenCache = 0;

    //COMUNICACIN CON EL INDEX
    ServerSocket serverSocketIndex;
    Socket socketIndex;
    DataOutputStream outIndex;
    ObjectInputStream inIndex;
    String mensajeIndex;

    try {
        serverSocket = new ServerSocket(4444);
        System.out.print("SERVIDOR CACHE ACTIVO a la espera de peticiones");

        //MIENTRAS PERMANEZCA ACTIVO EL SERVIDOR CACHE ESPERAR? POR PETICIONES DE LOS CLIENTES
        while (true) {
            socketCliente = serverSocket.accept();
            in = new DataInputStream(socketCliente.getInputStream()); //Entrada de los mensajes del cliente
            mensaje = in.readUTF(); //Leo el mensaje enviado por el cliente

            System.out.println("\nHe recibido del cliente: " + mensaje); //Muestro el mensaje recibido por el cliente
            //int particionBuscada = seleccionarParticion(mensaje, tamanoCache, numeroParticiones); //Busco la particin
            //double tamanoParticion = Math.ceil( (double)tamanoCache / (double)numeroParticiones);

            //Thread hilo = new Hilo(mensaje,particionBuscada,cache.GetTable(),(int) tamanoParticion);
            //hilo.start();

            //RESPUESTA DEL SERVIDOR CACHE AL CLIENTE
            out = new DataOutputStream(socketCliente.getOutputStream());
            String respuesta = "Respuesta para " + mensaje;
            if (laTengoenCache == 1) {
                out.writeUTF(respuesta);
                System.out.println("\nTengo la respuesta. He respondido al cliente: " + respuesta);
            } else {
                out.writeUTF("miss");
                out.close();
                in.close();
                socketCliente.close();

                System.out.println("\nNo tengo la respuesta.");

                //LEER RESPUESTA DEL SERVIDOR INDEX
                serverSocketIndex = new ServerSocket(6666);
                socketIndex = serverSocketIndex.accept();
                inIndex = new ObjectInputStream(socketIndex.getInputStream());
                JSONObject mensajeRecibidoIndex = (JSONObject) inIndex.readObject();

                System.out.println("He recibido del SERVIDOR INDEX: " + mensajeRecibidoIndex);

                //outIndex.close();
                inIndex.close();
                socketIndex.close();

            }

        }
    } catch (Exception e) {
        System.out.print(e.getMessage());
    }
}

From source file:Main.java

private static Object readFromFile(String filename) throws Exception {
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(filename)));
    Object object = ois.readObject();
    ois.close();/*from   w  ww . ja  v a 2 s.  c o  m*/
    return object;
}

From source file:Main.java

public static Object byteToObject(byte[] data) throws IOException, ClassNotFoundException {
    ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
    ObjectInputStream is = new ObjectInputStream(inputStream);
    return is.readObject();
}

From source file:Main.java

public static Object deserializar(String path) throws Exception {
    FileInputStream inFile = new FileInputStream(path);
    ObjectInputStream d = new ObjectInputStream(inFile);
    Object o = d.readObject();
    d.close();//ww  w  .j  a  v a2  s . com
    return o;
}

From source file:Main.java

public static Object fromBytes(byte[] arr) throws Exception {
    ByteArrayInputStream bais = new ByteArrayInputStream(arr);
    ObjectInputStream ois = new ObjectInputStream(bais);
    return ois.readObject();
}

From source file:Main.java

public static Object restore(String fileName) throws IOException, ClassNotFoundException {
    FileInputStream fis = new FileInputStream(fileName);
    ObjectInputStream in = new ObjectInputStream(fis);
    Object obj = in.readObject();
    in.close();//from   w  w  w . j a  v a2  s.c  om
    return obj;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T readSerializedObject(Context context, String fileName)
        throws FileNotFoundException, IOException, ClassNotFoundException {
    FileInputStream fis = context.openFileInput(fileName);
    ObjectInputStream in = new ObjectInputStream(fis);

    T out = (T) in.readObject();

    in.close();/*from ww w  .  j av  a2  s.com*/
    fis.close();
    return out;
}

From source file:Main.java

public static Object cloneObject(Object obj) {
    try {/* www  . ja  va  2 s .c  o  m*/
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(obj);
        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream in = new ObjectInputStream(byteIn);
        return in.readObject();
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static Object readAsObject(File source) throws IOException, ClassNotFoundException {
    FileInputStream in = new FileInputStream(source);
    try {/* www . j  a  v a 2s .c  om*/
        ObjectInputStream oi = new ObjectInputStream(in);
        return oi.readObject();
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}