hessiandebugstate.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,772 行 · 第 1/3 页

JAVA
1,772
字号
/* * Copyright (c) 2001-2004 Caucho Technology, Inc.  All rights reserved. * * The Apache Software License, Version 1.1 * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, if *    any, must include the following acknowlegement: *       "This product includes software developed by the *        Caucho Technology (http://www.caucho.com/)." *    Alternately, this acknowlegement may appear in the software itself, *    if and wherever such third-party acknowlegements normally appear. * * 4. The names "Hessian", "Resin", and "Caucho" must not be used to *    endorse or promote products derived from this software without prior *    written permission. For written permission, please contact *    info@caucho.com. * * 5. Products derived from this software may not be called "Resin" *    nor may "Resin" appear in their names without prior written *    permission of Caucho Technology. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @author Scott Ferguson */package com.caucho.hessian.io;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.util.ArrayList;/** * Debugging input stream for Hessian requests. */public class HessianDebugState implements Hessian2Constants{  private PrintWriter _dbg;  private State _state;  private ArrayList<State> _stateStack = new ArrayList<State>();  private ArrayList<ObjectDef> _objectDefList    = new ArrayList<ObjectDef>();  private ArrayList<String> _typeDefList    = new ArrayList<String>();  private int _refId;  private boolean _isNewline = true;  private boolean _isObject = false;  private int _column;    /**   * Creates an uninitialized Hessian input stream.   */  public HessianDebugState(PrintWriter dbg)  {    _dbg = dbg;    _state = new InitialState();  }  /**   * Reads a character.   */  public void next(int ch)    throws IOException  {    _state = _state.next(ch);  }  void pushStack(State state)  {    _stateStack.add(state);  }  State popStack()  {    return _stateStack.remove(_stateStack.size() - 1);  }  void println()  {    if (! _isNewline) {      _dbg.println();      _dbg.flush();    }    _isNewline = true;    _column = 0;  }  abstract class State {    State _next;    State()    {    }    State(State next)    {      _next = next;    }        abstract State next(int ch);    boolean isShift(Object value)    {      return false;    }    State shift(Object value)    {      return this;    }    int depth()    {      if (_next != null)	return _next.depth();      else	return 0;    }    void printIndent(int depth)    {      if (_isNewline) {	for (int i = _column; i < depth() + depth; i++) {	  _dbg.print(" ");	  _column++;	}      }    }    void print(String string)    {      print(0, string);    }    void print(int depth, String string)    {      printIndent(depth);            _dbg.print(string);      _isNewline = false;      _isObject = false;      int p = string.lastIndexOf('\n');      if (p > 0)	_column = string.length() - p - 1;      else	_column += string.length();    }    void println(String string)    {      println(0, string);    }    void println(int depth, String string)    {      printIndent(depth);      _dbg.println(string);      _dbg.flush();      _isNewline = true;      _isObject = false;      _column = 0;    }    void println()    {      if (! _isNewline) {	_dbg.println();	_dbg.flush();      }      _isNewline = true;      _isObject = false;      _column = 0;    }    void printObject(String string)    {      if (_isObject)	println();            printIndent(0);      _dbg.print(string);      _dbg.flush();      _column += string.length();      _isNewline = false;      _isObject = true;    }        protected State nextObject(int ch)    {      switch (ch) {      case -1:	println();	return this;	      case 'N':	if (isShift(null))	  return shift(null);	else {	  printObject("null");	  return this;	}	      case 'T':	if (isShift(Boolean.TRUE))	  return shift(Boolean.TRUE);	else {	  printObject("true");	  return this;	}	      case 'F':	if (isShift(Boolean.FALSE))	  return shift(Boolean.FALSE);	else {	  printObject("false");	  return this;	}      case 0x80: case 0x81: case 0x82: case 0x83:       case 0x84: case 0x85: case 0x86: case 0x87:       case 0x88: case 0x89: case 0x8a: case 0x8b:       case 0x8c: case 0x8d: case 0x8e: case 0x8f:       case 0x90: case 0x91: case 0x92: case 0x93:       case 0x94: case 0x95: case 0x96: case 0x97:       case 0x98: case 0x99: case 0x9a: case 0x9b:       case 0x9c: case 0x9d: case 0x9e: case 0x9f:       case 0xa0: case 0xa1: case 0xa2: case 0xa3:       case 0xa4: case 0xa5: case 0xa6: case 0xa7:       case 0xa8: case 0xa9: case 0xaa: case 0xab:       case 0xac: case 0xad: case 0xae: case 0xaf:       case 0xb0: case 0xb1: case 0xb2: case 0xb3:       case 0xb4: case 0xb5: case 0xb6: case 0xb7:       case 0xb8: case 0xb9: case 0xba: case 0xbb:       case 0xbc: case 0xbd: case 0xbe: case 0xbf:	{	  Integer value = new Integer(ch - 0x90);	  	  if (isShift(value))	    return shift(value);	  else {	    printObject(value.toString());	    return this;	  }	}      case 0xc0: case 0xc1: case 0xc2: case 0xc3:       case 0xc4: case 0xc5: case 0xc6: case 0xc7:       case 0xc8: case 0xc9: case 0xca: case 0xcb:       case 0xcc: case 0xcd: case 0xce: case 0xcf:	return new IntegerState(this, "int", ch - 0xc8, 3);      case 0xd0: case 0xd1: case 0xd2: case 0xd3:       case 0xd4: case 0xd5: case 0xd6: case 0xd7: 	return new IntegerState(this, "int", ch - 0xd4, 2);      case 'I':	return new IntegerState(this, "int");	      case 0xd8: case 0xd9: case 0xda: case 0xdb:       case 0xdc: case 0xdd: case 0xde: case 0xdf:       case 0xe0: case 0xe1: case 0xe2: case 0xe3:       case 0xe4: case 0xe5: case 0xe6: case 0xe7:       case 0xe8: case 0xe9: case 0xea: case 0xeb:       case 0xec: case 0xed: case 0xee: case 0xef:	{	  Long value = new Long(ch - 0xe0);	  	  if (isShift(value))	    return shift(value);	  else {	    printObject(value.toString() + "L");	    return this;	  }	}	      case 0xf0: case 0xf1: case 0xf2: case 0xf3:       case 0xf4: case 0xf5: case 0xf6: case 0xf7:       case 0xf8: case 0xf9: case 0xfa: case 0xfb:       case 0xfc: case 0xfd: case 0xfe: case 0xff:	return new LongState(this, "long", ch - 0xf8, 7);	      case 0x38: case 0x39: case 0x3a: case 0x3b:       case 0x3c: case 0x3d: case 0x3e: case 0x3f:	return new LongState(this, "long", ch - 0x3c, 6);	      case 0x77:	return new LongState(this, "long", 0, 4);      case 'L':	return new LongState(this, "long");      case 0x67: case 0x68:	{	  Double value = new Double(ch - 0x67);	  	  if (isShift(value))	    return shift(value);	  else {	    printObject(value.toString());	    return this;	  }	}      case 0x69:	return new DoubleIntegerState(this, 3);      case 0x6a:	return new DoubleIntegerState(this, 2);      case 0x6b:	return new FloatState(this);	      case 'D':	return new DoubleState(this);      case 0x4a:	return new RefState(this, "Ref", 0, 3);      case 0x4b:	return new RefState(this, "Ref", 0, 2);      case 'R':	return new RefState(this, "Ref");      case 'r':	return new RemoteState(this);      case 'd':	return new DateState(this);      case 0x00:	{	  String value = "\"\"";	  	  if (isShift(value))	    return shift(value);	  else {	    printObject(value.toString());	    return this;	  }	}      case 0x01: case 0x02: case 0x03:      case 0x04: case 0x05: case 0x06: case 0x07:      case 0x08: case 0x09: case 0x0a: case 0x0b:      case 0x0c: case 0x0d: case 0x0e: case 0x0f:	      case 0x10: case 0x11: case 0x12: case 0x13:      case 0x14: case 0x15: case 0x16: case 0x17:      case 0x18: case 0x19: case 0x1a: case 0x1b:      case 0x1c: case 0x1d: case 0x1e: case 0x1f:	return new StringState(this, 'S', ch);      case 'S': case 'X':	return new StringState(this, 'S', true);      case 's': case 'x':	return new StringState(this, 'S', false);      case 0x20:	{	  String value = "binary(0)";	  	  if (isShift(value))	    return shift(value);	  else {	    printObject(value.toString());	    return this;	  }	}      case 0x21: case 0x22: case 0x23:      case 0x24: case 0x25: case 0x26: case 0x27:      case 0x28: case 0x29: case 0x2a: case 0x2b:      case 0x2c: case 0x2d: case 0x2e: case 0x2f:	return new BinaryState(this, 'B', ch - 0x20);      case 'B':	return new BinaryState(this, 'B', true);      case 'b':	return new BinaryState(this, 'B', false);      case 'M':	return new MapState(this, _refId++);      case 'V':	return new ListState(this, _refId++);      case 'v':	return new CompactListState(this, _refId++);      case 'O':	return new ObjectDefState(this);      case 'o':	return new ObjectState(this, _refId++);      case 'P':	return new StreamingState(this, true);      case 'p':	return new StreamingState(this, false);	      default:	return this;      }    }  }    class InitialState extends State {    State next(int ch)    {      println();            if (ch == 'r') {	return new ReplyState(this);      }      else if (ch == 'c') {	return new CallState(this);      }      else	return nextObject(ch);    }  }    class IntegerState extends State {    String _typeCode;        int _length;    int _value;    IntegerState(State next, String typeCode)    {      super(next);      _typeCode = typeCode;    }    IntegerState(State next, String typeCode, int value, int length)    {      super(next);      _typeCode = typeCode;      _value = value;      _length = length;    }    State next(int ch)    {      _value = 256 * _value + (ch & 0xff);      if (++_length == 4) {	Integer value = new Integer(_value);		if (_next.isShift(value))	  return _next.shift(value);	else {	  printObject(value.toString());	  	  return _next;	}      }      else	return this;    }  }    class LongState extends State {    String _typeCode;        int _length;    long _value;    LongState(State next, String typeCode)    {      super(next);      _typeCode = typeCode;    }    LongState(State next, String typeCode, long value, int length)    {      super(next);      _typeCode = typeCode;      _value = value;      _length = length;    }    State next(int ch)    {      _value = 256 * _value + (ch & 0xff);      if (++_length == 8) {	Long value = new Long(_value);		if (_next.isShift(value))	  return _next.shift(value);	else {	  printObject(value.toString() + "L");	  	  return _next;	}      }      else	return this;    }  }    class DoubleIntegerState extends State {    int _length;    int _value;    boolean _isFirst = true;    DoubleIntegerState(State next, int length)    {      super(next);      _length = length;    }    State next(int ch)    {      if (_isFirst)	_value = (byte) ch;      else	_value = 256 * _value + (ch & 0xff);      _isFirst = false;      if (++_length == 4) {	Double value = new Double(_value);		if (_next.isShift(value))	  return _next.shift(value);	else {	  printObject(value.toString());	  	  return _next;	}      }      else	return this;    }  }    class RefState extends State {    String _typeCode;        int _length;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?