Java-java.io.StreamCorruptedException:无效的流标头:3F3F0005-

时间:2018-08-20 14:10:36

标签: java object serializable objectinputstream objectoutputstream

我试图通过ObjectOutputStream发送对象,并在到达文件后将其写入。 (发送:Windows 10,接收:Linux Debian)

接收和发送工作正常,但是当我尝试读取写入了对象的文件时,出现此错误:

java.io.StreamCorruptedException: invalid stream header: 3F3F0005
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:866)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:358)
    at at.dominik.lib.config.Configuration.readObject(Configuration.java:59)
    at at.dominik.lib.modules.user.User.getPosts(User.java:153)
    at at.dominik.lib.modules.user.User.post(User.java:103)
    at net.project.followsystem.PostHandler.run(PostHandler.java:45)
Exception in thread "Thread-7" java.lang.NullPointerException
    at at.dominik.lib.modules.user.User.getPosts(User.java:156)
    at at.dominik.lib.modules.user.User.post(User.java:103)
    at net.project.followsystem.PostHandler.run(PostHandler.java:45)

postgetPosts方法:

public void post(Post post){
    post.setLikes(new ArrayList<>());
    post.setDate(Calendar.getInstance().get(Calendar.DAY_OF_MONTH)+"-"+Calendar.getInstance().get(Calendar.MONTH)+"-"+Calendar.getInstance().get(Calendar.YEAR));
    File folder = new File(this.getUserFile().getParentFile(), "Posts");
    if(!folder.exists())folder.mkdirs();
    ArrayList<Post> posts = this.getPosts();
    if(posts.size()-1<Integer.MAX_VALUE){
        post.setId(1);
        File file = new File(folder, post.getId()+".post");
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException exception) {}

            Configuration configuration = new Configuration(file);
            configuration.writeObject(post);
            configuration.close();
        }
    }

}

public ArrayList<Post> getPosts(){
    File folder = new File(this.getUserFile().getParentFile(), "Posts");
    if(!folder.exists())folder.mkdirs();
    ArrayList<Post> posts=new ArrayList<>();
    for(File file : folder.listFiles()){
        if(!file.isDirectory()){
            if(file.getName().endsWith(".post")){
                Configuration configuration = new Configuration(file);
                Post post = null;
                try{
                    post=(Post)configuration.readObject();
                }catch(Exception exception){MemeFA.getMemeSystem().getSystemSender().sendMessage("An error has occurend while reading the post with the id \""+file.getName().replace(".post", "")+"\" from user \""+this.getName()+"\".", SystemSender.WARNING);}
                ArrayList<String> remove = new ArrayList<>();
                for(String like : post.getLikes()){
                    if(!MemeFA.getMemeSystem().getUserModule().userExists(like)){
                        remove.add(like);
                    }
                }
                post.getLikes().removeAll(remove);
                configuration.resetFile();
                if(remove.size()>0)configuration.writeObject(post);
                posts.add(post);
                configuration.close();
            }
        }
    }
    return posts;
}

writereadObject方法:

public void writeObject(Object object){
    try {
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(this.getFile()));
        objectOutputStream.writeObject(object);
        objectOutputStream.close();
    } catch (IOException exception) {exception.printStackTrace();}
}

public Object readObject(){
    Object object = null;
    try {
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(this.getFile()));
        while(objectInputStream.available() > 0){
            object=objectInputStream.readObject();
        }
        objectInputStream.close();
    } catch (IOException | ClassNotFoundException exception) {exception.printStackTrace();}
    return object;
}

这是从服务器到客户端的serializablingdeserializabling进程:

ObjectInputStream inputStream = new ObjectInputStream(client.getInputStream());
ObjectOutputStream outputStream = new ObjectOutputStream(client.getOutputStream());
Object input = null;
/*while ((input=inputStream.readObject())!=null) {
    break;
}*/

if(inputStream.available()>0)input=inputStream.readObject();

if(this.getClient()!=null) {
    this.getClient().close();
    this.flushed=false;
}

this.setClient(new Socket(this.getAddress(), this.getPort()));
ObjectOutputStream outputStream = new ObjectOutputStream(this.getClient().getOutputStream());

outputStream.writeObject(object);
outputStream.flush();
this.flushed=true;

0 个答案:

没有答案