• Post Reply Bookmark Topic Watch Topic
  • New Topic

serializing image file  RSS feed

 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I was testing serializing various types of files like text, zip and image. For text and zip, things were fine, but i dont know why, serializing and deserializing image is proving to be too hard. I am using input and output streams to read and write object and put it in a 2D byte array ( something to do with my requirement ).. but the thing is that, other file types seem to work fine. What is it with the image files? how is it different from other binary files that we read and write? it is still a binary data, right?

thanks
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tangent: What is the point of serializing the contents of a file? The file itself is an external representation of the contents, and can be used to transfer the contents elsewhere. Do you want the Java process to transfer the file contents to another JVM, instead of involving the file system?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reading and writing images is very easy with ImageIO (see the API docs of the package javax.imageio). It does only support a few basic image file formats though.

You're not showing how you're trying to read and write your images, so it's hard to say what exactly you're doing wrong. File types such as JPG and PNG have very specific formats, it's not as easy as putting pixel data in an array and serializing that to or from a file using Java's standard serialization mechanism.
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying. Well, the issue is I have a generic process to serialize and de-serialize the files.. Now, about the content part, I am not serializing the content. I know File object is just an abstraction. So, I am serializing the file object and passing the content seperately in a byte array. now, when i receive this byte array on another system, i am recovering the serialzed file and writing the contents back to it. this logic works well, when i use text or zip format.. but not for image files... it shows file is corrupt..

i have no means to know if the file is of particular type.. that is why i have a common function for all file types..
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The file type doesn't matter - bytes are bytes. But I'm still curious why you want to serialize file contents.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us your code to serialize and deserialize the data - just from your description of the problem, without seeing the code, it's very hard to tell if there might be a bug in it.
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:The file type doesn't matter - bytes are bytes. But I'm still curious why you want to serialize file contents.


I know it is idiotic.. But it is a client requirement... for a legacy application.. what can I say..


And here is the code below:


Please ignore the hardcodings, this was just a working prototype...
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you trying to store the data that is read from the file as characters in a StringBuilder, that you convert back to a byte array in line 43?

StringBuilder and strings are not suitable for storing arbitrary binary data. If you call getBytes() on a String, as you're doing in line 43, the data will be converted according to the default character encoding of your system, which likely corrupts the data, if the data doesn't consist of actual text.

Solution: Do not store the data in a StringBuilder. Instead, read it as binary data, store it in a byte array.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:I know it is idiotic.. But it is a client requirement... for a legacy application.. what can I say..

Well, if you know it's idiotic, you can try and explain this to (a) your boss, or (b) to the client themself. And my first question to them would be:
What do you think Java can do that your application can't?

Part of good programming is avoiding "noise" code (as Shakespeare said: 'full of sound and fury, signifying nothing').

PS: There are several ways of determining or assuming file content, including:
  • Filename suffix.
  • Magic number (on Unix; don't know what the equivalent is on Windows).

  • Winston
     
    s ravi chandran
    Ranch Hand
    Posts: 579
    6
    Java jQuery
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:
    s ravi chandran wrote:I know it is idiotic.. But it is a client requirement... for a legacy application.. what can I say..

    Well, if you know it's idiotic, you can try and explain this to (a) your boss, or (b) to the client themself. And my first question to them would be:
    What do you think Java can do that your application can't?

    Part of good programming is avoiding "noise" code (as Shakespeare said: 'full of sound and fury, signifying nothing').

    PS: There are several ways of determining or assuming file content, including:
  • Filename suffix.
  • Magic number (on Unix; don't know what the equivalent is on Windows).

  • Winston


    well, i was trying to point out the irony.. legacy application exists, it wont bend or move.. certain rules are already defined. so when it comes to mixing the old with the new, certain inconsistencies are bound to be there. my boss knows it, client knows it. they agree on its oddity, but still it has to be done.. so no point complaining. we are upgrading certain core areas of the legacy application, previous one was in jvm 1.2, current one will be jvm 1.5, that is as far as it goes. it is not just the jvm upgradation though. and about knowing the file types, well, i can find that.. I was just trying to fix everything with one magic wand.. but it looks like i will have to specific nuts for specific bolts..

    and regarding image file serialization, i have seen some sites showing serialization of image file using ImageIcon as a wrapper, is that the only way? If yes, then I will have to create a sort of switch mechanism with file types as a key.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    s ravi chandran wrote:well, i was trying to point out the irony.. legacy application exists, it wont bend or move.. certain rules are already defined. so when it comes to mixing the old with the new, certain inconsistencies are bound to be there. my boss knows it, client knows it. they agree on its oddity, but still it has to be done.. so no point complaining.

    Of course there is; and that's exactly where a good programmer will complain, because it sounds to me as if you're being told not only what to do, but how to do it - in which case, why are they paying you?

    If you were able to supply more background, we might be able to give you a few design alternatives; but if you've already allowed someone to dictate how you're going to implement, there's not a lot we can do.

    Winston
     
    Paul Clapham
    Sheriff
    Posts: 22829
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    s ravi chandran wrote:and regarding image file serialization, i have seen some sites showing serialization of image file using ImageIcon as a wrapper, is that the only way? If yes, then I will have to create a sort of switch mechanism with file types as a key.


    If you're being told you have to "serialize" an image file, because of some legacy application, then wouldn't you have to serialize it in exactly the same way that application does it? If so, then there should already exist specific descriptions of that application's serialization, and you should be looking for them.
     
    s ravi chandran
    Ranch Hand
    Posts: 579
    6
    Java jQuery
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jesper de Jong wrote:Why are you trying to store the data that is read from the file as characters in a StringBuilder, that you convert back to a byte array in line 43?

    StringBuilder and strings are not suitable for storing arbitrary binary data. If you call getBytes() on a String, as you're doing in line 43, the data will be converted according to the default character encoding of your system, which likely corrupts the data, if the data doesn't consist of actual text.

    Solution: Do not store the data in a StringBuilder. Instead, read it as binary data, store it in a byte array.

    Sorry, did not see your message yesterday. Will try it today itself.

    Paul Clapham wrote:If you're being told you have to "serialize" an image file, because of some legacy application, then wouldn't you have to serialize it in exactly the same way that application does it? If so, then there should already exist specific descriptions of that application's serialization, and you should be looking for them.

    this legacy application works in a different manner.. They were sending serialized file object in an arraylist and a background service used to take care of the file content transfer.. Our project design doesn't support that. I have just that arraylist to pass the file with content..

    Winston Gutkowski wrote: Of course there is; and that's exactly where a good programmer will complain, because it sounds to me as if you're being told not only what to do, but how to do it - in which case, why are they paying you?

    If you were able to supply more background, we might be able to give you a few design alternatives; but if you've already allowed someone to dictate how you're going to implement, there's not a lot we can do.

    Ya, I know.. It is complicated.. ~_~

    Anyways, I will try out what Jesper suggested, will revert back with results. It seems logical, I missed that simple thing somehow..
     
    s ravi chandran
    Ranch Hand
    Posts: 579
    6
    Java jQuery
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    okay, it worked out well. Thanks for all your suggestions.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!