Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

NX: Using InputStreamReader together with DataInputStream

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the contractor project for my SCJD exam.
I went through the data file format.
I managed to read the file just using the DataInputStream (for the numeric values in the header), and for the text values i just used the FileInputStream's read(byte[] b), and converted the byte[] into a String.
But i want to follow the encoding stuff given in the instructions.
So i thought of using InputStreamReader(InputStream in, String encoding), and the DataInputStream to read the binary primitive datas as before.
But i am stuck with the following problem.
I start of using the DataInputStream and read the header values. I read the magic cookie, the offset value, and the number of fields. Then i read the length of the first field name. All using the DataInputStream. Then the few bytes is text value encoded in US-ASCII format. So now i want to use the InputStreamReader and read the next few bytes.
Once i made use of the InputStreamReader, i want to go back to using the DataInputStream to read the next available primitive data, but i couldnt. It complains that EOF has been reached and throws EOFException. It seems like the InputStreamReader is stuck somewhere and makes it look like EOF?
I am using these three objects
InputStream inputFile = new FileInputStream("db-2x2.db");
DataInputStream inputStream = new DataInputStream(inputFile);
Reader reader = new InputStreamReader(inputFile,"US-ASCII");
Thoughts appreciated.
[ August 29, 2003: Message edited by: Ken Wong ]
 
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ken,
Try "public final void readFully(byte[] b)" on your DataInputStream followed by some "new String(byte[], String charsetName)".
Best,
Phil.
 
Arun Kumar
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou,
that solved my encoding usage problem. I can convert string to a byte array using a certain encoding, and convert the read bytes into a string using a certain encoding. So i will be safe to say that i followed the instructions of using the US-ASCII enconding while writing into and reading out of the file.
But out of curiosity why is that when i couple InputStreamReader and DataInputStream i cannot switch between them. After i switch to the InputStreamReader i cant get back to the DataInputStream. Throws an EOFException.
Ken
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ken,
I am happy I could help.

But out of curiosity why is that when i couple InputStreamReader and DataInputStream i cannot switch between them. After i switch to the InputStreamReader i cant get back to the DataInputStream. Throws an EOFException.


I really don't know. I just suppose that both input streams are not synchronized in some way, but as anyway you got a solution...
Please don't reply to me directly, because I'll quit this forum for a while, probably till I've done with my assignment (db is done, I am starting the sockets part and will finish with server and client GUIs). I am actually in a hurry to get my assignment done and I noticed that when reading posts in this forum, helping people you can help is so compelling than you do it even when you have not the time to do so. F..g altruism !
You'll find a lot of people here willing to help you as far as your question is precise (as yours was) and simple.
But for more technical questions, I think that other forums here on JavaRanch (the ones specialized in a given technology) are a better place to post them. For example, a very interesting discussion between Jim and Max about FileChannel has been moved recently to such a forum.
Our new bartender Andrew is amazingly fast and willing to reply and bring help, and I couldn't count the number of times Andrew helped me (even when the issue was rather technical or complex ).
Best and good luck,
Phil.
[ August 30, 2003: Message edited by: Philippe Maquet ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic