Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Parsing .dta files with Java

    I've been trying to build out some stuff in Java to read .dta files directly into the JVM (as part of some longer term ideas for data management) and was wondering if anyone else has figured out how to properly create/handle the 6-byte integer values that are used for the storage of strL data? The help file has some examples/advice on how to do all of this in C, but it isn't clear how it would translate into Java.

  • #2
    The solution I found for this was based on the use of the Arrays class. I've copied a snippet from the relevant class below:

    /** * Starting position used to select bytes from the array containing the * [v, o] elements to parse the v element. */ private static final Integer vdstart = 0; /** * Ending position used to select bytes from the array containing the * [v, o] elements to parse the v element. */ private static final Integer vdend = 2; /** * Starting position used to select bytes from the array containing the * [v, o] elements to parse the o element. */ private static final Integer odstart = 2; /** * Ending position used to select bytes from the array containing the * [v, o] elements to parse the o element. */ private static final Integer odend = 8; /** * Member used to store the [v, o] array from the data element in the * .dta file */ private byte[] strlArray = new byte[8]; /** * Member used to store the decimal value of the v element */ private Integer v = null; /** * Member used to store the decimal value of the o element */ private Long o = null; /** * Member containing the byte order to use when interpretting the bytes * from the file */ private ByteOrder bo;
    /** * Method used to parse an Array of bytes into the corresponding [v, o] * elements. <em>Note, this is the preferred method to use when parsing * release version 118 files. This is due to the requirement to handle the * 8-byte sequences as a 2-byte and 6 byte integer.</em> */ @Override public void parseMembers() { // Create a byte array from the first two elements of the strlArray // object and copy it into an Array that is 4 bytes long, then read // the datum and cast to an Integer this.v = ByteBuffer.wrap(Arrays.copyOf(Arrays.copyOfRange(this .strlArray, vdstart, vdend), 4)).order(this.bo).getInt(); // Create a byte array from second through last elements of the // strlArray object and copy it into an Array that is 8 bytes long, // then read the datum and cast to a Long. this.o = ByteBuffer.wrap(Arrays.copyOf(Arrays.copyOfRange(this .strlArray, odstart, odend), 8)).order(this.bo).getLong(); } // End of Method declaration
    All of the source is available on GitHub. The project is still very much a work in progress, but any help/comments/contributions are more than welcome.

    Comment

    Working...
    X