View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.auth;
29  
30  
31  import java.nio.ByteBuffer;
32  
33  
34  /**
35   * Simple debugging utility class for CredSSP and NTLM implementations.
36   */
37  class DebugUtil
38  {
39  
40      public static String dump( final ByteBuffer buf )
41      {
42          final ByteBuffer dup = buf.duplicate();
43          final StringBuilder sb = new StringBuilder( dup.toString() );
44          sb.append( ": " );
45          while ( dup.position() < dup.limit() )
46          {
47              sb.append( String.format( "%02X ", dup.get() ) );
48          }
49          return sb.toString();
50      }
51  
52  
53      public static void dump( final StringBuilder sb, final byte[] bytes )
54      {
55          if ( bytes == null )
56          {
57              sb.append( "null" );
58              return;
59          }
60          for ( final byte b : bytes )
61          {
62              sb.append( String.format( "%02X ", b ) );
63          }
64      }
65  
66  
67      public static String dump( final byte[] bytes )
68      {
69          final StringBuilder sb = new StringBuilder();
70          dump( sb, bytes );
71          return sb.toString();
72      }
73  
74  
75      public static byte[] fromHex( final String hex )
76      {
77          int i = 0;
78          final byte[] bytes = new byte[200000];
79          int h = 0;
80          while ( h < hex.length() )
81          {
82              if ( hex.charAt( h ) == ' ' )
83              {
84                  h++;
85              }
86              final String str = hex.substring( h, h + 2 );
87              bytes[i] = ( byte ) Integer.parseInt( str, 16 );
88              i++;
89              h = h + 2;
90          }
91          final byte[] outbytes = new byte[i];
92          System.arraycopy( bytes, 0, outbytes, 0, i );
93          return outbytes;
94      }
95  
96  }