burlapinput.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,810 行 · 第 1/3 页
JAVA
1,810 行
/* * Copyright (c) 2001-2008 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.burlap.io;import com.caucho.hessian.io.Deserializer;import com.caucho.hessian.io.HessianRemoteResolver;import com.caucho.hessian.io.SerializerFactory;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.Reader;import java.lang.reflect.Field;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.HashMap;import java.util.TimeZone;/** * Input stream for Burlap requests. * * <p>BurlapInput is unbuffered, so any client needs to provide * its own buffering. * * <pre> * InputStream is = ...; // from http connection * BurlapInput in = new BurlapInput(is); * String value; * * in.startReply(); // read reply header * value = in.readString(); // read string value * in.completeReply(); // read reply footer * </pre> */public class BurlapInput extends AbstractBurlapInput { private static int []base64Decode; public final static int TAG_EOF = -1; public final static int TAG_NULL = 0; public final static int TAG_BOOLEAN = 1; public final static int TAG_INT = 2; public final static int TAG_LONG = 3; public final static int TAG_DOUBLE = 4; public final static int TAG_DATE = 5; public final static int TAG_STRING = 6; public final static int TAG_XML = 7; public final static int TAG_BASE64 = 8; public final static int TAG_MAP = 9; public final static int TAG_LIST = 10; public final static int TAG_TYPE = 11; public final static int TAG_LENGTH = 12; public final static int TAG_REF = 13; public final static int TAG_REMOTE = 14; public final static int TAG_CALL = 15; public final static int TAG_REPLY = 16; public final static int TAG_FAULT = 17; public final static int TAG_METHOD = 18; public final static int TAG_HEADER = 19; public final static int TAG_NULL_END = TAG_NULL + 100; public final static int TAG_BOOLEAN_END = TAG_BOOLEAN + 100; public final static int TAG_INT_END = TAG_INT + 100; public final static int TAG_LONG_END = TAG_LONG + 100; public final static int TAG_DOUBLE_END = TAG_DOUBLE + 100; public final static int TAG_DATE_END = TAG_DATE + 100; public final static int TAG_STRING_END = TAG_STRING + 100; public final static int TAG_XML_END = TAG_XML + 100; public final static int TAG_BASE64_END = TAG_BASE64 + 100; public final static int TAG_MAP_END = TAG_MAP + 100; public final static int TAG_LIST_END = TAG_LIST + 100; public final static int TAG_TYPE_END = TAG_TYPE + 100; public final static int TAG_LENGTH_END = TAG_LENGTH + 100; public final static int TAG_REF_END = TAG_REF + 100; public final static int TAG_REMOTE_END = TAG_REMOTE + 100; public final static int TAG_CALL_END = TAG_CALL + 100; public final static int TAG_REPLY_END = TAG_REPLY + 100; public final static int TAG_FAULT_END = TAG_FAULT + 100; public final static int TAG_METHOD_END = TAG_METHOD + 100; public final static int TAG_HEADER_END = TAG_HEADER + 100; private static HashMap _tagMap; private static Field _detailMessageField; protected SerializerFactory _serializerFactory; protected ArrayList _refs; // the underlying input stream private InputStream _is; // a peek character protected int _peek = -1; // the method for a call private String _method; private int _peekTag; private Throwable _replyFault; protected StringBuffer _sbuf = new StringBuffer(); protected StringBuffer _entityBuffer = new StringBuffer(); protected Calendar _utcCalendar; protected Calendar _localCalendar; /** * Creates an uninitialized Burlap input stream. */ public BurlapInput() { } /** * Creates a new Burlap input stream, initialized with an * underlying input stream. * * @param is the underlying input stream. */ public BurlapInput(InputStream is) { init(is); } /** * Sets the serializer factory. */ public void setSerializerFactory(SerializerFactory factory) { _serializerFactory = factory; } /** * Gets the serializer factory. */ public SerializerFactory getSerializerFactory() { return _serializerFactory; } /** * Initialize the burlap stream with the underlying input stream. */ public void init(InputStream is) { _is = is; _method = null; _peek = -1; _peekTag = -1; _refs = null; _replyFault = null; if (_serializerFactory == null) _serializerFactory = new SerializerFactory(); } /** * Returns the calls method */ public String getMethod() { return _method; } /** * Returns any reply fault. */ public Throwable getReplyFault() { return _replyFault; } /** * Starts reading the call * * <pre> * <burlap:call> * <method>method</method> * </pre> */ public void startCall() throws IOException { readCall(); while ((readHeader() != null)) readObject(); readMethod(); } /** * Starts reading the call * * <p>A successful completion will have a single value: * * <pre> * <burlap:call> * </pre> */ public int readCall() throws IOException { expectTag(TAG_CALL); int major = 1; int minor = 0; return (major << 16) + minor; } /** * Reads the method * * <pre> * <method>method</method> * </pre> */ public String readMethod() throws IOException { expectTag(TAG_METHOD); _method = parseString(); expectTag(TAG_METHOD_END); return _method; } /** * Completes reading the call * * <p>A successful completion will have a single value: * * <pre> * </burlap:call> * </pre> */ public void completeCall() throws IOException { expectTag(TAG_CALL_END); } /** * Reads a reply as an object. * If the reply has a fault, throws the exception. */ public Object readReply(Class expectedClass) throws Throwable { expectTag(TAG_REPLY); int tag = parseTag(); if (tag == TAG_FAULT) throw prepareFault(); else { _peekTag = tag; Object value = readObject(expectedClass); expectTag(TAG_REPLY_END); return value; } } /** * Starts reading the reply * * <p>A successful completion will have a single value: * * <pre> * <burlap:reply> * <value> * </pre> */ public void startReply() throws Throwable { expectTag(TAG_REPLY); int tag = parseTag(); if (tag == TAG_FAULT) throw prepareFault(); else _peekTag = tag; } /** * Prepares the fault. */ private Throwable prepareFault() throws IOException { HashMap fault = readFault(); Object detail = fault.get("detail"); String message = (String) fault.get("message"); if (detail instanceof Throwable) { _replyFault = (Throwable) detail; if (message != null && _detailMessageField != null) { try { _detailMessageField.set(_replyFault, message); } catch (Throwable e) { } } return _replyFault; } else { String code = (String) fault.get("code"); _replyFault = new BurlapServiceException(message, code, detail); return _replyFault; } } /** * Completes reading the call * * <p>A successful completion will have a single value: * * <pre> * </burlap:reply> * </pre> */ public void completeReply() throws IOException { expectTag(TAG_REPLY_END); } /** * Reads a header, returning null if there are no headers. * * <pre> * <header>value</header> * </pre> */ public String readHeader() throws IOException { int tag = parseTag(); if (tag == TAG_HEADER) { _sbuf.setLength(0); String value = parseString(_sbuf).toString(); expectTag(TAG_HEADER_END); return value; } _peekTag = tag; return null; } /** * Reads a null * * <pre> * <null></null> * </pre> */ public void readNull() throws IOException { int tag = parseTag(); switch (tag) { case TAG_NULL: expectTag(TAG_NULL_END); return; default: throw expectedTag("null", tag); } } /** * Reads a boolean * * <pre> * <boolean>0</boolean> * <boolean>1</boolean> * </pre> */ public boolean readBoolean() throws IOException { int tag = parseTag(); boolean value; switch (tag) { case TAG_NULL: value = false; expectTag(TAG_NULL_END); return value; case TAG_BOOLEAN: value = parseInt() != 0; expectTag(TAG_BOOLEAN_END); return value; case TAG_INT: value = parseInt() != 0; expectTag(TAG_INT_END); return value; case TAG_LONG: value = parseLong() != 0; expectTag(TAG_LONG_END); return value; case TAG_DOUBLE: value = parseDouble() != 0; expectTag(TAG_DOUBLE_END); return value; default: throw expectedTag("boolean", tag); } } /** * Reads a byte * * <pre> * <int>value</int> * </pre> */ public byte readByte() throws IOException { return (byte) readInt(); } /** * Reads a short * * <pre> * <int>value</int> * </pre> */ public short readShort() throws IOException { return (short) readInt(); } /** * Reads an integer * * <pre> * <int>value</int> * </pre> */ public int readInt() throws IOException { int tag = parseTag(); int value; switch (tag) { case TAG_NULL: value = 0; expectTag(TAG_NULL_END); return value; case TAG_BOOLEAN: value = parseInt(); expectTag(TAG_BOOLEAN_END); return value; case TAG_INT: value = parseInt(); expectTag(TAG_INT_END); return value; case TAG_LONG: value = (int) parseLong(); expectTag(TAG_LONG_END); return value; case TAG_DOUBLE: value = (int) parseDouble(); expectTag(TAG_DOUBLE_END); return value; default: throw expectedTag("int", tag); } } /** * Reads a long * * <pre> * <long>value</long> * </pre> */ public long readLong() throws IOException { int tag = parseTag(); long value; switch (tag) { case TAG_NULL: value = 0; expectTag(TAG_NULL_END); return value; case TAG_BOOLEAN: value = parseInt(); expectTag(TAG_BOOLEAN_END); return value; case TAG_INT: value = parseInt(); expectTag(TAG_INT_END); return value; case TAG_LONG: value = parseLong(); expectTag(TAG_LONG_END); return value; case TAG_DOUBLE: value = (long) parseDouble(); expectTag(TAG_DOUBLE_END); return value; default: throw expectedTag("long", tag); } } /** * Reads a float * * <pre> * <double>value</double> * </pre>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?