View Javadoc

1   package org.apache.maven.surefire.util.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.surefire.util.NestedRuntimeException;
23  
24  import java.io.IOException;
25  import java.io.OutputStream;
26  
27  public class StreamUtils
28  {
29      public static void toHex( OutputStream target, Integer i )
30      {
31          if ( i != null )
32          {
33              toHex( target, i.intValue() );
34          }
35      }
36  
37      /**
38       * Convert the integer to an unsigned number.
39       *
40       * @param target The stream that will receive the encoded value
41       * @param i      the value
42       */
43      public static void toHex( OutputStream target, int i )
44      {
45          byte[] buf = new byte[32];
46          int charPos = 32;
47          int radix = 1 << 4;
48          int mask = radix - 1;
49          do
50          {
51              buf[--charPos] = (byte) digits[i & mask];
52              i >>>= 4;
53          }
54          while ( i != 0 );
55  
56          try
57          {
58              target.write( buf, charPos, ( 32 - charPos ) );
59          } catch (IOException e)
60          {
61              throw new NestedRuntimeException(e);
62          }
63      }
64  
65      private final static char[] digits =
66          { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
67              'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
68  
69  
70  }