microburlapinput.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,032 行 · 第 1/2 页
JAVA
1,032 行
/* * 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 "Burlap", "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.client;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.Calendar;import java.util.Date;import java.util.Hashtable;import java.util.TimeZone;import java.util.Vector;/** * Input stream for Burlap requests, compatible with microedition * Java. It only uses classes and types available to J2ME. In * particular, it does not have any support for the <double> type. * * <p>MicroBurlapInput does not depend on any classes other than * in J2ME, so it can be extracted independently into a smaller package. * * <p>MicroBurlapInput is unbuffered, so any client needs to provide * its own buffering. * * <pre> * InputStream is = ...; // from http connection * MicroBurlapInput in = new MicroBurlapInput(is); * String value; * * in.startReply(); // read reply header * value = in.readString(); // read string value * in.completeReply(); // read reply footer * </pre> */public class MicroBurlapInput { private static int base64Decode[]; private InputStream is; protected int peek; protected boolean peekTag; protected Date date; protected Calendar utcCalendar; private Calendar localCalendar; protected Vector refs; protected String method; protected StringBuffer sbuf = new StringBuffer(); protected StringBuffer entity = new StringBuffer(); /** * Creates a new Burlap input stream, initialized with an * underlying input stream. * * @param is the underlying input stream. */ public MicroBurlapInput(InputStream is) { init(is); } /** * Creates an uninitialized Burlap input stream. */ public MicroBurlapInput() { } /** * Returns a call's method. */ public String getMethod() { return method; } /** * Initialize the Burlap input stream with a new underlying stream. * Applications can use <code>init(InputStream)</code> to reuse * MicroBurlapInput to save garbage collection. */ public void init(InputStream is) { this.is = is; this.refs = null; } /** * Starts reading the call * * <p>A successful completion will have a single value: * * <pre> * <burlap:call> * <method>method</method> * </pre> */ public void startCall() throws IOException { expectStartTag("burlap:call"); expectStartTag("method"); method = parseString(); expectEndTag("method"); this.refs = null; } /** * Completes reading the call. * * <pre> * </burlap:call> * </pre> */ public void completeCall() throws IOException { expectEndTag("burlap:call"); } /** * Reads a reply as an object. * If the reply has a fault, throws the exception. */ public Object readReply(Class expectedClass) throws Exception { if (startReply()) { Object value = readObject(expectedClass); completeReply(); return value; } else { Hashtable fault = readFault(); Object detail = fault.get("detail"); if (detail instanceof Exception) throw (Exception) detail; else { String code = (String) fault.get("code"); String message = (String) fault.get("message"); throw new BurlapServiceException(message, code, detail); } } } /** * Starts reading the reply. * * <p>A successful completion will have a single value. An unsuccessful * one will have a fault: * * <pre> * <burlap:reply> * </pre> * * @return true if success, false for fault. */ public boolean startReply() throws IOException { this.refs = null; expectStartTag("burlap:reply"); if (! parseTag()) throw new BurlapProtocolException("expected <value>"); String tag = sbuf.toString(); if (tag.equals("fault")) { peekTag = true; return false; } else { peekTag = true; return true; } } /** * Completes reading the reply. * * <pre> * </burlap:reply> * </pre> */ public void completeReply() throws IOException { expectEndTag("burlap:reply"); } /** * Reads a boolean value from the input stream. */ public boolean readBoolean() throws IOException { expectStartTag("boolean"); int value = parseInt(); expectEndTag("boolean"); return value != 0; } /** * Reads an integer value from the input stream. */ public int readInt() throws IOException { expectStartTag("int"); int value = parseInt(); expectEndTag("int"); return value; } /** * Reads a long value from the input stream. */ public long readLong() throws IOException { expectStartTag("long"); long value = parseLong(); expectEndTag("long"); return value; } /** * Reads a date value from the input stream. */ public long readUTCDate() throws IOException { expectStartTag("date"); if (utcCalendar == null) utcCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); long value = parseDate(utcCalendar); expectEndTag("date"); return value; } /** * Reads a date value from the input stream. */ public long readLocalDate() throws IOException { expectStartTag("date"); if (localCalendar == null) localCalendar = Calendar.getInstance(); long value = parseDate(localCalendar); expectEndTag("date"); return value; } /** * Reads a remote value from the input stream. */ public BurlapRemote readRemote() throws IOException { expectStartTag("remote"); String type = readType(); String url = readString(); expectEndTag("remote"); return new BurlapRemote(type, url); } /** * Reads a string value from the input stream. * * <p>The two valid possibilities are either a <null> * or a <string>. The string value is encoded in utf-8, and * understands the basic XML escapes: "&123;", "<", ">", * "'", """. * * <pre> * <null></null> * <string>a utf-8 encoded string</string> * </pre> */ public String readString() throws IOException { if (! parseTag()) throw new BurlapProtocolException("expected <string>"); String tag = sbuf.toString(); if (tag.equals("null")) { expectEndTag("null"); return null; } else if (tag.equals("string")) { sbuf.setLength(0); parseString(sbuf); String value = sbuf.toString(); expectEndTag("string"); return value; } else throw expectBeginTag("string", tag); } /** * Reads a byte array from the input stream. * * <p>The two valid possibilities are either a <null> * or a <base64>. */ public byte []readBytes() throws IOException { if (! parseTag()) throw new BurlapProtocolException("expected <base64>"); String tag = sbuf.toString(); if (tag.equals("null")) { expectEndTag("null"); return null; } else if (tag.equals("base64")) { sbuf.setLength(0); byte []value = parseBytes(); expectEndTag("base64"); return value; } else throw expectBeginTag("base64", tag); } /** * Reads an arbitrary object the input stream. */ public Object readObject(Class expectedClass) throws IOException { if (! parseTag()) throw new BurlapProtocolException("expected <tag>"); String tag = sbuf.toString(); if (tag.equals("null")) { expectEndTag("null"); return null; } else if (tag.equals("boolean")) { int value = parseInt(); expectEndTag("boolean"); return new Boolean(value != 0); } else if (tag.equals("int")) { int value = parseInt(); expectEndTag("int"); return new Integer(value); } else if (tag.equals("long")) { long value = parseLong(); expectEndTag("long"); return new Long(value); } else if (tag.equals("string")) { sbuf.setLength(0); parseString(sbuf); String value = sbuf.toString(); expectEndTag("string"); return value; } else if (tag.equals("xml")) { sbuf.setLength(0); parseString(sbuf); String value = sbuf.toString(); expectEndTag("xml"); return value; } else if (tag.equals("date")) { if (utcCalendar == null) utcCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); long value = parseDate(utcCalendar); expectEndTag("date"); return new Date(value); } else if (tag.equals("map")) { String type = readType(); return readMap(expectedClass, type); } else if (tag.equals("list")) { String type = readType(); int length = readLength(); return readList(expectedClass, type, length); } else if (tag.equals("ref")) { int value = parseInt(); expectEndTag("ref"); return refs.elementAt(value); } else if (tag.equals("remote")) { String type = readType(); String url = readString(); expectEndTag("remote"); return resolveRemote(type, url); } else return readExtensionObject(expectedClass, tag); } /** * Reads a type value from the input stream. * * <pre> * <type>a utf-8 encoded string</type> * </pre> */ public String readType() throws IOException { if (! parseTag()) throw new BurlapProtocolException("expected <type>"); String tag = sbuf.toString(); if (! tag.equals("type")) throw new BurlapProtocolException("expected <type>"); sbuf.setLength(0); parseString(sbuf); String value = sbuf.toString(); expectEndTag("type"); return value; } /** * Reads a length value from the input stream. If the length isn't * specified, returns -1. * * <pre> * <length>integer</length> * </pre> */ public int readLength() throws IOException { expectStartTag("length");
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?