If you really don't want to use the convenience of
DataInputStream.readInt
,
then here is how you can check the first 4 bytes in Java:
InputStream stream = new FileInputStream("example.jpg");
byte[] bytes = new byte[4];
stream.read(bytes);
byte[] expectedBytes = { (byte) 0xFF, (byte) 0xD8, (byte) 0xFF, (byte) 0xE0 };
System.out.println(Arrays.equals(expectedBytes, bytes));
and the same in Scala:
val stream = new FileInputStream("example.jpg")
val bytes = Array.ofDim[Byte](4)
stream.read(bytes)
val expectedBytes = Array(0xFF.toByte, 0xD8.toByte, 0xFF.toByte, 0xE0.toByte)
println(Arrays.==(expectedBytes, bytes))