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:dk.statsbiblioteket.util.XProperties.java

/**
 * Fetch stored properties from the given stream.
 *
 * @param instream Input stream to read properties from.
 * @throws InvalidPropertiesException if the stream contains unknown
 *                                    or invalid classes
 * @throws IOException                thrown if there are IO errors during read OR if
 *                                    resource is not found and ignoreNonExisting is false.
 *//*from  www .j  a  v  a2  s.co  m*/
public void load(InputStream instream) throws IOException {
    InputStreamReader inreader = new InputStreamReader(instream);
    ObjectInputStream objectIn;
    try {

        objectIn = xstream.createObjectInputStream(inreader);
        Object o = objectIn.readObject();
        XProperties properties = (XProperties) o;
        for (Map.Entry<Object, Object> entries : properties.getEntries()) {
            put(entries.getKey(), entries.getValue());
        }

        //            ArrayList<XPropertiesEntry> entries
        //                    = (ArrayList<XPropertiesEntry>) objectIn.readObject();
        //            for (XPropertiesEntry entry : entries) {
        //                put(entry.key, entry.value);
        //            }
        objectIn.close();
    } catch (ClassNotFoundException excl) {
        clear();
        String msg = String.format("ClassNotFoundException loading properties from" + " resource %s",
                resourceName);
        log.warn(msg);
        throw new InvalidPropertiesException(msg, excl);
    } catch (StreamException exst) {
        clear();
        String msg = String.format("StreamException loading properties from" + " resource %s", resourceName);
        log.warn(msg);
        throw new InvalidPropertiesException(msg, exst);
    } catch (ClassCastException e) {
        throw new InvalidPropertiesException(
                "Input stream does not look " + "like a valid XProperties " + "file", e);
    } finally {
        instream.close();
    }
}

From source file:net.sf.ehcache.Element.java

private Object deepCopy(Object oldValue) {
    Serializable newValue = null;
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    ObjectInputStream ois = null;
    try {/* ww w  . j a va2 s  .co  m*/
        oos = new ObjectOutputStream(bout);
        oos.writeObject(oldValue);
        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
        ois = new ObjectInputStream(bin);
        newValue = (Serializable) ois.readObject();
    } catch (IOException e) {
        LOG.error(
                "Error cloning Element with key " + key + " during serialization and deserialization of value");
    } catch (ClassNotFoundException e) {
        LOG.error(
                "Error cloning Element with key " + key + " during serialization and deserialization of value");
    } finally {
        try {
            if (oos != null) {
                oos.close();
            }
            if (ois != null) {
                ois.close();
            }
        } catch (Exception e) {
            LOG.error("Error closing Stream");
        }
    }
    return newValue;
}

From source file:edu.umass.cs.mallet.util.bibsonomy.IEInterface.java

public boolean loadCRF(File crfFile) {

    assert (crfFile != null);

    CRF4 crf = null;/* w  w w. ja v  a2s  . c om*/
    try {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(crfFile));
        crf = (CRF4) ois.readObject();
        ois.close();
    } catch (IOException e) {
        System.err.println("Exception reading crf file: " + e);
        e.printStackTrace();
        crf = null;
    } catch (ClassNotFoundException cnfe) {
        System.err.println("Cound not find class reading in object: " + cnfe);
        crf = null;
    }

    //      crf = CRFIO.readCRF(crfFile.toString());

    if (crf == null) {
        System.err.println("Read a null crf from file: " + crfFile);
        System.exit(1);
    }

    this.crf = crf;
    this.pipe = (SerialPipes) crf.getInputPipe();
    if (this.pipe == null) {
        System.err.println("Get a null pipe from CRF");
        System.exit(1);
    }

    //xxx print out the read-in pipes, just for debugging purpose
    //      printPipe();

    //      System.out.println("================= start of CRF ============");
    //      crf.print();
    //      System.out.println("==================end of crf ==============");

    //xxx

    logger.info("Load CRF successfully\n");

    return true;
}

From source file:fr.pasteque.pos.ticket.TicketInfo.java

