httphandler.java

来自「Sony Ericsson手机上的Facebook客户端全套代码」· Java 代码 · 共 159 行

JAVA
159
字号
// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   HttpHandler.java

package se.southend.drops.resource;

import java.io.*;
import javax.microedition.io.*;

public class HttpHandler
{

    public HttpHandler()
    {
        instance = this;
    }

    public static HttpHandler getInstance()
    {
        if(instance == null)
            new HttpHandler();
        return instance;
    }

    public InputStream requestStream(String url)
        throws IOException
    {
        HttpConnection conn = (HttpConnection)Connector.open(url);
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Connection", "close");
        InputStream in = requestStream(conn);
        conn.close();
        return in;
    }

    private InputStream requestStream(HttpConnection conn)
        throws IOException
    {
        length = conn.getLength();
        responseCode = conn.getResponseCode();
        responseMessage = conn.getResponseMessage();
        type = conn.getType();
        lastModified = conn.getLastModified();
        data = null;
        if(getResponseCode() != 200)
            throw new IOException("HTTP connection to URL: '" + conn.getURL() + "' returned response: " + responseCode + " " + responseMessage);
        else
            return conn.openInputStream();
    }

    public void requestGet(String url)
        throws IOException
    {
        InputStream in = requestStream(url);
        if(length != -1L)
            data = getBytes(in, (int)getLength());
        else
            data = getBytes(in);
        in.close();
    }

    public void requestPost(String url, byte post[])
        throws IOException
    {
        HttpConnection conn = (HttpConnection)Connector.open(url);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Length", String.valueOf(post.length));
        conn.setRequestProperty("Connection", "close");
        OutputStream out = conn.openOutputStream();
        out.write(post);
        out.flush();
        InputStream in = requestStream(conn);
        if(length != -1L)
            data = getBytes(in, (int)getLength());
        else
            data = getBytes(in);
        in.close();
        out.close();
    }

    public long getLength()
    {
        return length;
    }

    public int getResponseCode()
    {
        return responseCode;
    }

    public String getResponseMessage()
    {
        return responseMessage;
    }

    public String getType()
    {
        return type;
    }

    public long getLastModified()
    {
        return lastModified;
    }

    public byte[] getData()
    {
        return data;
    }

    public void abort()
    {
        abort = true;
    }

    public byte[] getBytes(InputStream in, int length)
        throws IOException
    {
        abort = false;
        int offset = 0;
        byte data[] = new byte[length];
        for(; offset < length && !abort; offset += in.read(data, offset, length - offset));
        return abort ? null : data;
    }

    public byte[] getBytes(InputStream in)
        throws IOException
    {
        abort = false;
        byte buffer[] = new byte[2048];
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        do
        {
            if(abort)
                break;
            int readLen = in.read(buffer);
            if(readLen == -1)
                break;
            bytes.write(buffer, 0, readLen);
        } while(true);
        bytes.flush();
        byte data[] = bytes.toByteArray();
        bytes.close();
        length = data.length;
        return abort ? null : data;
    }

    private static final int BUFFER_SIZE = 2048;
    private static HttpHandler instance;
    private int responseCode;
    private String responseMessage;
    private long length;
    private String type;
    private long lastModified;
    private byte data[];
    private boolean abort;
}

⌨️ 快捷键说明

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