Home | History | Annotate | Download | only in utils
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 /**
     19 * @author Vladimir N. Molotkov
     20 * @version $Revision$
     21 */
     22 
     23 package org.apache.harmony.security.utils;
     24 
     25 
     26 /**
     27  * Utility class for arrays
     28  *
     29  */
     30 public class Array {
     31 
     32     // No instances of this class
     33     private Array() {
     34     }
     35 
     36     public static String getBytesAsString(byte[] data) {
     37         StringBuilder result = new StringBuilder(data.length * 3);
     38         for (int i = 0; i < data.length; ++i) {
     39             result.append(Byte.toHexString(data[i], false));
     40             result.append(' ');
     41         }
     42         return result.toString();
     43     }
     44 
     45     /**
     46      * Represents <code>array</code> as <code>String</code>
     47      * for printing. Array length can be up to 32767
     48      *
     49      * @param array to be represented as <code>String</code>
     50      *
     51      * @return <code>String</code> representation of the <code>array</code>
     52      */
     53     public static String toString(byte[] array, String prefix) {
     54         // Prefixes to be added to the offset values
     55         // in <code>String toString(byte[], String)</code> method
     56         final String[] offsetPrefix = {
     57                 "",
     58                 "000",
     59                 "00",
     60                 "0",
     61                 ""
     62         };
     63         StringBuilder sb = new StringBuilder();
     64         StringBuilder charForm = new StringBuilder();
     65         int i=0;
     66         for (i=0; i<array.length; i++) {
     67             if (i%16 == 0) {
     68                 sb.append(prefix);
     69                 // put offset
     70                 String offset = Integer.toHexString(i);
     71                 sb.append(offsetPrefix[offset.length()]);
     72                 sb.append(offset);
     73                 // clear char form for new line
     74                 charForm.delete(0, charForm.length());
     75             }
     76             // put delimiter
     77             sb.append(' ');
     78             // put current byte
     79             sb.append(Byte.toHexString(array[i], false));
     80             // form character representation part
     81             int currentByte = (0xff & array[i]);
     82             char currentChar = (char)(currentByte & 0xffff);
     83             // FIXME if needed (how to distinguish PRINTABLE chars?)
     84             charForm.append(
     85                     (Character.isISOControl(currentChar) ? '.' : currentChar));
     86             // Add additional delimiter for each 8 values
     87             if ((i+1)%8 == 0) {
     88                 sb.append(' ');
     89             }
     90             // Add character representation for each line
     91             if ((i+1)%16 == 0) {
     92                 sb.append(' ');
     93                 sb.append(charForm.toString());
     94                 sb.append('\n');
     95             }
     96         }
     97         // form last line
     98         if (i%16 != 0) {
     99             int ws2add = 16 - i%16;
    100             for (int j=0; j<ws2add; j++) {
    101                 sb.append("   ");
    102             }
    103             if (ws2add > 8) {
    104                 sb.append(' ');
    105             }
    106             sb.append("  ");
    107             sb.append(charForm.toString());
    108             sb.append('\n');
    109         }
    110         return sb.toString();
    111     }
    112 }
    113