aspose file tools*
The moose likes I/O and Streams and the fly likes ObjectInputStream peek Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "ObjectInputStream peek" Watch "ObjectInputStream peek" New topic
Author

ObjectInputStream peek

Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Hi all

I want to be able to "peek" at my ObjectInputStream. I thought mark / reset would of worked, but that is not supported with ObjectInputStream. I need to use ObjectInputStream because I am reading and writing serialized objects. Any thoughts on how to accomplish this?

Thanks,

AMD
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 797

1. Would you explain why do you want to use ObjectInputStream ?

I thought mark / reset would of worked, but that is not supported with ObjectInputStream.

2. mark & reset are supported in ObjectInputStream see Java documentation for details.


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
when I call the mark supported method it returns false. I need to use ObjectInputStream because I am writing and reading in serialized objects.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Andrew Mcmurray wrote:when I call the mark supported method it returns false. I need to use ObjectInputStream because I am writing and reading in serialized objects.


Indeed, the Javadoc allows you to conclude that it will, so I'm not sure why Ninad thinks otherwise.

Anyway: do you want to peek in the sense of reading an object, then reading it again? Or just check if there are some bytes to read, or what, exactly? If I know what you want to do, I'm sure I can help you figure out a solution.


[Jess in Action][AskingGoodQuestions]
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Hi Ernest,

So basically what I am doing is a merge sort. I have separate file chunks and I need to "peek" at the first record(object) of each file and then return the smallest one. I only want to advance the file pointer in file where the smallest record was found. Does this help?

Thanks,

AMD
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

If they're files, do you have the paths? The very easiest thing to do would be to just open the files, read an object from each, then close those streams and open new ones.

A more complicated alternative that would work with network streams as well as files would be to create a wrapper class for an ObjectInputStream called, say, "ObjectSource". It would have a method getNextObject which looked something like (just off the top of my head, not tested)



Then you call getNextObject() to get an object from the stream, or unreadObject() to return an object to be read again in FIFO order.
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Thanks Ernest

I will give your second idea a shot. For the 1st idea I do have the paths to the files, but I am dealing with 100K records per file and on average ten files(chunks) so I think the overhead of opening and closing the stream might be a performance killer.

Thanks,

AMD
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 797

Andrew Mcmurray wrote:when I call the mark supported method it returns false.
Andrew I agree with you

Ernest Friedman-Hill wrote:Indeed, the Javadoc allows you to conclude that it will, so I'm not sure why Ninad thinks otherwise.
Thanks Ernest for nice explaination. Ignore my previous post It was wrong.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ObjectInputStream peek