【发布时间】:2013-02-18 07:48:45
【问题描述】:
我要序列化一个对象并反序列化该对象,但我得到:
- 0 表示可用()
- -1 for read()
-
readByte() 的 EOFException
public static Element getCacheObject(String key, String cacheName, String server) throws IOException, ClassNotFoundException, ConnectException { String url = StringUtils.join(new String[] { server, cacheName, key}, "/"); GetMethod getMethod = new GetMethod(url); ObjectInputStream oin = null; InputStream in = null; int status = -1; Element element = null; try { status = httpClient.executeMethod(getMethod); if (status == HttpStatus.SC_NOT_FOUND) { // if the content is deleted already return null; } in = getMethod.getResponseBodyAsStream(); oin = new ObjectInputStream(in); System.out.println("oin.available():" + oin.available()); // returns 0 System.out.println("oin.read():" + oin.read()); // returns -1 element = (Element) oin.readObject(); // returns the object } catch (Exception except) { except.printStackTrace(); throw except; } finally { try { oin.close(); in.close(); } catch (Exception except) { except.printStackTrace(); } } return element; }
我在这里错过了什么?
【问题讨论】:
-
使用
read()和其他方法(包括readFully())的目的是能够在readObject()抛出InvalidClassException的情况下重建对象。但是,似乎只有readObject()上的ObjectInputStream给出了预期的结果。
标签: java serialization deserialization ehcache objectinputstream