Example usage for java.io ObjectInputStream close

List of usage examples for java.io ObjectInputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the input stream.

Usage

From source file:SaveYourDrawingToFile.java

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == clearBtn) {
        displayList = new Vector();
        repaint();/*  w w w.  jav a2  s.  co m*/
    } else if (e.getSource() == saveBtn) {
        try {
            FileOutputStream fos = new FileOutputStream(pathname);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(displayList);
            oos.flush();
            oos.close();
            fos.close();
        } catch (Exception ex) {
            System.out.println("Trouble writing display list vector");
        }
    } else if (e.getSource() == restoreBtn) {
        try {
            FileInputStream fis = new FileInputStream(pathname);
            ObjectInputStream ois = new ObjectInputStream(fis);
            displayList = (Vector) (ois.readObject());
            ois.close();
            fis.close();
            repaint();
        } catch (Exception ex) {
            System.out.println("Trouble reading display list vector");
        }
    } else if (e.getSource() == quitBtn) {
        setVisible(false);
        dispose();
        System.exit(0);
    }
}

From source file:net.sf.ehcache.distribution.EventMessageTest.java

/**
 * test serialization and deserialization of EventMessage.
 *///from  w w  w  .  ja  v  a 2s  .c  om
public void testSerialization() throws IOException, ClassNotFoundException {

    EventMessage eventMessage = new EventMessage(EventMessage.PUT, "key", new Element("key", "element"));

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bout);
    oos.writeObject(eventMessage);
    byte[] serializedValue = bout.toByteArray();
    oos.close();
    EventMessage eventMessage2 = null;
    ByteArrayInputStream bin = new ByteArrayInputStream(serializedValue);
    ObjectInputStream ois = new ObjectInputStream(bin);
    eventMessage2 = (EventMessage) ois.readObject();
    ois.close();

    //Check after Serialization
    assertEquals("key", eventMessage2.getSerializableKey());
    assertEquals("element", eventMessage2.getElement().getObjectValue());
    assertEquals(EventMessage.PUT, eventMessage2.getEvent());
    assertTrue(eventMessage2.isValid());

}

From source file:de.tudarmstadt.ukp.dkpro.tc.weka.util.WekaUtils.java

/**
 * Reads a file serialized with {@link WekaUtils#writeMlResultToFile(MultilabelResult, File)}.
 *
 * @param file the file to read from//w  w w.j  av  a  2  s.co m
 * @return an object holding the results
 * @throws IOException i/o error
 * @throws ClassNotFoundException file not found
 */
public static MultilabelResult readMlResultFromFile(File file) throws IOException, ClassNotFoundException {
    ObjectInputStream stream = new ObjectInputStream(new FileInputStream(file));
    MultilabelResult result = (MultilabelResult) stream.readObject();
    stream.close();
    return result;
}

From source file:com.earldouglas.xjdl.io.LicenseLoader.java

protected License decryptLicense(String encodedLicense)
        throws BadPaddingException, UnsupportedEncodingException, Exception {
    byte[] encryptedLicense = Base64.decodeBase64(encodedLicense);

    SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
    byte[] serializedLicense = cipher.doFinal(encryptedLicense);

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(serializedLicense);
    ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
    License license = (License) objectInputStream.readObject();
    objectInputStream.close();
    return license;
}

From source file:com.topekalabs.bigmachine.lib.app.ContinuationSerializationTest.java

@Test
public void simpleSerializationTest() throws Exception {
    MutableInt context = new MutableInt();
    Continuation c = Continuation.startWith(new SimpleContinuationRunnable(), context);
    logger.debug("Is serializable: {}", c.isSerializable());

    Assert.assertEquals("The value of the context was not set properly", SimpleContinuationRunnable.FIRST_VALUE,
            context.getValue());/*from  w  w  w  .  j ava 2  s  .c  o m*/

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(c);
    oos.close();

    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
    c = (Continuation) ois.readObject();
    ois.close();

    Continuation.continueWith(c, context);

    Assert.assertEquals("The value of the context was not set properly",
            SimpleContinuationRunnable.SECOND_VALUE, context.getValue());
}

