19777

When reading a binary file with java, how? [closed]

Question:

If you are wanting to find certain hex values from a file you are reading in with a buffer and eventually parse a piece of code to an output file, what is the best way of doing this? I'm not looking for example code.

It is a very large file with multiple parses required. Should I read all of it into an array? What kind of array? It would depend on what would be best for searching for the hex values I'm looking for and I'm not sure how you would search for a hex value in java.

How would you do it?

EDIT: Full sequence of events.

"Sorry yes I guess that would be confusing. Full explanation. What will happen is I search along for the marker byte pattern. Find it. Then I will deal with the next 1035 Bytes. The 12th and 13th byte is a value I need for a check as well as the 1034th and 1035th byte for another check. Byte 14 is the start of the 1016 bytes I need to parse to the file.

The sequence of events should be: Get first check, check. Get second check, check. If these checks fail write a certain constant byte value to the output file (as a flag). If they are good, write the 1016 bytes to the output file."

Answer1:

Multiple parses required? at the same time? Not sure what that requirement means.

I would probably start simple. Convert the hex search string to the array of bytes I am looking for. Use a FileInputStream to read bytes from the file a few at a time, and keep a running total of how many of the bytes match the search bytes at the current file byte.

bStream = new BufferedInputStream(new FileInputStream(file)); byte[] bytes = new byte[4096]; int read; while ((read = bStream.read(bytes)) > 0) { // do these new bytes match the pattern I'm looking for // accounting for boundaries, etc }

Answer2:

Memory mapped IO, using a File Channel.... <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html#map%28java.nio.channels.FileChannel.MapMode,%20long,%20long%29" rel="nofollow">FileChannel.map(...)</a>

Recommend

  • Nuget crash in visual studio 2015
  • Keras metric produces unexpected values
  • check the difference between two arrays of objects in javascript
  • how do you uninstall an xampp installation that refuses to uninstall?
  • conditional plot linetype in ggplot2
  • Two way one to one associations in Ember Data
  • Create a mobile app that just points to URL
  • Is it possible to determine if a Cypher MERGE results in a create or a match?
  • Advertising Identifier for devices lower than iOS 6.0
  • How to resume hash slots of a particular node in redis cluster in case of hard failure?
  • Velocity (VM) template request parameters: Getting GET variables
  • How to use template selector within a ContentPresenter in Windows 8.1
  • Get max bookings count in range
  • Immutability and synchronization in Java
  • How to add ng-app after page load?
  • async GET request with body from browser
  • Javascript / jQuery not executing in IE until the body of the page is moused over
  • Use 2D Text into 3D scenes in JavaFX results in blurry texts
  • Threads and Concurrent Modification Exception working with a list
  • can you use embedded ruby in custom javascript files in rails?
  • SyntaxError: expected expression, got '.'
  • How to search for a method or variable name within android project?
  • In metro, get all inherited classes of an (abstract) class?
  • Django REST framework - HyperlinkedRelatedField with additional parameter
  • Unable to run testNG tests from maven
  • Synchronous Calls with jqGrid?
  • Adding horizontal slider to QTableWidget
  • Why do you need 2 Javascript files for cross-platform Cordova plugin?
  • Debugging php script timeout?
  • How to use Typescript with libraries like Ampersand.js that parse configs to build prototypes
  • Copy and paste data from multiple workbooks to a worksheet in another Workbook
  • java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/SpringDispatcher-ser
  • Creating 2d platforms using JavaScript
  • PHPMailer return to AJAX
  • how do i compare two rows and store the similarities of the two rows in another column
  • how to get the location(lat/lng) on google maps v3 from the location(x,y)
  • Another “Cannot make static reference…” Question
  • matrix multiplication apache pig
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • Call Microservice from another Microservice within Docker