1

オブジェクトをシリアル化し、オブジェクトを逆シリアル化する必要がありますが、次のようになります。

  1. available() の場合は 0
  2. read() の場合は -1
  3. 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;
    }
    

ここで何が欠けていますか?

4

3 に答える 3

1

ObjectInputStream最初にfromを作成してからオンにInputStreamするだけなので、この動作が見られると思います。コンストラクターを確認すると、次のことがわかります。availableInputStreamObjectInputStream

public ObjectInputStream(InputStream in) throws IOException {
    verifySubclass();
    bin = new BlockDataInputStream(in);
    handles = new HandleTable(10);
    vlist = new ValidationList();
    enableOverride = false;
    readStreamHeader();
    bin.setBlockDataMode(true);
} 

readStreamHeader入力ストリームからヘッダーを読み取るメソッドがあります。InputStreamそのため、 の構築中にすべてのデータが読み込まれる可能性がありObjectInputStreamます。

于 2013-02-18T07:59:57.857 に答える
0

あなたは変数を呼び出しavailable and readていinますがreadObjectoin変数を呼び出しています。

于 2013-02-18T07:55:36.200 に答える