/** Deserialize as shared ticket */
public TicketInfo(byte[] data) throws IOException {
    ByteArrayInputStream bis = new ByteArrayInputStream(data);
    ObjectInputStream in = new ObjectInputStream(bis);
    try {/*from  w  w  w. j  a v a2 s  . co  m*/
        m_sId = (String) in.readObject();
        tickettype = in.readInt();
        m_iTicketId = in.readInt();
        m_Customer = (CustomerInfoExt) in.readObject();
        m_dDate = (Date) in.readObject();
        attributes = (Properties) in.readObject();
        m_aLines = (List<TicketLineInfo>) in.readObject();
        this.customersCount = (Integer) in.readObject();
        this.tariffAreaId = (Integer) in.readObject();
        this.discountProfileId = (Integer) in.readObject();
        this.discountRate = in.readDouble();
    } catch (ClassNotFoundException cnfe) {
        // Should never happen
        cnfe.printStackTrace();
    }
    in.close();
    m_User = null;
    m_sActiveCash = null;

    payments = new ArrayList<PaymentInfo>();
    taxes = null;
}

From source file:caesar.feng.framework.utils.ACache.java

/**
 * ? Serializable?/*from   ww  w.j a  v a2 s  .c  o m*/
 * 
 * @param key
 * @return Serializable ?
 */
public Object getAsObject(String key) {
    byte[] data = getAsBinary(key);
    if (data != null) {
        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;
        try {
            bais = new ByteArrayInputStream(data);
            ois = new ObjectInputStream(bais);
            Object reObject = ois.readObject();
            return reObject;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (bais != null)
                    bais.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ois != null)
                    ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;

}

From source file:com.raddle.tools.ClipboardTransferMain.java

private void setRemoteClipboard(boolean alert) {
    if (!isProcessing) {
        isProcessing = true;/*from www . j av a  2 s .com*/
        try {
            Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
            Transferable clipT = sysc.getContents(null);
            if (alert && !ClipboardUtils.isClipboardNotEmpty(clipT)) {
                updateMessage("?");
                return;
            }
            updateMessage("???");
            trayIcon.setImage(sendImage);
            final BooleanHolder success = new BooleanHolder();
            doInSocket(new SocketCallback() {

                @Override
                public Object connected(Socket socket) throws Exception {
                    ClipCommand cmd = new ClipCommand();
                    cmd.setCmdCode(ClipCommand.CMD_SET_CLIP);
                    cmd.setResult(ClipboardUtils.getClipResult());
                    // ??
                    ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
                    out.writeObject(cmd);
                    // 
                    ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                    ClipResult result = (ClipResult) in.readObject();
                    if (result.isSuccess()) {
                        StringBuilder sb = new StringBuilder();
                        for (DataFlavor dataFlavor : cmd.getResult().getClipdata().keySet()) {
                            sb.append("\n");
                            sb.append(dataFlavor.getPrimaryType()).append("/").append(dataFlavor.getSubType());
                        }
                        iconQueue.add("send");
                        success.value = true;
                        updateMessage("????? " + sb);
                    } else {
                        updateMessage("???:" + result.getMessage());
                    }
                    in.close();
                    out.close();
                    return null;
                }
            });
            if (!success.value) {
                trayIcon.setImage(grayImage);
            }
        } catch (Exception e) {
            trayIcon.setImage(grayImage);
            updateMessage("???" + e.getMessage());
        } finally {
            isProcessing = false;
        }
    }
}

From source file:fr.eoidb.util.ImageDownloader.java

@SuppressWarnings("unchecked")
public void loadImageCache(Context context) {
    if (BuildConfig.DEBUG)
        Log.v(LOG_TAG, "Loading image cache...");
    File cacheFile = new File(context.getCacheDir(), cacheFileName);

    if (cacheFile.exists() && cacheFile.length() > 0) {
        ObjectInputStream ois = null;
        try {/*from w  w  w  .j av  a2s . c  o  m*/
            HashMap<String, String> cacheDescription = new HashMap<String, String>();
            ois = new ObjectInputStream(new FileInputStream(cacheFile));
            cacheDescription = (HashMap<String, String>) ois.readObject();

            for (Entry<String, String> cacheDescEntry : cacheDescription.entrySet()) {
                loadSingleCacheFile(cacheDescEntry.getKey(), context);
            }
        } catch (StreamCorruptedException e) {
            Log.e(LOG_TAG, e.getMessage(), e);
        } catch (FileNotFoundException e) {
            Log.e(LOG_TAG, e.getMessage(), e);
        } catch (EOFException e) {
            //delete the corrupted cache file
            Log.w(LOG_TAG, "Deleting the corrupted cache file.", e);
            cacheFile.delete();
        } catch (IOException e) {
            Log.e(LOG_TAG, e.getMessage(), e);
        } catch (ClassNotFoundException e) {
            Log.e(LOG_TAG, e.getMessage(), e);
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    Log.e(LOG_TAG, e.getMessage(), e);
                }
            }
        }
    }
}

