⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 packet.java

📁 封装了SQL、Socket、WAP、MIME等功能的通用组件
💻 JAVA
字号:
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: www.lazybug.com</p>
 * @author 刘学
 * @version 1.0
 */
package org.lazybug.net;

import org.lazybug.util.*;

public abstract class Packet implements PacketHeader
{
    public int page;             //缓冲区的页数
    public int offset;           //数据包数据的偏移量
    public int buffer_offset;    //缓冲区的偏移量(不会修改的)

    public byte[] ip;
    public int port;
    public Connection connection;

    protected Packet()
    {
        this.page = -1;
        this.offset = 0;
        this.ip = new byte[4];
    }
    //接收
    public abstract boolean receive() throws java.io.IOException;
    //发送
    public abstract boolean send() throws java.io.IOException;
    /**
     * 释放缓存的占用空间
     */
    public void release()
    {
        this.connection.release(page);
        this.page = -1;
    }

    public void setAddress(byte[] _ip, int port)
    {
        Tools.copyByteArray(_ip, ip);
        this.port = port;
    }
    /**
     * 当前缓存数据的长度
     */
    public int currentLength()
    {
        return offset - buffer_offset;
    }

    /**
     * 从缓存区读的方法
     */
    public byte read8()
    {
        return this.connection.read8(this.offset++);
    }

    public byte[] read16()
    {
        byte[] buf = this.connection.read16(this.offset);
        this.offset += 2;
        return buf;
    }

    public byte[] read32()
    {
        byte[] buf = this.connection.read32(this.offset);
        this.offset += 4;
        return buf;
    }

    public abstract byte[] readString();

/*    public byte[] readString(int length)
    {
        byte[] buf = this.connection.readString(this.offset, length);
        this.offset += buf.length + 2;
        return buf;
    }*/
    /**
     * 从缓存区写的方法
     */
    public void write8(byte buf)
    {
        this.connection.write8(buf, offset);
        offset += 1;
    }

    public void write16(byte[] buf)
    {
        this.connection.write16(buf, offset);
        offset += 2;
    }

    public void write32(byte[] buf)
    {
        this.connection.write32(buf, offset);
        offset += 4;
    }

    public abstract void writeString(byte[] buf);
//    {
//        this.connection.writeString(buf, offset);
//        offset += 2 + buf.length;
//    }
    /**
     * 得到字节流
     */
    public final byte[] getByteStream()
    {

        byte[] buffer = new byte[this.getLength()];
        for( int i = 0; i < this.getLength(); i++ )
        {
            buffer[i] = this.connection.getBuffer()[buffer_offset+i];
        }
        return buffer;
    }
}

⌨️ 快捷键说明

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