View Javadoc

    1   /*
    2    * Copyright 2012 The Netty Project
    3    *
    4    * The Netty Project licenses this file to you under the Apache License,
    5    * version 2.0 (the "License"); you may not use this file except in compliance
    6    * with the License. You may obtain a copy of the License at:
    7    *
    8    *   http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    13   * License for the specific language governing permissions and limitations
    14   * under the License.
    15   */
    16  package org.jboss.netty.handler.codec.serialization;
    17
    18  import java.io.EOFException;
    19  import java.io.IOException;
    20  import java.io.InputStream;
    21  import java.io.ObjectInputStream;
    22  import java.io.ObjectStreamClass;
    23  import java.io.StreamCorruptedException;
    24
    25  class CompactObjectInputStream extends ObjectInputStream {
    26
    27      private final ClassResolver classResolver;
    28
    29      CompactObjectInputStream(InputStream in, ClassResolver classResolver) throws IOException {
    30          super(in);
    31          if (classResolver == null) {
    32              throw new NullPointerException("classResolver");
    33          }
    34          this.classResolver = classResolver;
    35      }
    36
    37      @Override
    38      protected void readStreamHeader() throws IOException {
    39          int version = readByte() & 0xFF;
    40          if (version != STREAM_VERSION) {
    41              throw new StreamCorruptedException(
    42                      "Unsupported version: " + version);
    43          }
    44      }
    45
    46      @Override
    47      protected ObjectStreamClass readClassDescriptor()
    48              throws IOException, ClassNotFoundException {
    49          int type = read();
    50          if (type < 0) {
    51              throw new EOFException();
    52          }
    53          switch (type) {
    54          case CompactObjectOutputStream.TYPE_FAT_DESCRIPTOR:
    55              return super.readClassDescriptor();
    56          case CompactObjectOutputStream.TYPE_THIN_DESCRIPTOR:
    57              String className = readUTF();
    58              Class<?> clazz = classResolver.resolve(className);
    59              ObjectStreamClass streamClass = ObjectStreamClass.lookup(clazz);
    60              if (streamClass == null) {
    61                  // If streamClass is null its very likely that we had an old netty version that was writing an
    62                  // interface with a thin descriptor. Fall back to use ObjectStreamClazz.lookupAny(..) to resolve
    63                  // it.
    64                  //
    65                  // This will only work on java6+ but if we hit this line its very likely that a user is upgrading
    66                  // from netty 3.2.x which was using the method before and so use java6+.
    67                  //
    68                  // See https://github.com/netty/netty/commit/6c2eba79d70a532822a0e38092faa9783d90906b
    69                  streamClass = ObjectStreamClass.lookupAny(clazz);
    70              }
    71              return streamClass;
    72          default:
    73              throw new StreamCorruptedException(
    74                      "Unexpected class descriptor type: " + type);
    75          }
    76      }
    77
    78      @Override
    79      protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
    80          Class<?> clazz;
    81          try {
    82              clazz = classResolver.resolve(desc.getName());
    83          } catch (ClassNotFoundException ex) {
    84              clazz = super.resolveClass(desc);
    85          }
    86
    87          return clazz;
    88      }
    89
    90  }