]> source.dussan.org Git - poi.git/commitdiff
obsoleted Codec classes by HexDump
authorAndreas Beeker <kiwiwings@apache.org>
Mon, 5 Oct 2015 00:27:47 +0000 (00:27 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Mon, 5 Oct 2015 00:27:47 +0000 (00:27 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1706741 13f79535-47bb-0310-9956-ffa450edef68

src/examples/src/org/apache/poi/poifs/poibrowser/Codec.java [deleted file]
src/examples/src/org/apache/poi/poifs/poibrowser/DocumentDescriptorRenderer.java
src/examples/src/org/apache/poi/poifs/poibrowser/PropertySetDescriptorRenderer.java
src/java/org/apache/poi/util/HexDump.java

diff --git a/src/examples/src/org/apache/poi/poifs/poibrowser/Codec.java b/src/examples/src/org/apache/poi/poifs/poibrowser/Codec.java
deleted file mode 100644 (file)
index 76ffe3d..0000000
+++ /dev/null
@@ -1,240 +0,0 @@
-/* ====================================================================
-   Licensed to the Apache Software Foundation (ASF) under one or more
-   contributor license agreements.  See the NOTICE file distributed with
-   this work for additional information regarding copyright ownership.
-   The ASF licenses this file to You 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.poi.poifs.poibrowser;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.util.Locale;
-
-import org.apache.poi.hpsf.ClassID;
-
-/**
- * <p>Provides utility methods for encoding and decoding hexadecimal
- * data.</p>
- *
- * @author Rainer Klute (klute@rainer-klute.de) - with portions from Tomcat
- */
-public class Codec
-{
-
-    /**
-     * <p>The nibbles' hexadecimal values. A nibble is a half byte.</p>
-     */
-    protected static final byte hexval[] =
-        {(byte) '0', (byte) '1', (byte) '2', (byte) '3',
-         (byte) '4', (byte) '5', (byte) '6', (byte) '7',
-         (byte) '8', (byte) '9', (byte) 'A', (byte) 'B',
-         (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F'};
-
-
-
-    /**
-     * <p>Converts a string into its hexadecimal notation.</p>
-     */
-    public static String hexEncode(final String s)
-    {
-        return hexEncode(s.getBytes());
-    }
-
-
-
-    /**
-     * <p>Converts a byte array into its hexadecimal notation.</p>
-     */
-    public static String hexEncode(final byte[] s)
-    {
-        return hexEncode(s, 0, s.length);
-    }
-
-
-
-    /**
-     * <p>Converts a part of a byte array into its hexadecimal
-     * notation.</p>
-     */
-    public static String hexEncode(final byte[] s, final int offset,
-                                   final int length)
-    {
-        StringBuffer b = new StringBuffer(length * 2);
-        for (int i = offset; i < offset + length; i++)
-        {
-            int c = s[i];
-            b.append((char) hexval[(c & 0xF0) >> 4]);
-            b.append((char) hexval[(c & 0x0F) >> 0]);
-        }
-        return b.toString();
-    }
-
-
-
-    /**
-     * <p>Converts a single byte into its hexadecimal notation.</p>
-     */
-    public static String hexEncode(final byte b)
-    {
-        StringBuffer sb = new StringBuffer(2);
-        sb.append((char) hexval[(b & 0xF0) >> 4]);
-        sb.append((char) hexval[(b & 0x0F) >> 0]);
-        return sb.toString();
-    }
-
-
-
-    /**
-     * <p>Converts a short value (16-bit) into its hexadecimal
-     * notation.</p>
-     */
-    public static String hexEncode(final short s)
-    {
-        StringBuffer sb = new StringBuffer(4);
-        sb.append((char) hexval[(s & 0xF000) >> 12]);
-        sb.append((char) hexval[(s & 0x0F00) >>  8]);
-        sb.append((char) hexval[(s & 0x00F0) >>  4]);
-        sb.append((char) hexval[(s & 0x000F) >>  0]);
-        return sb.toString();
-    }
-
-
-
-    /**
-     * <p>Converts an int value (32-bit) into its hexadecimal
-     * notation.</p>
-     */
-    public static String hexEncode(final int i)
-    {
-        StringBuffer sb = new StringBuffer(8);
-        sb.append((char) hexval[(i & 0xF0000000) >> 28]);
-        sb.append((char) hexval[(i & 0x0F000000) >> 24]);
-        sb.append((char) hexval[(i & 0x00F00000) >> 20]);
-        sb.append((char) hexval[(i & 0x000F0000) >> 16]);
-        sb.append((char) hexval[(i & 0x0000F000) >> 12]);
-        sb.append((char) hexval[(i & 0x00000F00) >>  8]);
-        sb.append((char) hexval[(i & 0x000000F0) >>  4]);
-        sb.append((char) hexval[(i & 0x0000000F) >>  0]);
-        return sb.toString();
-    }
-
-
-
-    /**
-     * <p>Converts a long value (64-bit) into its hexadecimal
-     * notation.</p>
-     */
-    public static String hexEncode(final long l)
-    {
-        StringBuffer sb = new StringBuffer(16);
-        sb.append(hexEncode((int) (l & 0xFFFFFFFF00000000L) >> 32));
-        sb.append(hexEncode((int) (l & 0x00000000FFFFFFFFL) >>  0));
-        return sb.toString();
-    }
-
-
-
-    /**
-     * <p>Converts a class ID into its hexadecimal notation.</p>
-     */
-    public static String hexEncode(final ClassID classID)
-    {
-        return hexEncode(classID.getBytes());
-    }
-
-
-
-    /**
-     * <p>Decodes the hexadecimal representation of a sequence of
-     * bytes into a byte array. Each character in the string
-     * represents a nibble (half byte) and must be one of the
-     * characters '0'-'9', 'A'-'F' or 'a'-'f'.</p>
-     *
-     * @param s The string to be decoded
-     *
-     * @return The bytes
-     *
-     * @throws IllegalArgumentException if the string does not contain
-     * a valid representation of a byte sequence.
-     */
-    public static byte[] hexDecode(final String s)
-    {
-        final int length = s.length();
-
-        /* The string to be converted must have an even number of
-           characters. */
-        if (length % 2 == 1)
-            throw new IllegalArgumentException
-                ("String has odd length " + length);
-        byte[] b = new byte[length / 2];
-        char[] c = new char[length];
-        s.toUpperCase(Locale.ROOT).getChars(0, length, c, 0);
-        for (int i = 0; i < length; i += 2)
-            b[i/2] = (byte) (decodeNibble(c[i]) << 4 & 0xF0 |
-                             decodeNibble(c[i+1])    & 0x0F);
-        return b;
-    }
-
-
-
-    /**
-     * <p>Decodes a nibble.</p>
-     *
-     * @param c A character in the range '0'-'9' or 'A'-'F'. Lower
-     * case is not supported here.
-     *
-     * @return The decoded nibble in the range 0-15
-     *
-     * @throws IllegalArgumentException if <em>c</em> is not a
-     * permitted character
-     */
-    protected static byte decodeNibble(final char c)
-    {
-        for (byte i = 0; i < hexval.length; i++)
-            if ((byte) c == hexval[i])
-                return i;
-        throw new IllegalArgumentException("\"" + c + "\"" +
-                                           " does not represent a nibble.");
-    }
-
-
-
-    /**
-     * <p>For testing.</p>
-     */
-    public static void main(final String args[])
-        throws IOException
-    {
-        final BufferedReader in =
-            new BufferedReader(new InputStreamReader(System.in));
-        String s;
-        do
-        {
-            s = in.readLine();
-            if (s != null)
-            {
-                String bytes = hexEncode(s);
-                System.out.print("Hex encoded (String): ");
-                System.out.println(bytes);
-                System.out.print("Hex encoded (byte[]): ");
-                System.out.println(hexEncode(s.getBytes()));
-                System.out.print("Re-decoded (byte[]):  ");
-                System.out.println(new String(hexDecode(bytes)));
-            }
-        }
-        while (s != null);
-    }
-
-}
index 599ea7cc1cdbab629f6d904ecbf8d54c984bcb8f..7be745b8f070fd3f4ff62d61a3014f61dd0c9a3b 100644 (file)
@@ -21,6 +21,8 @@ import java.awt.*;
 import javax.swing.*;
 import javax.swing.tree.*;
 
+import org.apache.poi.util.HexDump;
+
 /**
  * <p>{@link TreeCellRenderer} for a {@link DocumentDescriptor}. The
  * renderer is extremly rudimentary since displays only the document's
@@ -34,11 +36,11 @@ public class DocumentDescriptorRenderer extends DefaultTreeCellRenderer
 
     public Component getTreeCellRendererComponent(final JTree tree,
                                                   final Object value,
-                                                  final boolean selected,
+                                                  final boolean selectedCell,
                                                   final boolean expanded,
                                                   final boolean leaf,
                                                   final int row,
-                                                  final boolean hasFocus)
+                                                  final boolean hasCellFocus)
     {
         final DocumentDescriptor d = (DocumentDescriptor)
             ((DefaultMutableTreeNode) value).getUserObject();
@@ -47,8 +49,9 @@ public class DocumentDescriptorRenderer extends DefaultTreeCellRenderer
         text.append(renderAsString(d));
         text.setFont(new Font("Monospaced", Font.PLAIN, 10));
         p.add(text);
-        if (selected)
+        if (selectedCell) {
             Util.invert(text);
+        }
         return p;
     }
 
@@ -58,19 +61,19 @@ public class DocumentDescriptorRenderer extends DefaultTreeCellRenderer
      */
     protected String renderAsString(final DocumentDescriptor d)
     {
-        final StringBuffer b = new StringBuffer();
+        final StringBuilder b = new StringBuilder();
         b.append("Name: ");
         b.append(d.name);
-        b.append(" (");
-        b.append(Codec.hexEncode(d.name));
-        b.append(")  \n");
+        b.append(" ");
+        b.append(HexDump.toHex(d.name));
+        b.append("\n");
 
         b.append("Size: ");
         b.append(d.size);
         b.append(" bytes\n");
 
         b.append("First bytes: ");
-        b.append(Codec.hexEncode(d.bytes));
+        b.append(HexDump.toHex(d.bytes));
 
         return b.toString();
     }
index 297fa908c99db35756a6c59b9408a0f9bae9cf7e..8f81f51665909b64fdb7777965037c06d4b62ab8 100644 (file)
@@ -32,6 +32,7 @@ import org.apache.poi.hpsf.Property;
 import org.apache.poi.hpsf.PropertySet;
 import org.apache.poi.hpsf.Section;
 import org.apache.poi.hpsf.SummaryInformation;
+import org.apache.poi.util.HexDump;
 
 /**
  * <p>Renders a {@link PropertySetDescriptor} by more or less dumping
@@ -59,14 +60,14 @@ public class PropertySetDescriptorRenderer extends DocumentDescriptorRenderer
         text.setBackground(new Color(200, 255, 200));
         text.setFont(new Font("Monospaced", Font.PLAIN, 10));
         text.append(renderAsString(d));
-        text.append("\nByte order: " +
-                    Codec.hexEncode((short) ps.getByteOrder()));
-        text.append("\nFormat: " +
-                    Codec.hexEncode((short) ps.getFormat()));
-        text.append("\nOS version: " +
-                    Codec.hexEncode(ps.getOSVersion()));
-        text.append("\nClass ID: " +
-                    Codec.hexEncode(ps.getClassID()));
+        text.append("\nByte order: ");
+        text.append(HexDump.toHex((short) ps.getByteOrder()));
+        text.append("\nFormat: ");
+        text.append(HexDump.toHex((short) ps.getFormat()));
+        text.append("\nOS version: ");
+        text.append(HexDump.toHex(ps.getOSVersion()));
+        text.append("\nClass ID: ");
+        text.append(HexDump.toHex(ps.getClassID().getBytes()));
         text.append("\nSection count: " + ps.getSectionCount());
         text.append(sectionsToString(ps.getSections()));
         p.add(text);
@@ -132,7 +133,7 @@ public class PropertySetDescriptorRenderer extends DocumentDescriptorRenderer
     {
         final StringBuffer b = new StringBuffer();
         b.append("\n" + name + " Format ID: ");
-        b.append(Codec.hexEncode(s.getFormatID()));
+        b.append(HexDump.toHex(s.getFormatID().getBytes()));
         b.append("\n" + name + " Offset: " + s.getOffset());
         b.append("\n" + name + " Section size: " + s.getSize());
         b.append("\n" + name + " Property count: " + s.getPropertyCount());
@@ -153,17 +154,17 @@ public class PropertySetDescriptorRenderer extends DocumentDescriptorRenderer
             b.append("), Type: ");
             b.append(type);
             b.append(", Value: ");
-            if (value instanceof byte[])
-            {
-                byte[] b2 = (byte[]) value;
-                b.append("0x" + Codec.hexEncode(b2, 0, 4));
+            if (value instanceof byte[]) {
+                byte[] buf = new byte[4];
+                System.arraycopy(value, 0, buf, 0, 4);
+                b.append(HexDump.toHex(buf));
                 b.append(' ');
-                b.append("0x" + Codec.hexEncode(b2, 4, b2.length - 4));
-            }
-            else if (value != null)
+                System.arraycopy(value, ((byte[])value).length - 4, buf, 0, 4);
+            } else if (value != null) {
                 b.append(value.toString());
-            else
+            } else {
                 b.append("null");
+            }
         }
         return b.toString();
     }
index 7eb8c85068415f2887de2acc678fc69f0f21c534..e72a80ede48d3e8581a3f44a0913e9e025b9040d 100644 (file)
@@ -172,7 +172,8 @@ public class HexDump {
         if (Character.isISOControl(charB)) return '.';
         
         switch (charB) {
-        case 0xFF: case 0xDD: // printable, but not compilable with current compiler encoding
+        // printable, but not compilable with current compiler encoding
+        case 0xFF: case 0xDD:
             charB = '.';
             break;
         }
@@ -187,7 +188,7 @@ public class HexDump {
      */
     public static String toHex(final byte[] value)
     {
-        StringBuffer retVal = new StringBuffer();
+        StringBuilder retVal = new StringBuilder();
         retVal.append('[');
         if (value != null && value.length > 0)
         {
@@ -211,7 +212,7 @@ public class HexDump {
      */
     public static String toHex(final short[] value)
     {
-        StringBuffer retVal = new StringBuffer();
+        StringBuilder retVal = new StringBuilder();
         retVal.append('[');
         for(int x = 0; x < value.length; x++)
         {
@@ -261,7 +262,7 @@ public class HexDump {
      * @param value     The value to convert
      * @return          The result right padded with 0
      */
-    public static String toHex(final short value) {
+    public static String toHex(short value) {
         return xpad(value & 0xFFFF, 4, "");
     }
 
@@ -271,7 +272,7 @@ public class HexDump {
      * @param value     The value to convert
      * @return          The result right padded with 0
      */
-    public static String toHex(final byte value) {
+    public static String toHex(byte value) {
         return xpad(value & 0xFF, 2, "");
     }
 
@@ -281,7 +282,7 @@ public class HexDump {
      * @param value     The value to convert
      * @return          The result right padded with 0
      */
-    public static String toHex(final int value) {
+    public static String toHex(int value) {
         return xpad(value & 0xFFFFFFFF, 8, "");
     }
 
@@ -291,10 +292,22 @@ public class HexDump {
      * @param value     The value to convert
      * @return          The result right padded with 0
      */
-    public static String toHex(final long value) {
-        return xpad(value & 0xFFFFFFFF, 16, "");
+    public static String toHex(long value) {
+        return xpad(value, 16, "");
     }
 
+    /**
+     * Converts the string to a string of hex values.
+     *
+     * @param value     The value to convert
+     * @return          The resulted hex string
+     */
+    public static String toHex(String value) {
+        return (value == null || value.length() == 0)
+            ? "[]"
+            : toHex(value.getBytes(LocaleUtil.CHARSET_1252));
+    }
+    
     /**
      * Dumps <code>bytesToDump</code> bytes to an output stream.
      *