Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By dvreed77

Thread: Converting C File reader to Java

  1. #1
    dvreed77 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default Converting C File reader to Java

    Hi,

    I am in need of some urgent help. I am attempting to implement a legacy C .dat file reader in Java. I would like to convert the following code to Java, but am having some trouble doing so.

    Java Code:
    long ** inp;
    
    while (1) {
       int i, j, k;  
          for (i = 0 ; i < 64 ; i++) { 	
             input_file.read(reinterpret_cast<char *>(inp[i]), sizeof(long) * 2048);
            }  		
       if (input_file.eof())
          break;
    }
    I was successful in using a DataInputStream and reading on long at a time, but this was very very slow, it took 12 seconds to read in a 54MB file.

    I also tried using a filechannel, and then converting the bytebuffer to a longbuffer like so.

    Java Code:
    int bytesRead = fileChannel.read(buffer);
    
    long[][] longData = new long[64][2048];
    
    while (bytesRead != -1) {
        for (ii = 0; ii < 64; ii++) {
    
                   ((ByteBuffer) (buffer.flip())).asLongBuffer().
                           get(longData[ii]);
                   bytesRead = fileChannel.read(buffer);
                   buffer.clear();
        }
    }
    But I get errors like java.nio.BufferUnderflowException, which I can't seem to find any help on.

    I would really appreciate any help I can get. Thanks a lot for the help.

    -Dave

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,435
    Rep Power
    25

    Default Re: Converting C File reader to Java

    I get errors like java.nio.BufferUnderflowException
    Which line does the error occur on?

    Do you have a reason for using the nio package classes?
    Have you tried using the io class?

    NOTE: I need to start using nio classes, but have not gotten around to it yet.

  3. #3
    dvreed77 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default Re: Converting C File reader to Java

    SOLVED:

    I was able to get it working by doing something like this:

    Java Code:
    FileInputStream instream = new FileInputStream(inputFile);
    
    
            byte[] bArray = new byte[numSamplesPerRead * 4];
            int[][] intArray = new int[64][numSamplesPerRead];
            ByteBuffer testBuffer = ByteBuffer.wrap(bArray);
            testBuffer.order(ByteOrder.LITTLE_ENDIAN);
    
            
    
    while (instream.available() > 0) {
               
                for (int ii = 0; ii < 162; ii++) {
                    
                    instream.read(bArray);
                    // Wrap a ByteBuffer around these bytes
                    testBuffer.wrap(bArray);
                    // Create an IntBuffer from this ByteBuffer
                    IntBuffer intB1 = testBuffer.asIntBuffer();
                    // Create an int array from this IntBuffer    
                    intB1.get(intArray[ii]);
    
                }            
            }
    Some of the issues involved were that longs in C are 32 bytes which is an int in Java, and that the byte order for the file was Little-Endian.In any case this feels like a pretty elegant solution and the read time is about a quarter of a second.
    DarrylBurke likes this.

Similar Threads

  1. Trouble with Java File Reader
    By name in forum New To Java
    Replies: 3
    Last Post: 12-17-2011, 12:25 AM
  2. java file reader, jgrasp can't find the file
    By aramiky818 in forum New To Java
    Replies: 3
    Last Post: 04-22-2011, 02:06 AM
  3. converting java class file to exe file
    By satheeshtech in forum Advanced Java
    Replies: 5
    Last Post: 07-18-2009, 11:47 PM
  4. Java file reader...?
    By prabhurangan in forum New To Java
    Replies: 3
    Last Post: 11-21-2008, 08:19 AM
  5. Converting text file(.txt) to JPG file(.jpg) in java
    By javadeveloper in forum Advanced Java
    Replies: 0
    Last Post: 11-09-2007, 04:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •