Thursday, August 13, 2015

Friday, June 27, 2014

Sensor Listing App for Android

Here I provide the .apk for an app that will list all of the hardware sensors on your Android device.

SensorListingApp.apk

Thursday, March 6, 2014

Debugging - Dumping binary data to a file in Java

 

Debugging -- Dumping binary data to a file in Java

Sometimes in debugging code you may need to dump byte buffers to output to view the contents.  For example, when writing Java network code that interfaces with Web Services, you have a need to see exactly what it is that you are sending out over the wire.  You can use tools like WireShark or Charles to see what is sent over the network, but if you simply want to dump binary buffers, something easy to use and light-weight might be a better solution.

Here I provide a simple class that will write a byte buffer to a file.  To use it, just copy and paste it into your own Java or Android project.
This class is really basic, without a lot of error checking.   Feel free to update it and beef it up. 


 Usage Example: 

        DumpBytesToFile dbtf = new DumpBytesToFile( path, "outputfile.txt" );
        dbtf.open();
        String r = "This is a string for testing purposes.";
        dbtf.write( r.getBytes() );
        dbtf.close();



 import java.io.BufferedWriter;  
 import java.io.File;  
 import java.io.FileNotFoundException;  
 import java.io.FileWriter;  
 import java.io.IOException;  
   
 public class DumpBytesToFile  
 {  
    private String           mPath         = "";  
    private String           mFilename     = "";  
    private File             mFile         = null;  
    private long             mBufferSize   = 0;  
      
    // Change this if you want more columns on a line  
    private final int        LINESIZE      = 10;   
    private int              mCurrLine     = LINESIZE;  
    private StringBuilder    mLine         = null;  
    private BufferedWriter   mFileWriter   = null;  
      
    public DumpBytesToFile( String path, String filename )  
    {  
       mPath        = path;  
       mFilename    = filename;  
    }  
      
    public void open()  
    {  
       // Open file and initialize variables  
       mFile = new File( mPath, mFilename );  
       try  
       {  
          mFile.createNewFile();  
          mFileWriter       = new BufferedWriter( new FileWriter(mFile) );  
          mBufferSize       = 0;  
          mCurrLine         = LINESIZE;  
          mLine             = new StringBuilder( ".........." );  
       }   
         
       catch (FileNotFoundException e)  
       {  
          e.printStackTrace();  
       }   
       catch (IOException e)  
       {  
          e.printStackTrace();  
       }  
    }  
      
    public void write( byte [] buffer, int len )  
    {  
       // Write buffer of given length to open file  
       if (len>0)  
       {  
          mBufferSize += len;  
          try  
          {     
             for ( int i = 0; i<len; ++i )  
             {  
                // Convert each byte to Hex representation  
                mFileWriter.write( String.format( "%02X ", buffer[i] ) );
                // Convert each byte to char so we can check for '\n', which we don't want write  
                char c = (char) ( buffer[i] & 0xFF );  
                if ( ( buffer[i] & 0xFF ) != 0x0A )  
                   mLine.setCharAt( LINESIZE-mCurrLine, c );  
                else  
                   mLine.setCharAt( LINESIZE-mCurrLine, '.' );
                // Check for end of line  
                if ( (mCurrLine--) == 1 )  
                {  
                   mFileWriter.write( mLine.toString() );  
                   mFileWriter.write( "\n" );  
                   mCurrLine = LINESIZE;  
                };  
             }  
          }   
          catch (IOException e)  
          {  
             e.printStackTrace();  
          }  
       }  
    }  
      
    public void write( byte [] buffer )  
    {  
       // Write buffer to open file  
       write( buffer, buffer.length );  
    }  
      
    public void close()  
    {  
       // Close open file after writing  
       try  
       {  
          mFileWriter.write( "\n" );  
          mFileWriter.write( "Byte Length: " + mBufferSize );  
          mFileWriter.close();  
          mFileWriter.flush();  
       }   
       catch (IOException e)  
       {  
          e.printStackTrace();  
       }  
    }  
 }