src/java.management.rmi/share/classes/javax/management/remote/rmi/NoCallStackClassLoader.java
author herrick
Mon, 30 Sep 2019 19:33:13 -0400
branchJDK-8200758-branch
changeset 58417 67ffaf3a2b75
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231280: Linux packages produced by jpackage should have correct dependencies Submitted-by: asemenyuk Reviewed-by: herrick
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
32639
339de1317e84 8043937: Drop support for the IIOP transport from the JMX RMIConnector
jbachorik
parents: 25859
diff changeset
     2
 * Copyright (c) 2003, 2015, 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
32639
339de1317e84 8043937: Drop support for the IIOP transport from the JMX RMIConnector
jbachorik
parents: 25859
diff changeset
    34
    with Serialization, in particular as used by RMI. The JMX Remote API
339de1317e84 8043937: Drop support for the IIOP transport from the JMX RMIConnector
jbachorik
parents: 25859
diff changeset
    35
    defines exactly what class loader must be used to deserialize arguments on
339de1317e84 8043937: Drop support for the IIOP transport from the JMX RMIConnector
jbachorik
parents: 25859
diff changeset
    36
    the server, and return values on the client.  We communicate this class
339de1317e84 8043937: Drop support for the IIOP transport from the JMX RMIConnector
jbachorik
parents: 25859
diff changeset
    37
    loader to RMI by setting it as the context class loader.  RMI uses the
