I have a custom image file where the first block of data is ASCII meta data. I need to be able to read this ASCII meta-data part of the file with Java and know when it ends, and when the ‘raw image data’ in another encoding starts.
I was thinking of reading all of the file into a byte[], and then somehow either start reading bytes out of this and convert them to ASCII until I hit the end of the ascii meta-data section, at which point I would store this data. Then I could just rearrange the raw binary data in a different order as-is (no reading necessary). However, the only way I could think about doing this would be to read the ascii stuff byte-by-byte and look for new lines, and concat everything prior to a new line and see if that is the tag which signifies the beginning of the raw image data. However, there must be a better way of reading the ascii part of the file with readLine() and then be able to immediately start with the raw image binary without needed to reopen the file in a new reader and go to the line where in the other reader I found the ‘begin image’ tag.
Any ideas?
FileInputStream(wrapped in aBufferedInputStream)ByteArrayOutputStreamchar(that’s using ASCII implicitly)ByteArrayOutputStreamByteArrayOutputStreamand convert it to a String usingnew String(array, "US-ASCII");It might be possible to do the string searching easily by using aScanneron the input stream, but you have to be careful which pattern you use to make sure it will find the tag without starting to read the image data (since you want to read that yourself from the underlying input stream you’re keeping a separate reference to).Edit: Unfortunately, it looks like Scanner implicitly uses a buffer as well, so the only option left is to implement the string search “manually”.