From source file:PersisTest.java

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == clearBtn) {
        // Repaint with an empty display list.
        displayList = new ArrayList();
        repaint();/*  w w  w.  j  av a  2  s . c  o m*/
    } else if (e.getSource() == saveBtn) {
        // Write display list array list to an object output stream.
        try {
            FileOutputStream fos = new FileOutputStream(pathname);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(displayList);
            oos.flush();
            oos.close();
            fos.close();
        } catch (IOException ex) {
            System.err.println("Trouble writing display list array list");
        }
    } else if (e.getSource() == restoreBtn) {
        // Read a new display list array list from an object input stream.
        try {
            FileInputStream fis = new FileInputStream(pathname);
            ObjectInputStream ois = new ObjectInputStream(fis);
            displayList = (ArrayList) (ois.readObject());
            ois.close();
            fis.close();
            repaint();
        } catch (ClassNotFoundException ex) {
            System.err.println("Trouble reading display list array list");
        } catch (IOException ex) {
            System.err.println("Trouble reading display list array list");
        }
    } else if (e.getSource() == quitBtn) {
        System.exit(0);
    }
}

From source file:it.acubelab.smaph.SmaphAnnotator.java

/**
 * Set the file to which the Bing responses cache is bound.
 * //from   ww  w  . jav a2s .c  o m
 * @param cacheFilename
 *            the cache file name.
 * @throws FileNotFoundException
 *             if the file could not be open for reading.
 * @throws IOException
 *             if something went wrong while reading the file.
 * @throws ClassNotFoundException
 *             is the file contained an object of the wrong class.
 */
public static void setCache(String cacheFilename)
        throws FileNotFoundException, IOException, ClassNotFoundException {
    if (resultsCacheFilename != null && resultsCacheFilename.equals(cacheFilename))
        return;
    System.out.println("Loading bing cache...");
    resultsCacheFilename = cacheFilename;
    if (new File(resultsCacheFilename).exists()) {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(resultsCacheFilename));
        url2jsonCache = (HashMap<String, byte[]>) ois.readObject();
        ois.close();
    }
}

From source file:ch.epfl.data.squall.utilities.SquallSerializationDelegate.java

@Override
public Object deserialize(byte[] bytes) {
    try {/*  ww w. j a  v a2s . c  om*/
        return super.deserialize(bytes);
    } catch (RuntimeException e) {
        try {
            if (classdir == null)
                throw e;

            URLClassLoader classloader = new URLClassLoader(new URL[] { classdir },
                    Thread.currentThread().getContextClassLoader());

            // Read the object
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ClassLoaderObjectInputStream(classloader, bis);
            Object ret = ois.readObject();
            ois.close();
            return ret;
        } catch (ClassNotFoundException error) {
            throw new RuntimeException(error);
        } catch (IOException error) {
            throw new RuntimeException(error);
        }
    }
}

From source file:com.servioticy.queueclient.SimpleQueueClient.java

LinkedList<Object> readQueue() throws ClassNotFoundException, IOException {
    LinkedList<Object> queue;
    try {/*from  w  ww . ja v a  2  s .  co m*/
        FileInputStream fileIn;
        fileIn = new FileInputStream(filePath);
        ObjectInputStream in = new ObjectInputStream(fileIn);
        queue = (LinkedList<Object>) in.readObject();
        in.close();
        fileIn.close();
    } catch (FileNotFoundException e) {
        queue = new LinkedList<Object>();
    }
    return queue;
}

From source file:com.fuzhepan.arpc.client.ProxyHandler.java

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    log.debug("invoke was called!");

    if (method.getName().equals("toString")) {
        return "toString method was called";
    }//from   w w w.j  av a  2 s. c  o  m

    RpcContext rpcContext = new RpcContext(interfaceName, method.getName(), method.getParameterTypes(), args);

    //get service info and load balance
    List<HostPortPair> serviceList = RpcConfig.getServiceList(serviceName);
    if (serviceList == null || serviceList.size() == 0)
        throw new ClassNotFoundException("not find service : " + serviceName);
    int index = requestCount.get() % serviceList.size();
    if (requestCount.get() > 100)
        requestCount.set(0);
    else
        requestCount.getAndIncrement();
    HostPortPair hostPort = serviceList.get(index);

    Socket socket = new Socket(hostPort.host, hostPort.port);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
    objectOutputStream.writeObject(rpcContext);
    objectOutputStream.flush();

    ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
    Object response = objectInputStream.readObject();

    objectInputStream.close();
    objectOutputStream.close();
    socket.close();

    Class methodReturnType = method.getReturnType();
    return methodReturnType.cast(response);
}