ObjectOutputStream在其他语言中的翻译为:

huangapple go评论87阅读模式
英文:

ObjectOutputStream read in other language

问题

以下是翻译好的内容:

我过去在使用 Android Studio 和 Java 保存项目。但现在我想在 Flutter SDK(Dart 语言)中的重写应用程序中读取那些数据。但似乎这些文件非常依赖于 Java。这是 Java 中的保存和加载方法。

public void save(Item item)
{
    String json = gson.toJson(item);
    FileOutputStream fos = null;
    try {
        fos = context.openFileOutput("item_"+ item.getUid(), Context.MODE_PRIVATE);
        ObjectOutputStream os = new ObjectOutputStream(fos);
        os.writeObject(json);
        os.close();
        fos.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private Item loadItem(String fileName)
{
    FileInputStream fis = null;
    try {
        fis = context.openFileInput(fileName);
        ObjectInputStream is = new ObjectInputStream(fis);
        Object object = is.readObject();
        Item item = null;
        String json = (String)object;
        item = gson.fromJson(json, Item.class);
        is.close();
        fis.close();
        return item;
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return null;
}

现在来看 Dart 中的读取部分:

Uint8List bytes = f.readAsBytesSync();
var data = bytes.buffer.asByteData();
// 如何检测 Java?
if (data.getUint8(0) == 0xAC && data.getUint8(1) == 0xED) { }

bytes 的长度相当大。它是一个长度为 3,245,500 的数组。
JSON 字符串的长度应在 740 到 840 字节之间(即相对较短的 JSON)。

我如何只读取 JSON 部分,而不是整个 Java“开销”或“Java 序列化格式”?

英文:

I used to save items in Android using Android Studio with Java. But now I want to read in exactly that data in my rewritten app in Flutter SDK (Dart language). But it seems that the files that are written are very Java specific. This is the save and load method in Java.

public void save(Item item)
{
    String json = gson.toJson(item);
    FileOutputStream fos = null;
    try {
        fos = context.openFileOutput("item_"+ item.getUid(), Context.MODE_PRIVATE);
        ObjectOutputStream os = new ObjectOutputStream(fos);
        os.writeObject(json);
        os.close();
        fos.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}


private Item loadItem(String fileName)
{
    FileInputStream fis = null;
    try {
        fis = context.openFileInput(fileName);
        ObjectInputStream is = new ObjectInputStream(fis);
        Object object = is.readObject();
        Item item = null;
        String json = (String)object;
        item = gson.fromJson(json, Item.class);
        is.close();
        fis.close();
        return item;
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return null;
}

And now for reading it in Dart

Uint8List bytes = f.readAsBytesSync();
var data = bytes.buffer.asByteData();
// HOW TO DETECT JAVA
if (?SOMETHING?) { }

The length of bytes is quite large. It is an array of length = 3 245 500.
The length of the json string should be between 740 and 840 bytes long (i.e. a relatively short json).

How could I read in only the json part and not the entire Java 'overhead'/'java serialization format'
ObjectOutputStream在其他语言中的翻译为:

答案1

得分: 1

你目前正在做的是:

  1. 序列化为 JSON
  2. 使用 Java 序列化将 Java 字符串与 JSON 数据序列化

相反地,修改你的代码为:

  1. 序列化为 JSON(直接写入文件,或者先转换为字符串,然后使用将 OutputStreamWriter 包装在 FileOutputStream 上的方式写入文件)。

试图在另一种语言中重新实现 Java 序列化非常痛苦,你目前对 Java 序列化的使用几乎没有意义,它只会增加不必要的开销并使你的工作变得更加困难。将其从代码中移除,直接将 JSON 写入文件。

这将简化你的代码,无需在 Dart 中实现 Java 序列化。

为了能够处理当前以 Java 序列化包装 JSON 数据写入的文件,我建议在你的 Java 应用程序中使用一些额外的代码来检测文件是否为 Java 序列化文件,如果是,则进行一次性转换以直接存储 JSON 数据。

或者,你可以阅读 Java 对象序列化规范 中关于 Java 序列化协议的内容。

英文:

What you are currently doing is:

  1. Serialize to JSON
  2. Serialize the Java String with JSON data using Java serialization

Instead, change your code to:

  1. Serialize to JSON (directly to file, or first to string, then to file using OutputStreamWriter wrapping a FileOutputStream).

Trying to reimplement Java serialization in another language is extremely painful, and your current usage of Java serialization makes little sense, and it is unnecessary overhead and only makes your life harder. Remove it from the mix and write the JSON directly to file.

It will simplify your code, and removes the need to implement Java serialization in Dart.

To be able to handle current files written in this combination of Java serialization wrapping JSON data, I would recommend using some extra code in your Java application to detect if a file is Java serialization and if so do a one-off conversion to store the JSON directly.

Alternatively, you can read up on the Java serialization protocol in Java Object Serialization Specification.

huangapple
  • 本文由 发表于 2020年9月25日 17:09:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64061157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定