簡體   English   中英

ObjectInputStream readObject EOFException

[英]ObjectInputStream readObject EOFException

我嘗試使用此問題的答案來獲得正常運行的實現,但出現各種錯誤,現在是EOFException,在調試時,似乎文件未寫入。

目的是從URL下載圖像,將其保存到內部緩存中,然后再從該緩存中獲取以進行顯示。 我哪里出問題了? 在讀取byte[] data = (byte[]) ois.readObject();的行上,將EOFException拋出在CachedImage.java中byte[] data = (byte[]) ois.readObject();

CachedImage.java

package com.example.droid;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;

import android.graphics.Bitmap;

public class CachedImage implements Serializable {
  private static final long serialVersionUID = -12345678987654321L;
  private transient Bitmap _bmp;

  public CachedImage(Bitmap bmp) {
    this._bmp = bmp;
  }

  public void writeObject(ObjectOutputStream oos) throws IOException {
    oos.defaultWriteObject();

    if (this._bmp != null) {
      int bytes = this._bmp.getWidth() * this._bmp.getHeight() * 4;

      ByteBuffer buffer = ByteBuffer.allocate(bytes);
      this._bmp.copyPixelsToBuffer(buffer);

      if (buffer.hasArray()) {
        try {
          String configName = this._bmp.getConfig().name();

          byte[] array = buffer.array();

          oos.writeObject(array);
          oos.writeInt(this._bmp.getWidth());
          oos.writeInt(this._bmp.getHeight());
          oos.writeObject(configName);
        } catch (BufferUnderflowException e) {
        }
      }
    } else {
      oos.writeObject(null);
    }
  }

  private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
    ois.defaultReadObject();

    byte[] data = (byte[]) ois.readObject();
    if (data != null) {
      int w = ois.readInt();
      int h = ois.readInt();
      String configName = (String) ois.readObject();

      Bitmap.Config configBmp = Bitmap.Config.valueOf(configName);
      Bitmap bitmap_tmp = Bitmap.createBitmap(w, h, configBmp);
      ByteBuffer buffer = ByteBuffer.wrap(data);

      bitmap_tmp.copyPixelsFromBuffer(buffer);

      this._bmp = bitmap_tmp.copy(configBmp, true);

      bitmap_tmp.recycle();
    } else {
      this._bmp = null;
    }
  }
  public Bitmap getBitmap() {
    return this._bmp;
  }
}

以下是觸發調用的代碼段:

異步回調函數,用於從URL提取圖像以將圖像寫入內部時:

@Override
protected void onPostExecute(Bitmap result) {
  if (result != null) {
      FileOutputStream output = null;
      ObjectOutputStream oos = null;
      try {
        output = ICEApplication.getAppContext().openFileOutput(filename, Context.MODE_PRIVATE);
        oos = new ObjectOutputStream(output);
        CachedImage cachedImage = new CachedImage(result);
        oos.writeObject(cachedImage);
      } catch (Exception e) {
        Log.d("DEBUG", "Exception: " + e.getMessage());
      } finally {
        if (output != null) {
          try {
            oos.close();
            output.close();
          } catch (IOException e) {
          }
        }
      }
    }
  }

下載並保存后從磁盤讀取圖像的代碼:

Bitmap image = null;
FileInputStream input = null;
ObjectInputStream ois = null;
try {
  input = ICEApplication.getAppContext().openFileInput(urldisplay);
  ois = new ObjectInputStream(input);
  CachedImage cachedImage = (CachedImage)ois.readObject();

  image = cachedImage.getBitmap();
} catch (Exception e) {
  Log.d("DEBUG", "Exception: " + e.getMessage());
  return null;
} finally {
  if (input != null) {
    try {
      ois.close();
      input.close();
    } catch (IOException e) {

    }
  }
}

我從http://www.javablogging.com/what-are-writeobject-and-readobject-customizing-the-serialization-process/閱讀

ObjectOutputStream使用反射來找出是否聲明了這些方法。 它使用getPrivateMethod,因此必須將那些方法聲明為私有,以便由ObjectOutputStream使用

因此,將CachedImage的方法writeObject更改為private(因為您將其發布為公共)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM