last day (14 days later) » 

12:43 AM
1
Q: Read a JPG file until certain two bytes occur

TetramputechtureSo I'm reading JPG files, and I've finished reading the 'header' data and now I'm onto the actual image data. Thing is, I don't know the size of the image beforehand, so I cannot create an array to read from. What I could do, though, is read from the end of the 'header' until the end of the image...

 
I'd recommend using a ByteArrayOutputStream or ByteBuffer or similar. ArrayList<Byte> will have a large memory footprint from boxing every byte as a Byte object.
 
Alright! Thanks! Edited to reflect current code.
 
Also byteData[0].equals("FF") should be byteData[0] == (byte) 0xFF, assuming byteData is a byte[] array. Comparing bytes against Strings won't work.
 
I'm sorry, I forgot to specify that byteData is array of Strings! Edited.
 
It would be best to switch to a byte[] array, then. Using Strings would be an unnecessary inefficiency.
 
12:43 AM
I didn't realize you could convert hex values to bytes, thank you! That even makes my Bitmap class more efficient, too! (: @JohnKugelman
Also, if the byte array is made from values already in hex, do you still have to do the conversion?
@JohnKugelman
 
What conversion do you mean?
Do you mean the (byte) cast when I wrote (byte) 0xFF?
 
Yes! I'm reading from a file that is arranged like so (byte byte byte byte etc) with (byte) being a hex value, such as FF.
When I make the byte array from that file, will those bytes still be hex values, or will I have to cast them to hex again?
:@JohnKugelman
 
Heheh you don't have to ping my name, I'm here. :-P
Let me think of what I want to say.
 
Alright!
 
Something you want to keep separated when you're working with bytes and binary data and such
Is the difference between various integer types
meaning byte, short, int, long
 
12:54 AM
right
 
those are the four integer types
 
byte can only be between -128 and 127 i know
 
and the ways you can type a number
If you type a number out, the default is for it to be an int
 
four bytes
 
12:55 AM
but if I'm reading from a file, and putting those values in a byte array, they are automatically casted to byte
bytes
 
so if I type 255 or I type 0xFF those are two representations of the same int value
 
and if that file is full of hex data, will those bytes be represented as hex or as integers?
 
What I'm talking about here is in your source code. If you type 0xFF in your source code, the Java compiler sees an int.
To get a byte literal
you have to cast it
 
you have to cast to byte
alright
 
(byte) 0xFF
Okay, so that's why I wrote the cast. This is a totally free operation, it is done at compile time, not run time. It's annoyingly verbose that you have to write so much. But that's how Java is.
 
12:57 AM
alright! so just to be clear
take, for example, my bitmap class (which works), where I test the signature
    // signature must be 42 4D ("BM")
    if(!(bData[0] == 42 && bData[1] == 4D))
I would have to do (byte) 0x42 and (byte) 0x4D, correct?
 
right!
if you don't, if you do, say, byte value = 0x4D;
Java will throw a compile error saying you can't assign an int to a byte variable
Similar thing with comparisons. Actually with comparisons you might not get a compile time error but you might get wonky runtime bugs instead due to the int/byte mixup.
 
thank you very much, dude! that helps a ton! time to rewrite a lot of my bitmap code, but its all worth it for efficiency!
 
Okay but yeah, so when you're reading in your file
When you're dealing with an InputStream
you're going to get bytes
so it's most natural if you work in terms of bytes everywhere
 
// fetch file
try {
fin = new FileInputStream(this.filename);
} catch (FileNotFoundException ex) {
System.err.printf("ERROR: File %s not found!\n", this.filename);
Logger.getLogger(Bitmap.class.getName()).log(Level.SEVERE, null, ex);
}

// get header byteData in bytes
this.bData = new byte[this.hSize];

try {
fin.read(bData);
} catch (IOException ex) {
System.err.printf("ERROR: File %s is unreadable!\n", this.filename);
Logger.getLogger(Bitmap.class.getName()).log(Level.SEVERE, null, ex);
}
thats my file reading code!
 
yeah that's a good start
 
1:01 AM
thanks!
 
oh btw another small thing you can do is to add private static final Logger log = Logger.getLogger(Bitmap.class.getName()); to the top of your class
then you can simplify those logging statements
to log.severe(null, ex);
 
thanks! thats awesome!
 
oh actually i'd combine that with the printf too
make it
log.log(Level.SEVERE, String.format("File %s not found!", this.filename), ex);
or something like that
I don't use java.util.logging so that's my best guess at it.
Anyways hope you get a good answer to your original question. I'll let someone else tackle that. :)
I'll leave this chat window up for a little while if you have any more q's.
 
Thank you! Sorry, I got a phone call.
 
1:23 AM
I have another question, if you're still here.
Nevermind, I've got it!
 
Still here.
 

last day (14 days later) »