From source file:jfs.sync.meta.AbstractMetaStorageAccess.java

protected Map<String, FileInfo> getMetaData(String rootPath, String relativePath) {
    if (directoryCache.containsKey(relativePath)) {
        return directoryCache.get(relativePath);
    } // if/*from  ww  w . ja va 2  s . c  o  m*/
    Map<String, FileInfo> result = new HashMap<>();
    ObjectInputStream ois = null;
    try {
        InputStream inputStream = getInputStream(rootPath, getMetaDataPath(relativePath));
        byte[] credentials = getCredentials(relativePath);
        Cipher cipher = SecurityUtils.getCipher(getCipherSpec(), Cipher.DECRYPT_MODE, credentials);
        inputStream = new CipherInputStream(inputStream, cipher);
        if (LOG.isDebugEnabled()) {
            LOG.debug("getMetaData() reading infos for " + relativePath);
        } // if
        ois = new ObjectInputStream(inputStream);
        Object o;
        while ((o = ois.readObject()) != null) {
            if (o instanceof FileInfo) {
                FileInfo fi = (FileInfo) o;
                if (fi.isDirectory()) {
                    String date;
                    synchronized (FORMATTER) {
                        date = FORMATTER.format(new Date(fi.getModificationDate()));
                    }
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(
                                "getMetaData() " + relativePath + getSeparator() + fi.getName() + ": " + date);
                    } // if
                } // if
                result.put(fi.getName(), fi);
            } // if
        } // while
        ois.close();
    } catch (FileNotFoundException | EOFException e) {
        // empty directory or - who cares?
    } catch (Exception e) {
        if (LOG.isInfoEnabled()) {
            LOG.info("getMetaData() possible issue while reading infos " + e, e);
        } // if
    } finally {
        try {
            if (ois != null) {
                ois.close();
            } // if
        } catch (Exception ex) {
            // who cares?
        } // try/catch
    } // try/catch
    directoryCache.put(relativePath, result);
    return result;
}

From source file:gda.gui.BatonPanel.java

private StyledDocument getLog() {
    ObjectInputStream ois = null;
    StyledDocument doc = null;//from w  ww .  ja v a 2 s  .c  om
    String s = getLogPath();
    File f = new File(s);
    try {
        if (f.exists()) {
            FileInputStream fis = new FileInputStream(s);
            ois = new ObjectInputStream(fis);
            doc = (StyledDocument) ois.readObject();
        }
    } catch (Exception e) {
        logger.error("unable to read message log from " + f, e);
    } finally {
        if (ois != null) {
            try {
                ois.close();
            } catch (IOException e) {
                // do nothing
            }
            ois = null;
        }
    }
    return doc;
}

From source file:disko.flow.analyzers.socket.SocketReceiver.java

private Message readMessage(ServerSocket serverSocket) {
    while (true) {
        Socket clientSocket = null;
        ObjectInputStream in = null;
        try {// w ww.  ja va 2  s  .co m
            clientSocket = serverSocket.accept();
            in = new ObjectInputStream(clientSocket.getInputStream());
            Message message = null;
            try {
                message = (Message) in.readObject();
            } catch (IOException e) {
                log.error("Error reading object.", e);
                continue;
            } catch (ClassNotFoundException e) {
                log.error("Class not found.", e);
                continue;
            }
            // while (in.read() != -1);
            // if (in.available() > 0)
            // {
            // System.err.println("OOPS, MORE THAT ON SOCKET HASN'T BEEN
            // READ: " + in.available());
            // while (in.available() > 0)
            // in.read();
            // }
            return message;
        } catch (SocketTimeoutException ex) {
            if (Thread.interrupted())
                return null;
        } catch (InterruptedIOException e) // this doesn't really work, that's why we put an Socket timeout and we check for interruption
        {
            return null;
        } catch (IOException e) {
            log.error("Could not listen on port: " + port, e);
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (Throwable t) {
                    log.error("While closing receiver input.", t);
                }
            if (clientSocket != null)
                try {
                    clientSocket.close();
                } catch (Throwable t) {
                    log.error("While closing receiver socket.", t);
                }
        }
    }
}