Java,在二进制文件输入中搜索long,8字节对齐,big endian [英] Java, Search for a long in a binary file input, 8 byte aligned, big endian

查看:183
本文介绍了Java,在二进制文件输入中搜索long,8字节对齐,big endian的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static void main(String[] args) {
   File inFile = null;
   if (0 < args.length) {
      inFile = new File(args[0]);
   }
   BufferedInputStream bStream = null;
   try {
      int read;
      bStream = new BufferedInputStream(new FileInputStream(inFile));
      while ((read = bStream.read()) > 0) {
         getMarker(read, bStream);
         System.out.println(read);
      }
   }
   catch (IOException e) {
      e.printStackTrace();
   }
   finally {
      try {
         if (bStream != null)bStream.close();
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

private static void getMarker(int read, BufferedInputStream bStream) {
}

我想在bufferedInputStream中找到长1234567890.我可以在bufferedInputStream中搜索长类型吗? (我不确定是否需要将"read"作为参数.我对此表示怀疑,可以将其删除).如何搜索bufferedInputStream?大字节序,对齐8字节.

I want to find the long 1234567890 in the bufferedInputStream. Am I able to search the bufferedInputStream for a long type? (I'm not sure whether I need 'read' as a parameter. I doubt it, I may remove that). How do I search a bufferedInputStream? Big endian, 8 byte aligned.

我要搜索的初始标记包含值1234567890.找到该值后,我想将2个字节的值放入变量中.这2个字节位于标记之后的11个字节处.

The initial marker that I'm searching for contains the value 1234567890. Once I have found that value I want to put the value of 2 bytes into a variable. These 2 bytes are located 11 bytes after marker.

推荐答案

使用方法

With the method java.io.DataInputStream.readLong() it's possible to read data 8 bytes per 8 bytes. But the question is: the file contains only long or other data?

如果数据可能在任何地方,我们必须读取文件的8倍,从偏移量0、1、2等开始.

If the data may be anywhere, we have to read 8 times the file beginning at the offset 0, 1, 2 and so on.

class FuzzyReaderHelper {

   public static final long MAGIC_NUMBER = 1234567890L;

   public static DataInputStream getStream( File source ) {
      boolean magicNumberFound = false;
      for( int offset = 0; !magicNumberFound && offset < 8; ++offset ) {
         dis = new DataInputStream( new FileInputStream( source ));
         for( int i = 0; i < offset; ++i ) {
            dis.read();
         }
         try {
            long l;
            while(( l = dis.readLong()) != MAGIC_NUMBER ) {
               /* Nothing to do... */
            }
            magicNumberFound = true;
            for( int i = 0; i < 11; ++i ) {
               dis.read();
            }
            return dis;
         }
         catch( EOFException eof ){}
         dis.close();
      }
   // choose:
      throw new IllegalStateException( "Incompatible file: " + source );
   // or
      return null; 
   }
}

下一步取决于您:

DataInputStream dis = FuzzyReaderHelper.getStream( new File( root, "toto.dat" ));
if( dis != null ) {
   byte[] bytes = new byte[2];
   bytes[0] = dis.read();
   bytes[1] = dis.read();
   ...
}

这篇关于Java,在二进制文件输入中搜索long,8字节对齐,big endian的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