简体   繁体   English

在ByteArray Java中跳过字节

[英]Skip bytes in ByteArray Java

My app reads a png-like file (in byte) and stores bytes into a byteArray. 我的应用程序读取一个类似png的文件(以字节为单位),并将字节存储到byteArray中。 This is the method I use : 这是我使用的方法:

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;
    }

After that, I want to extract patterns of that byteArray. 之后,我想提取该byteArray的模式。

It follows the pattern of png file : 它遵循png文件的模式:
4 bytes length + 4 bytes types + data (optional) + CRC and repeats that scheme. 4字节长+ 4字节类型+数据(可选)+ CRC并重复该方案。

I want to do something like a do while : reading the length + type. 我想做类似的事情:读取长度和类型。 If I'm not interested of the type, I want to skip that chunk. 如果我对这种类型不感兴趣,我想跳过该块。 But I'm struggling because I can't find any skip method for byteArray[]. 但是我很努力,因为我找不到byteArray []的任何跳过方法

Does anyone have an idea of how to proceed ? 有谁知道如何进行?

Have you tried to use ByteArrayInputStream http://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html ? 您是否尝试过使用ByteArrayInputStream http://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html there is skip method 有跳过方法

If you want to iterate through the array with a while and you need to skip to the next iteration on a given condition you can use the label continue to skip to the next iteration of the loop. 如果要在一段时间内迭代数组,并且需要在给定条件下跳到下一个迭代,则可以使用标签继续跳到循环的下一个迭代。

The syntax is as follows: 语法如下:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM