001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Vladimir N. Molotkov
020: * @version $Revision$
021: */package org.apache.harmony.security.utils;
022:
023: /**
024: * Utility class for arrays
025: *
026: */
027: public class Array {
028:
029: // No instances of this class
030: private Array() {
031: }
032:
033: /**
034: * Represents <code>array</code> as <code>String</code>
035: * for printing. Array length can be up to 32767
036: *
037: * @param array to be represented as <code>String</code>
038: *
039: * @return <code>String</code> representation of the <code>array</code>
040: */
041: public static String toString(byte[] array, String prefix) {
042: // Prefixes to be added to the offset values
043: // in <code>String toString(byte[], String)</code> method
044: final String[] offsetPrefix = { "", //$NON-NLS-1$
045: "000", //$NON-NLS-1$
046: "00", //$NON-NLS-1$
047: "0", //$NON-NLS-1$
048: "" //$NON-NLS-1$
049: };
050: StringBuilder sb = new StringBuilder();
051: StringBuilder charForm = new StringBuilder();
052: int i = 0;
053: for (i = 0; i < array.length; i++) {
054: if (i % 16 == 0) {
055: sb.append(prefix);
056: // put offset
057: String offset = Integer.toHexString(i);
058: sb.append(offsetPrefix[offset.length()]);
059: sb.append(offset);
060: // clear char form for new line
061: charForm.delete(0, charForm.length());
062: }
063: // put delimiter
064: sb.append(' ');
065: // put current byte
066: int currentByte = (0xff & array[i]);
067: String hexTail = Integer.toHexString(currentByte);
068: if (hexTail.length() == 1) {
069: sb.append('0');
070: }
071: sb.append(hexTail);
072: // form character representation part
073: char currentChar = (char) (currentByte & 0xffff);
074: // FIXME if needed (how to distinguish PRINTABLE chars?)
075: charForm.append((Character.isISOControl(currentChar) ? '.'
076: : currentChar));
077: // Add additional delimiter for each 8 values
078: if ((i + 1) % 8 == 0) {
079: sb.append(' ');
080: }
081: // Add character representation for each line
082: if ((i + 1) % 16 == 0) {
083: sb.append(' ');
084: sb.append(charForm.toString());
085: sb.append('\n');
086: }
087: }
088: // form last line
089: if (i % 16 != 0) {
090: int ws2add = 16 - i % 16;
091: for (int j = 0; j < ws2add; j++) {
092: sb.append(" "); //$NON-NLS-1$
093: }
094: if (ws2add > 8) {
095: sb.append(' ');
096: }
097: sb.append(" "); //$NON-NLS-1$
098: sb.append(charForm.toString());
099: sb.append('\n');
100: }
101: return sb.toString();
102: }
103: }
|