gpt4 book ai didi

java - 使用序列化代理模式不需要 writeObject() 吗?

转载 作者:行者123 更新时间:2023-11-30 03:44:10 26 4
gpt4 key购买 nike

我正在为我的一堆类使用序列化代理模式,并且最近将 FindBugs 添加到我的构建过程中,现在我想知道 FindBugs 是否正确......

这是有问题的类:

public class Block implements Serializable {
private static final long serialVersionUID = 584958030434385L;

private final float confidence;
private final Rectangle boundingBox;
private final Rectangle baseline;
private final BufferedImage binaryImage;
private final List<Paragraph> paragraphs;

private TessResult parentTessResult;

private Block(final float confidence, final Rectangle boundingBox, final Rectangle baseline, final BufferedImage binaryImage, final List<Paragraph> paragraphs) {
this.confidence = confidence;
this.boundingBox = Objects.requireNonNull(boundingBox, "boundingBox");
this.baseline = Objects.requireNonNull(baseline, "baseline");
this.binaryImage = binaryImage;
this.paragraphs = Objects.requireNonNull(paragraphs, "paragraphs");
}

void setParentTessResult(final TessResult parentTessResult) {
this.parentTessResult = Objects.requireNonNull(parentTessResult, "parentTessResult");
}

public float getConfidence() {
return confidence;
}

public Rectangle getBoundingBox() {
return boundingBox;
}

public Rectangle getBaseline() {
return baseline;
}

public BufferedImage getBinaryImage() {
return binaryImage;
}

public List<Paragraph> getParagraphs() {
return paragraphs;
}

public TessResult getParentTessResult() {
return parentTessResult;
}

public static class BlockBuilder {
private final float confidence;
private final Rectangle boundingBox;
private final Rectangle baseline;
private final BufferedImage binaryImage;
private final List<Paragraph> paragraphs = new ArrayList<>();

public BlockBuilder(final float confidence, final Rectangle boundingBox, final Rectangle baseline, final BufferedImage binaryImage) {
this.confidence = confidence;
this.boundingBox = boundingBox;
this.baseline = baseline;
this.binaryImage = binaryImage;
}

public BlockBuilder addParagraph(final Paragraph paragraph) {
paragraphs.add(Objects.requireNonNull(paragraph, "paragraph"));
return this;
}

public Block build() {
return new Block(confidence, boundingBox, baseline, binaryImage, paragraphs);
}
}

private Object writeReplace() throws IOException {
return new SerializationProxy(this);
}

private void readObject(final ObjectInputStream stream) throws InvalidObjectException {
throw new InvalidObjectException("Proxy required");
}

private static class SerializationProxy implements Serializable {
private static final long serialVersionUID = 12321313232553L;

private final float confidence;
private final Rectangle boundingBox;
private final Rectangle baseline;
private final byte[] binaryImageBytes;
private final List<Paragraph> paragraphs;

private SerializationProxy(final Block block) throws IOException {
this.confidence = block.confidence;
this.boundingBox = block.boundingBox;
this.baseline = block.baseline;
this.binaryImageBytes = bufferedImageToBytes(block.binaryImage);
this.paragraphs = block.paragraphs;
}

private Object readResolve() throws IOException {
BufferedImage binaryImage = bytesToBufferedImage(binaryImageBytes);
Block block = new Block(confidence, boundingBox, baseline, binaryImage, paragraphs);
for (Paragraph paragraph : paragraphs) {
paragraph.setParentBlock(block);
}
return block;
}
}
}

要观察的关键点是它存储了 BufferedImage,但在内部它将 BufferedImage 存储为 byte[] 以进行序列化。

现在我从 FindBugs 收到此警告:

Class com.yob.dpc2.ocr.utils.data.Block defines non-transient non-serializable instance field binaryImage ["com.yob.dpc2.ocr.utils.data.Block"] At Block.java:[lines 18-95]

因此,我深入研究了这个问题,发现我没有提供 private void writeObject(ObjectOutputStream oos) throws IOException...

所以我的问题是:我应该提供 writeObject() 方法吗?如果是这样,有没有办法可以重用 writeReplace() 方法或执行其他操作来防止逻辑重复?

最佳答案

Should I provide that writeObject() method?

不,因为你的 writeReplace() 方法永远无法调用它。

您在 FindBugs 中遇到了限制。让您的类(class)成为一个异常(exception)。

关于java - 使用序列化代理模式不需要 writeObject() 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26156523/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com