httpconnectionbase.java

来自「cqME :java framework for TCK test.」· Java 代码 · 共 555 行 · 第 1/2 页

JAVA
555
字号
/* * $Id$ * * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package com.sun.cldc.communication.http;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.InterruptedIOException;import java.io.OutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.util.Hashtable;import java.util.Enumeration;import java.io.ByteArrayOutputStream;import javax.microedition.io.Connector;import javax.microedition.io.StreamConnection;/** * This class implements the necessary functionality * for an HTTP connection.  */public class HttpConnectionBase {    public final static String GET = "GET";    public static final int HTTP_OK = 200;    public static final int HTTP_PARTIAL = 206;    public final static String POST = "POST";    public final static String HEAD = "HEAD";    private int WAIT_RESPONSE = 60000;    private int index;    private String url;    private String protocol;    private String host;    private String file;    private String ref;    private String query;    private int port = 80;    private int responseCode;    private String responseMsg;    private Hashtable reqProperties;    private Hashtable headerFields;    private String method;    private int opens;    private boolean connected;    private boolean timeouts;    private PrivateOutputStream poster;    private int mode;    private StreamConnection netIO;    private DataOutputStream output;    private DataInputStream input;    private static final String encoding = "ISO8859_1";    /**     * create a new instance of this class.     * We are initially unconnected.     */    public HttpConnectionBase() {        reqProperties = new Hashtable();        headerFields = new Hashtable();        opens = 0;        connected = false;        method = GET;        responseCode = -1;        protocol = "http";    }    public void open(String url, int mode, boolean timeouts) throws IOException {        if (opens > 0) {            throw new IOException("already connected");        }        this.timeouts = timeouts;        opens++;        if (mode != Connector.READ && mode != Connector.WRITE            && mode != Connector.READ_WRITE) {            throw new IOException("illegal mode: " + mode);        }        this.url = new String(url);        this.mode = mode;        parseURL();    }    public void close() throws IOException {        if (--opens == 0 && connected) disconnect();    }    public InputStream openInputStream() throws IOException {        connect();        opens++;        return new PrivateInputStream(input);    }    public OutputStream openOutputStream() throws IOException {        if (mode != Connector.WRITE && mode != Connector.READ_WRITE) {            throw new IOException("read-only connection");        }        opens++;        poster = new PrivateOutputStream();        return poster;    }    class PrivateInputStream extends InputStream {        private InputStream worker;        public PrivateInputStream(InputStream worker) {            this.worker = worker;        }        public int read() throws IOException {            return worker.read();        }        public int read(byte[] buf, int offset, int length) throws IOException {            return worker.read(buf, offset, length);        }        public int available() throws IOException {            return worker.available();        }        public void close() throws IOException {            worker.close();            if (--opens == 0 && connected) disconnect();        }    }    class PrivateOutputStream extends OutputStream {        private ByteArrayOutputStream output;        public PrivateOutputStream() {            output = new ByteArrayOutputStream();        }        public void write(int b) throws IOException {            output.write(b);        }        public void flush() throws IOException {            if (output.size() > 0) connect();        }        public byte[] toByteArray() {            return output.toByteArray();        }        public int size() {            return output.size();        }        public void close() throws IOException {            flush();            if (--opens == 0 && connected) disconnect();        }    }    public String getURL() {        return url;    }    public String getProtocol() {        return protocol;    }    public String getHost() {        return host;    }    public String getFile() {        return file;    }    public String getRef() {        return ref;    }    public String getQuery() {        return query;    }    public int getPort() {        return port;    }    public String getRequestMethod() {        return method;    }    public void setRequestMethod(String method) throws IOException {        if (connected) throw new IOException("connection already open");        if (!method.equals(HEAD) && !method.equals(GET) && !method.equals(POST)) {            throw new IOException("unsupported method: " + method);        }        this.method = new String(method);    }    public String getRequestProperty(String key) {        return (String)reqProperties.get(key);    }    public void setRequestProperty(String key, String value) throws IOException {        if (connected) throw new IOException("connection already open");        reqProperties.put(key, value);    }    public int getResponseCode() throws IOException {        connect();        return responseCode;    }    public String getResponseMessage() throws IOException {        connect();        return responseMsg;    }    public long getLength() {        try {connect();}        catch (IOException x) {return -1;}	    return getHeaderFieldInt("content-length", -1);    }    public String getType() {        try {connect();}        catch (IOException x) {return null;}	    return getHeaderField("content-type");    }    public String getEncoding() {        try {connect();}        catch (IOException x) {return null;}	    return getHeaderField("content-encoding");    }    public String getHeaderField(String name) {        try {connect();}        catch (IOException x) {return null;}	    return (String)headerFields.get(name.toLowerCase());    }    public int getHeaderFieldInt(String name, int def) {        try {connect();}        catch (IOException x) {return def;}    	try {    	    return Integer.parseInt(getHeaderField(name));    	} catch(Throwable t) {}

⌨️ 快捷键说明

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