Re: [問題] ObjectOutputStream + ImageIO 出現的問題

看板java作者 (小安)時間14年前 (2010/01/02 10:20), 編輯推噓0(0021)
留言21則, 3人參與, 最新討論串6/7 (看更多)
※ 引述《Maisky (McDummy)》之銘言: : 如果要用ObjectInputStream和ObjectOutputStream, 傳的object就應該要 : 是Serializable. 而BufferedImage不是Serializable,所以 : ImageIO.write(BufferedImage, "png", ObjectOutputStream) 並沒有說用 ObjectOutputStream 傳的 object 就必須 Serializable。 正確的說法應該是, 透過 writeObject() 傳出去的 object 才需要 Serializable。 ObjectOutputStream 用的是 Composition 的概念, 在他的內部其實是另一個 OutputStream, 當呼叫的 method 與 Object 的操作無關, ObjectOutputStream 就會直接把呼叫轉給其內部的 OutputStream。 以 write(byte[]) 為例,source code 如下: public void write(byte[] buf) throws IOException { bout.write(buf, 0, buf.length, false); } // 其中 bout 就是 ObjectOutputStream 內部的 Stream ImageIO 跟 ObjectOutputStream 的 source 都有點複雜, 所以接下來我就直接推理了。 如果你有看完這串所有的討論, 你應該會知道後來我們已經發現這個問題跟 ObjectOutputStream 無關, 即使是用 FileOutputStream 或 SocketOutputStream 也會有相同的問題, 並且我們已經找出問題並解決了。 如果如你所說,ImageIO 是用 Serializable 方式傳送圖片, 你要如何解釋為什麼使用 FileOutputStream 沒有問題? --- ImageIO 不是用 Serializable 在傳資料的, 他是把 image,依照你所指定的 format (jpg, png, etc) 寫成對應的 Image File, 所以用 ImageIO 把 image 寫成檔案後,你家的繪圖軟體才打得開。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.132.160.117

01/02 10:32, , 1F
Composition? 這不是 Decoration 嗎 ?XD
01/02 10:32, 1F

01/02 10:57, , 2F
Decorated 的精神之一就是用 Composition 代替 Inheritance
01/02 10:57, 2F

01/02 11:07, , 3F
你在開玩笑吧 , 你翻翻API就看的到 ObjectOutputStrema有
01/02 11:07, 3F

01/02 11:09, , 4F
繼承 OutputStream , 且被裝飾者本就跟裝飾者要同一繼承體系
01/02 11:09, 4F

01/02 11:11, , 5F
怎麼會有「代替」繼承這回事出現 , 這兩者在這的性質不同啊.
01/02 11:11, 5F

01/02 11:16, , 6F
還是你在講得是子類別延伸出去的繼承樹 XD
01/02 11:16, 6F

01/02 11:18, , 7F
*避免
01/02 11:18, 7F

01/02 11:25, , 8F
他還是有利用到繼承沒錯,但那是為了達成 setComponent的手段
01/02 11:25, 8F

01/02 11:29, , 9F
如果是要 setComponent , interface-implement 就ok了 XD
01/02 11:29, 9F

01/02 11:31, , 10F
但是 OutputStream 裡頭有提供實作,我覺得這樣做並沒有問題
01/02 11:31, 10F

01/02 11:31, , 11F
覺得應是OutputStream要拿來作為被裝飾的基底類別吧:3
01/02 11:31, 11F

01/02 11:32, , 12F
覺得雖然Composition可以解釋 , 不過總覺得很彆扭 XD
01/02 11:32, 12F

01/02 11:33, , 13F
ttp://tinyurl.com/yfh8269 最後一段
01/02 11:33, 13F

01/02 11:37, , 14F
是說講Composition 我總會想到 Composition Pattern . XDDD
01/02 11:37, 14F

01/02 11:38, , 15F
http://tinyurl.com/yksyaza 這篇應該比較有說服力了
01/02 11:38, 15F

01/03 09:10, , 16F
我說的是BufferedImage不是Serializable,所以才會有問題的
01/03 09:10, 16F

01/03 09:12, , 17F
Only objects that support the java.io.Serializable
01/03 09:12, 17F

01/03 09:13, , 18F
interface can be written to streams.
01/03 09:13, 18F

01/03 09:21, , 19F
我並沒說ImageIO 是用 Serializable 方式傳送圖片
01/03 09:21, 19F

01/03 12:27, , 20F
ImageIO 是重新分析 Object 內容用對應 format byte 陣列傳送
01/03 12:27, 20F

01/03 12:29, , 21F
我不懂你現在到底想幹嘛,證明你是對的?
01/03 12:29, 21F
文章代碼(AID): #1BFgs0g8 (java)
討論串 (同標題文章)
文章代碼(AID): #1BFgs0g8 (java)