Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Reading Bytes from the Middle of a Character File

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Reading Bytes from the Middle of a Character File

    Hey I could use some help.

    I'm extracting data from a file that uses Bencoding. Its a Metainfo .torrent file for a Torrent Client I'm working on. I'm reading it char by char using an InputStreamReader.

    The problem is in the middle of the concatenated string file there is a byte string I need to grab. The InputStreamReader only has methods for reading characters.

    Is there a way I can convert the character output of the InputStreamReader into the raw bytes from the file?

    From the documentation... it says that InputStreamReader will read a random number of bytes for each .read() statement as it tries to find characters. So I'm confused at how I might convert that back into raw bytes that might be outside of the range.

    Bellow is the part of the file I want to read.
    It's a short clip of the data becuase the forum doesn't like all the special characters the binary shows up as.
    pieces1100:à•ì†Á' ...

    pieces is the name of the data and 1100 is the number of bytes to be read. It's supposed to be a concatenation of SHA-1 hashes for a file download.

    I'm reading each char from the file like this...
    int intch;
    intch = inputStreamReader.read();
    //char hold veriable
    char ch = (char) intch;

    I know I can reopen the file with a new reader that reads byte by byte. The problem is I would then have to write code to convert and then search the bytes for the location of the byte string in the middle of the file. With the InputStreamReader I have read up to it's location.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Reading Bytes from the Middle of a Character File

    convert the character output of the InputStreamReader into the raw bytes from the file?
    You can get the bytes in a String by calling the String class's get bytes method.
    There might be a problem with that because of charset conversions done by some java I/O classes.

    Another way would be to read the bytes of the file into a byte array and scan that for the bytes that are in the String: pieces1100
    That would position you to the data you want and not get involved with charset conversion problems.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Reading text file character by character into a 2d char[][] array
    By filipasa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 18th, 2019, 07:09 PM
  2. character reading
    By deependeroracle in forum Java Theory & Questions
    Replies: 5
    Last Post: September 19th, 2014, 12:06 PM
  3. Replies: 4
    Last Post: February 14th, 2012, 03:28 PM
  4. reading stream of bytes from serial port
    By KrisTheSavage in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: January 3rd, 2011, 11:02 AM
  5. Reading an int from bytes(binary file) - HELP
    By yogiyogi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 14th, 2010, 02:03 PM