Author: akarasulu Date: Sat Nov 27 01:44:01 2004 New Revision: 106701 URL: http://svn.apache.org/viewcvs?view=rev&rev=106701 Log: some tools that came in handy Added: incubator/directory/seda/trunk/src/java/org/apache/seda/HexDump.java incubator/directory/seda/trunk/src/test/org/apache/seda/HexDumpTest.java
Added: incubator/directory/seda/trunk/src/java/org/apache/seda/HexDump.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/trunk/src/java/org/apache/seda/HexDump.java?view=auto&rev=106701 ============================================================================== --- (empty file) +++ incubator/directory/seda/trunk/src/java/org/apache/seda/HexDump.java Sat Nov 27 01:44:01 2004 @@ -0,0 +1,113 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.seda; + + +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.io.FileOutputStream; +import java.io.File; +import java.io.IOException; + + +/** + * Document this class. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Apache Directory Project</a> + * @version $Rev$ + */ +public class HexDump +{ + private final static char[] hexChars = { + '0', '1', '2', '3', + '4', '5', '6', '7', + '8', '9', 'a', 'b', + 'c', 'd', 'e', 'f' + }; + + + public static final StringBuffer dump( ByteBuffer[] buffers ) + { + StringBuffer buf = new StringBuffer(); + + int byteCount = 0; + for ( int ii = 0; ii < buffers.length; ii++ ) + { + ByteBuffer dup = buffers[ii].duplicate(); + while ( dup.hasRemaining() ) + { + if ( byteCount % 8 == 0 ) + { + buf.append( " " ); + } + + if ( byteCount % 16 == 0 ) + { + buf.append( '\n' ); + + int address = byteCount/16 + 10000000; + String addressStr = String.valueOf( address ).substring(1) + "0: "; + buf.append( addressStr ); + } + + byte bite = dup.get(); + buf.append( hexChars[ ( bite & 0xF0 ) >> 4 ] ); + buf.append( hexChars[ bite & 0x0F ]); + buf.append( ' ' ); + byteCount++; + } + } + + return buf; + } + + + public static void dump( String filename, ByteBuffer[] buffers ) throws IOException + { + File f = new File( filename ); + f.createNewFile(); + FileOutputStream outStream = new FileOutputStream( f ); + FileChannel out = outStream.getChannel(); + + ByteBuffer[] dups = new ByteBuffer[ buffers.length ]; + for ( int ii = 0; ii < buffers.length; ii++ ) + { + dups[ii] = buffers[ii].duplicate(); + } + + while ( hasRemaining( dups ) ) + { + out.write( dups ); + } + + outStream.close(); + } + + + public static boolean hasRemaining( ByteBuffer[] buffers ) + { + for ( int ii = 0; ii < buffers.length; ii++ ) + { + if ( buffers[ii].hasRemaining() ) + { + return true; + } + } + + return false; + } +} Added: incubator/directory/seda/trunk/src/test/org/apache/seda/HexDumpTest.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/trunk/src/test/org/apache/seda/HexDumpTest.java?view=auto&rev=106701 ============================================================================== --- (empty file) +++ incubator/directory/seda/trunk/src/test/org/apache/seda/HexDumpTest.java Sat Nov 27 01:44:01 2004 @@ -0,0 +1,41 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.seda; + + +import java.nio.ByteBuffer; + +import junit.framework.TestCase; + + +/** + * Document this class. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Apache Directory Project</a> + * @version $Rev$ + */ +public class HexDumpTest extends TestCase +{ + public void testDump() + { + ByteBuffer[] buffers = new ByteBuffer[] { + ByteBuffer.wrap( "Hello World this is the hex dump tool".getBytes() ) + }; + + System.err.println( HexDump.dump( buffers ) ); + } +}
