首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ObjectInputStream readObject EOFException

ObjectInputStream readObject EOFException
EN

Stack Overflow用户
提问于 2014-07-17 15:49:19
回答 1查看 796关注 0票数 0

我曾经尝试过使用this question's answer来获得一个有效的实现,但是我得到了各种错误,现在我开始使用EOFException,并且在调试时,文件似乎没有被写入。

其目标是从URL下载图像,将其保存到内部缓存,然后从该缓存中获取图像以供显示。我哪里出错了?在读取EOFException的行的CachedImage.java中抛出byte[] data = (byte[]) ois.readObject();

CachedImage.java

代码语言:javascript
复制
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获取图像以将图像写入内部:

代码语言:javascript
复制
@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) {
          }
        }
      }
    }
  }

下载和保存后从磁盘读取映像的代码:

代码语言:javascript
复制
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) {

    }
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-17 18:53:50

我从http://www.javablogging.com/what-are-writeobject-and-readobject-customizing-the-serialization-process/上读到

ObjectOutputStream使用反射来确定是否声明了这些方法。它使用getPrivateMethod,因此这些方法必须声明为私有,以便由ObjectOutputStream使用。

因此,将CachedImage的方法writeObject更改为私有(因为您将其作为公共发布)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24808135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档