【问题标题】:Java, Search for a long in a binary file input, 8 byte aligned, big endianJava,在二进制文件输入中搜索long,8字节对齐,大端
【发布时间】:2013-06-03 17:26:50
【问题描述】:
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 中搜索长类型吗? (我不确定我是否需要“读取”作为参数。我对此表示怀疑,我可能会删除它)。如何搜索 bufferedInputStream?大端,8 字节对齐。

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

【问题讨论】:

    标签: java search long-integer endianness


    【解决方案1】:

    使用java.io.DataInputStream.readLong() 方法可以每 8 个字节读取 8 个字节的数据。但问题是:文件只包含long还是其他数据?

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

    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();
       ...
    }
    

    【讨论】:

    • 我正在搜索的初始标记包含值 1234567890。一旦找到该值,我想将 2 个字节的值放入变量中。这 2 个字节位于标记之后 11 个字节。
    • 现在如果在 retval 之后还有 1000 个字节怎么办。我们如何使用单独的方法将这 1000 个字节放入变量中?
    • 我想我的问题是,我如何从我离开的地方继续“dis”?我的想法是,我可能必须从这种方法中删除“dis”并将其放在我的主要功能中。然后调用一个单独的 'retVal' 函数和一个单独的 'thousandBytes' 函数?这可能吗?
    • eof 会发生什么?如果没有留下任何标记,它不会卡在while循环中吗?没关系,这就是为什么它是文件结尾的尝试。
    猜你喜欢
    • 1970-01-01
    • 2010-12-03
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多