339de1317e84 8043937: Drop support for the IIOP transport from the JMX RMIConnector
jbachorik
parents: 25859
diff changeset
    38
    context class loader to load classes as it deserializes, which is what we
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    want.  However, before consulting the context class loader, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    looks up the call stack for a class with a non-null class loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    and uses that if it finds one.  So, in the standalone version of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    javax.management.remote, if the class you're looking for is known
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    to the loader of jmxremote.jar (typically the system class loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    then that loader will load it.  This contradicts the class-loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    semantics required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    <p>We get around the problem by ensuring that the search up the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    call stack will find a non-null class loader that doesn't load any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    classes of interest, namely this one.  So even though this loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    is indeed consulted during deserialization, it never finds the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    class being deserialized.  RMI then proceeds to use the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    class loader, as we require.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    <p>This loader is constructed with the name and byte-code of one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    or more classes that it defines, and a class-loader to which it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    will delegate certain other classes required by that byte-code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    We construct the byte-code somewhat painstakingly, by compiling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    the Java code directly, converting into a string, copying that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    string into the class that needs this loader, and using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    stringToBytes method to convert it into the byte array.  We
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    compile with -g:none because there's not much point in having
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    line-number information and the like in these directly-encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    <p>The referencedClassNames should contain the names of all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    classes that are referenced by the classes defined by this loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    It is not necessary to include standard J2SE classes, however.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    Here, a class is referenced if it is the superclass or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    superinterface of a defined class, or if it is the type of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    field, parameter, or return value.  A class is not referenced if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    it only appears in the throws clause of a method or constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    Of course, referencedClassNames should not contain any classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    that the user might want to deserialize, because the whole point
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    of this loader is that it does not find such classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
class NoCallStackClassLoader extends ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /** Simplified constructor when this loader only defines one class.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public NoCallStackClassLoader(String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                  byte[] byteCode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                                  String[] referencedClassNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                  ClassLoader referencedClassLoader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                  ProtectionDomain protectionDomain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this(new String[] {className}, new byte[][] {byteCode},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
             referencedClassNames, referencedClassLoader, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public NoCallStackClassLoader(String[] classNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                                  byte[][] byteCodes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                                  String[] referencedClassNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                  ClassLoader referencedClassLoader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                  ProtectionDomain protectionDomain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        super(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        /* Validation. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (classNames == null || classNames.length == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            || byteCodes == null || classNames.length != byteCodes.length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            || referencedClassNames == null || protectionDomain == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        for (int i = 0; i < classNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (classNames[i] == null || byteCodes[i] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        for (int i = 0; i < referencedClassNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            if (referencedClassNames[i] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        this.classNames = classNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this.byteCodes = byteCodes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        this.referencedClassNames = referencedClassNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        this.referencedClassLoader = referencedClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        this.protectionDomain = protectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /* This method is called at most once per name.  Define the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * if it is one of the classes whose byte code we have, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * delegate the load if it is one of the referenced classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   120
    @Override
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   121
    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
   122
        // Note: classNames is guaranteed by the constructor to be non-null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        for (int i = 0; i < classNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (name.equals(classNames[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                return defineClass(classNames[i], byteCodes[i], 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                   byteCodes[i].length, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        /* If the referencedClassLoader is null, it is the bootstrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
         * class loader, and there's no point in delegating to it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
         * because it's already our parent class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (referencedClassLoader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            for (int i = 0; i < referencedClassNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                if (name.equals(referencedClassNames[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    return referencedClassLoader.loadClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        throw new ClassNotFoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private final String[] classNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private final byte[][] byteCodes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private final String[] referencedClassNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private final ClassLoader referencedClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private final ProtectionDomain protectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * <p>Construct a <code>byte[]</code> using the characters of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * given <code>String</code>.  Only the low-order byte of each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * character is used.  This method is useful to reduce the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * footprint of classes that include big byte arrays (e.g. the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * byte code of other classes), because a string takes up much
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * less space in a class file than the byte code to initialize a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <code>byte[]</code> with the same number of bytes.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <p>We use just one byte per character even though characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * contain two bytes.  The resultant output length is much the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * same: using one byte per character is shorter because it has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * more characters in the optimal 1-127 range but longer because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * it has more zero bytes (which are frequent, and are encoded as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * two bytes in classfile UTF-8).  But one byte per character has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * two key advantages: (1) you can see the string constants, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * is reassuring, (2) you don't need to know whether the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * file length is odd.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <p>This method differs from {@link String#getBytes()} in that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * it does not use any encoding.  So it is guaranteed that each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * byte of the result is numerically identical (mod 256) to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * corresponding character of the input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public static byte[] stringToBytes(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        final int slen = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        byte[] bytes = new byte[slen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        for (int i = 0; i < slen; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            bytes[i] = (byte) s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
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
You can use the following Emacs function to convert class files into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
strings to be used by the stringToBytes method above.  Select the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
whole (defun...) with the mouse and type M-x eval-region, or save it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
to a file and do M-x load-file.  Then visit the *.class file and do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
M-x class-string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
;; class-string.el
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
;; visit the *.class file with emacs, then invoke this function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
(defun class-string ()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
  "Construct a Java string whose bytes are the same as the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
buffer.  The resultant string is put in a buffer called *string*,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
possibly with a numeric suffix like <2>.  From there it can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
insert-buffer'd into a Java program."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
  (interactive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
  (let* ((s (buffer-string))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
         (slen (length s))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         (i 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
         (buf (generate-new-buffer "*string*")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    (set-buffer buf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    (insert "\"")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    (while (< i slen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
      (if (> (current-column) 61)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
          (insert "\"+\n\""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
      (let ((c (aref s i)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        (insert (cond
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                 ((> c 126) (format "\\%o" c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                 ((= c ?\") "\\\"")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                 ((= c ?\\) "\\\\")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                 ((< c 33)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                  (let ((nextc (if (< (1+ i) slen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                   (aref s (1+ i))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                 ?\0)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    (cond
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                     ((and (<= nextc ?7) (>= nextc ?0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                      (format "\\%03o" c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                     (t
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                      (format "\\%o" c)))))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                 (t c))))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
      (setq i (1+ i)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    (insert "\"")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    (switch-to-buffer buf)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
4167
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   228
Alternatively, the following class reads a class file and outputs a string
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   229
that can be used by the stringToBytes method above.
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   230
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   231
import java.io.File;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   232
import java.io.FileInputStream;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   233
import java.io.IOException;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   234
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   235
public class BytesToString {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   236
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   237
    public static void main(String[] args) throws IOException {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   238
        File f = new File(args[0]);
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   239
        int len = (int)f.length();
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   240
        byte[] classBytes = new byte[len];
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   241
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   242
        FileInputStream in = new FileInputStream(args[0]);
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   243
        try {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   244
            int pos = 0;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   245
            for (;;) {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   246
                int n = in.read(classBytes, pos, (len-pos));
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   247
                if (n < 0)
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   248
                    throw new RuntimeException("class file changed??");
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   249
                pos += n;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   250
                if (pos >= n)
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   251
                    break;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   252
            }
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   253
        } finally {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   254
            in.close();
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   255
        }
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
        int pos = 0;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   258
        boolean lastWasOctal = false;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   259
        for (int i=0; i<len; i++) {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   260
            int value = classBytes[i];
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   261
            if (value < 0)
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   262
                value += 256;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   263
            String s = null;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   264
            if (value == '\\')
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   265
                s = "\\\\";
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   266
            else if (value == '\"')
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   267
                s = "\\\"";
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   268
            else {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   269
                if ((value >= 32 && value < 127) && ((!lastWasOctal ||
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   270
                    (value < '0' || value > '7')))) {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   271
                    s = Character.toString((char)value);
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   272
                }
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
            if (s == null) {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   275
                s = "\\" + Integer.toString(value, 8);
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   276
                lastWasOctal = true;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   277
            } else {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   278
                lastWasOctal = false;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   279
            }
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   280
            if (pos > 61) {
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   281
                System.out.print("\"");
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   282
                if (i<len)
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   283
                    System.out.print("+");
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   284
                System.out.println();
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   285
                pos = 0;
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   286
            }
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   287
            if (pos == 0)
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   288
                System.out.print("                \"");
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   289
            System.out.print(s);
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   290
            pos += s.length();
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   291
        }
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   292
        System.out.println("\"");
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 1639
diff changeset
   293
    }
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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
*/