跳过 ByteArray 中的字节 Java
Skip bytes in ByteArray Java
我的应用程序读取类似 png 的文件(以字节为单位)并将字节存储到 byteArray 中。
这是我使用的方法:
public static byte[] read(File file) throws IOException {
byte []buffer = new byte[(int) file.length()];
InputStream ios = null;
try {
ios = new FileInputStream(file);
if ( ios.read(buffer) == -1 ) {
throw new IOException("EOF reached while trying to read the whole file");
}
} finally {
try {
if ( ios != null )
ios.close();
} catch ( IOException e) {
}
}
return buffer;
}
之后,我想提取那个 byteArray 的模式。
它遵循 png 文件的模式:
4 字节长度 + 4 字节类型 + 数据(可选)+ CRC 并重复该方案。
我想做一些类似于 do while 的事情:读取长度 + 类型。如果我对该类型不感兴趣,我想跳过该块。
但是我很挣扎,因为我找不到 byteArray[].
的任何 skip 方法
有人知道如何进行吗?
如果您想用一段时间迭代数组,并且需要在给定条件下跳到下一次迭代,您可以使用标签 continue 跳到循环的下一次迭代。
语法如下:
do {
if (condition) {
continue;
}
// more code here that will only run if the condition is false
} while(whatever you use to iterate over your array);
您是否尝试过使用 ByteArrayInputStream http://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html?有跳过方法
我的应用程序读取类似 png 的文件(以字节为单位)并将字节存储到 byteArray 中。 这是我使用的方法:
public static byte[] read(File file) throws IOException {
byte []buffer = new byte[(int) file.length()];
InputStream ios = null;
try {
ios = new FileInputStream(file);
if ( ios.read(buffer) == -1 ) {
throw new IOException("EOF reached while trying to read the whole file");
}
} finally {
try {
if ( ios != null )
ios.close();
} catch ( IOException e) {
}
}
return buffer;
}
之后,我想提取那个 byteArray 的模式。
它遵循 png 文件的模式:
4 字节长度 + 4 字节类型 + 数据(可选)+ CRC 并重复该方案。
我想做一些类似于 do while 的事情:读取长度 + 类型。如果我对该类型不感兴趣,我想跳过该块。 但是我很挣扎,因为我找不到 byteArray[].
的任何 skip 方法有人知道如何进行吗?
如果您想用一段时间迭代数组,并且需要在给定条件下跳到下一次迭代,您可以使用标签 continue 跳到循环的下一次迭代。
语法如下:
do {
if (condition) {
continue;
}
// more code here that will only run if the condition is false
} while(whatever you use to iterate over your array);
您是否尝试过使用 ByteArrayInputStream http://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html?有跳过方法