jdk/src/share/classes/javax/management/remote/rmi/NoCallStackClassLoader.java
author bpb
Tue, 22 Oct 2013 11:25:01 -0700
changeset 21328 13dbd66f4c09
parent 5506 202f599c92aa
child 23010 6dadb192ad81
permissions -rw-r--r--
7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE 6445180: URLClassLoader does not describe the behavior of several methods with respect to null arguments Summary: Document when a NPE will be thrown by URLClassLoader constructors, newInstance(), findClass(), and getPermissions(). Reviewed-by: alanb, mduigou, chegar, dholmes, jrose
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4167
diff changeset
     2
 * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4167
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4167
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4167
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4167
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4167
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.management.remote.rmi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.ProtectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
    <p>A class loader that only knows how to define a limited number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
    of classes, and load a limited number of other classes through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    delegation to another loader.  It is used to get around a problem
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    with Serialization, in particular as used by RMI (including
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    RMI/IIOP).  The JMX Remote API defines exactly what class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    must be used to deserialize arguments on the server, and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    values on the client.  We communicate this class loader to RMI by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    setting it as the context class loader.  RMI uses the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    class loader to load classes as it deserializes, which is what we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    want.  However, before consulting the context class loader, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    looks up the call stack for a class with a non-null class loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    and uses that if it finds one.  So, in the standalone version of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    javax.management.remote, if the class you're looking for is known
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    to the loader of jmxremote.jar (typically the system class loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    then that loader will load it.  This contradicts the class-loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    semantics required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    <p>We get around the problem by ensuring that the search up the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    call stack will find a non-null class loader that doesn't load any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    classes of interest, namely this one.  So even though this loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    is indeed consulted during deserialization, it never finds the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    class being deserialized.  RMI then proceeds to use the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    class loader, as we require.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    <p>This loader is constructed with the name and byte-code of one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    or more classes that it defines, and a class-loader to which it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    will delegate certain other classes required by that byte-code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    We construct the byte-code somewhat painstakingly, by compiling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    the Java code directly, converting into a string, copying that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    string into the class that needs this loader, and using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    stringToBytes method to convert it into the byte array.  We
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    compile with -g:none because there's not much point in having
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    line-number information and the like in these directly-encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    <p>The referencedClassNames should contain the names of all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    classes that are referenced by the classes defined by this loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    It is not necessary to include standard J2SE classes, however.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    Here, a class is referenced if it is the superclass or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    superinterface of a defined class, or if it is the type of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    field, parameter, or return value.  A class is not referenced if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    it only appears in the throws clause of a method or constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    Of course, referencedClassNames should not contain any classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    that the user might want to deserialize, because the whole point
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    of this loader is that it does not find such classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
class NoCallStackClassLoader extends ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /** Simplified constructor when this loader only defines one class.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public NoCallStackClassLoader(String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                                  byte[] byteCode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                  String[] referencedClassNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                  ClassLoader referencedClassLoader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                                  ProtectionDomain protectionDomain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        this(new String[] {className}, new byte[][] {byteCode},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
             referencedClassNames, referencedClassLoader, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public NoCallStackClassLoader(String[] classNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                                  byte[][] byteCodes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                  String[] referencedClassNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                  ClassLoader referencedClassLoader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                  ProtectionDomain protectionDomain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        super(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        /* Validation. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (classNames == null || classNames.length == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            || byteCodes == null || classNames.length != byteCodes.length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            || referencedClassNames == null || protectionDomain == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        for (int i = 0; i < classNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (classNames[i] == null || byteCodes[i] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        for (int i = 0; i < referencedClassNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (referencedClassNames[i] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this.classNames = classNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        this.byteCodes = byteCodes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        this.referencedClassNames = referencedClassNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        this.referencedClassLoader = referencedClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        this.protectionDomain = protectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /* This method is called at most once per name.  Define the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * if it is one of the classes whose byte code we have, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * delegate the load if it is one of the referenced classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   121
    @Override
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   122
    protected Class<?> findClass(String name) throws ClassNotFoundException {
21328
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 5506
diff changeset
   123
        // Note: classNames is guaranteed by the constructor to be non-null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        for (int i = 0; i < classNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (name.equals(classNames[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                return defineClass(classNames[i], byteCodes[i], 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                   byteCodes[i].length, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        /* If the referencedClassLoader is null, it is the bootstrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
         * class loader, and there's no point in delegating to it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         * because it's already our parent class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (referencedClassLoader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            for (int i = 0; i < referencedClassNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                if (name.equals(referencedClassNames[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    return referencedClassLoader.loadClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        throw new ClassNotFoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private final String[] classNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private final byte[][] byteCodes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private final String[] referencedClassNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private final ClassLoader referencedClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private final ProtectionDomain protectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <p>Construct a <code>byte[]</code> using the characters of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * given <code>String</code>.  Only the low-order byte of each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * character is used.  This method is useful to reduce the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * footprint of classes that include big byte arrays (e.g. the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * byte code of other classes), because a string takes up much
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * less space in a class file than the byte code to initialize a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * <code>byte[]</code> with the same number of bytes.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * <p>We use just one byte per character even though characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * contain two bytes.  The resultant output length is much the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * same: using one byte per character is shorter because it has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * more characters in the optimal 1-127 range but longer because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * it has more zero bytes (which are frequent, and are encoded as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * two bytes in classfile UTF-8).  But one byte per character has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * two key advantages: (1) you can see the string constants, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * is reassuring, (2) you don't need to know whether the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * file length is odd.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * <p>This method differs from {@link String#getBytes()} in that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * it does not use any encoding.  So it is guaranteed that each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * byte of the result is numerically identical (mod 256) to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * corresponding character of the input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public static byte[] stringToBytes(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        final int slen = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        byte[] bytes = new byte[slen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        for (int i = 0; i < slen; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            bytes[i] = (byte) s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
You can use the following Emacs function to convert class files into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
strings to be used by the stringToBytes method above.  Select the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
whole (defun...) with the mouse and type M-x eval-region, or save it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
to a file and do M-x load-file.  Then visit the *.class file and do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
M-x class-string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
;; class-string.el
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
;; visit the *.class file with emacs, then invoke this function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
(defun class-string ()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
  "Construct a Java string whose bytes are the same as the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
buffer.  The resultant string is put in a buffer called *string*,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
possibly with a numeric suffix like <2>.  From there it can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
insert-buffer'd into a Java program."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
  (interactive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
  (let* ((s (buffer-string))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         (slen (length s))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
         (i 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
         (buf (generate-new-buffer "*string*")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    (set-buffer buf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    (insert "\"")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    (while (< i slen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
      (if (> (current-column) 61)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
          (insert "\"+\n\""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
      (let ((c (aref s i)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        (insert (cond
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                 ((> c 126) (format "\\%o" c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                 ((= c ?\") "\\\"")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                 ((= c ?\\) "\\\\")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                 ((< c 33)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                  (let ((nextc (if (< (1+ i) slen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                   (aref s (1+ i))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                 ?\0)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    (cond
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                     ((and (<= nextc ?7) (>= nextc ?0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                      (format "\\%03o" c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                     (t
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                      (format "\\%o" c)))))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                 (t c))))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
      (setq i (1+ i)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    (insert "\"")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    (switch-to-buffer buf)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
4167
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   229
Alternatively, the following class reads a class file and outputs a string
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   230
that can be used by the stringToBytes method above.
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   231
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   232
import java.io.File;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   233
import java.io.FileInputStream;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   234
import java.io.IOException;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   235
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   236
public class BytesToString {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   237
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   238
    public static void main(String[] args) throws IOException {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   239
        File f = new File(args[0]);
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   240
        int len = (int)f.length();
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   241
        byte[] classBytes = new byte[len];
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   242
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   243
        FileInputStream in = new FileInputStream(args[0]);
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   244
        try {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   245
            int pos = 0;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   246
            for (;;) {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   247
                int n = in.read(classBytes, pos, (len-pos));
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   248
                if (n < 0)
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   249
                    throw new RuntimeException("class file changed??");
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   250
                pos += n;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   251
                if (pos >= n)
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   252
                    break;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   253
            }
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   254
        } finally {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   255
            in.close();
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   256
        }
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   257
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   258
        int pos = 0;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   259
        boolean lastWasOctal = false;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   260
        for (int i=0; i<len; i++) {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   261
            int value = classBytes[i];
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   262
            if (value < 0)
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   263
                value += 256;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   264
            String s = null;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   265
            if (value == '\\')
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   266
                s = "\\\\";
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   267
            else if (value == '\"')
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   268
                s = "\\\"";
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   269
            else {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   270
                if ((value >= 32 && value < 127) && ((!lastWasOctal ||
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   271
                    (value < '0' || value > '7')))) {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   272
                    s = Character.toString((char)value);
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   273
                }
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   274
            }
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   275
            if (s == null) {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   276
                s = "\\" + Integer.toString(value, 8);
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   277
                lastWasOctal = true;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   278
            } else {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   279
                lastWasOctal = false;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   280
            }
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   281
            if (pos > 61) {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   282
                System.out.print("\"");
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   283
                if (i<len)
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   284
                    System.out.print("+");
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   285
                System.out.println();
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   286
                pos = 0;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   287
            }
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   288
            if (pos == 0)
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   289
                System.out.print("                \"");
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   290
            System.out.print(s);
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   291
            pos += s.length();
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   292
        }
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   293
        System.out.println("\"");
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   294
    }
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   295
}
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   296
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
*/