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

📄 cmppbuffer.java~1~

📁 短信网关发送接受平台。
💻 JAVA~1~
字号:
import java.lang.*;
import java.net.*;
import java.io.*;

import CMPPLog;
import CMPPException;

import CMPPUtility;

public class CMPPBuffer
{
	final private static int SIZEOF_BYTE 	= 1;
	final private static int SIZEOF_SHORT 	= 2;
	final private static int SIZEOF_INTEGER = 4;
	final private static int SIZEOF_LONG 	= 8;

	int m_nOffset;
	byte[] m_bBytes;
	
	public CMPPBuffer()
	{
		m_nOffset = 0;
		m_bBytes = null;
	}
	
	public CMPPBuffer(CMPPBuffer buffer)
	{
		m_nOffset = buffer.m_nOffset;
		m_bBytes = buffer.m_bBytes;
	}
	
	public int getLength()
	{
		return (m_bBytes == null) ? 0 : m_bBytes.length;
	}

	private byte getByte(int nOffset)
		throws CMPPException
	{
		if((nOffset < 0) || (nOffset + SIZEOF_BYTE) > m_bBytes.length)
		{
			throw new CMPPException("CMPPBuffer.getByte : not proper offset !");
		}
		
		byte bValue = m_bBytes[nOffset];
		
		return bValue;
	}
	
	private void setByte(int nOffset,byte bValue)
		throws CMPPException
	{
		if((nOffset < 0) || (nOffset + SIZEOF_BYTE) > m_bBytes.length)
		{
			throw new CMPPException("CMPPBuffer.setByte : not proper offset !");
		}
		
		m_bBytes[nOffset] = bValue;
	}		
	
	private short getShort(int nOffset)
		throws CMPPException
	{
		if((nOffset < 0 ) || (nOffset + SIZEOF_SHORT) > m_bBytes.length)
		{
			throw new CMPPException("CMPPBuffer.getShort : not proper offset !");
		}
		
		short sValue = 0;
		for(int i = 0;i < SIZEOF_SHORT;i ++)
		{
			/*Big-Endian Order
			sValue <<= 8;
			sValue |= (m_bBytes[nOffset + i] & 0xff);
			*/
			
			//Network Order
			sValue |= (m_bBytes[nOffset + i] & 0xff) << (i * 8);
		}
		
		return sValue;
	}
		
	private void setShort(int nOffset,short sValue)
		throws CMPPException
	{
		if((nOffset < 0) || (nOffset + SIZEOF_SHORT) > m_bBytes.length)
		{
			throw new CMPPException("CMPPBuffer.setShort : not proper offset !");
		}
		
		for(int i = 0;i < SIZEOF_SHORT;i ++)
		{
			/*Big-Endian Order
			m_bBytes[nOffset + SIZEOF_SHORT - i - 1] = (byte)sValue;
			*/
			
			//Network Order
			m_bBytes[nOffset + i] = (byte)sValue;
			sValue >>= 8;
		}
	}
	
	private int getInteger(int nOffset)
		throws CMPPException
	{
		if((nOffset < 0) || (nOffset + SIZEOF_INTEGER) > m_bBytes.length)
		{
			throw new CMPPException("CMPPBuffer.getInteger : not proper offset !");
		}
		
		int nValue = 0;
		for(int i = 0;i < SIZEOF_INTEGER;i ++)
		{
			/*Big-Endian Order
			nValue <<= 8;
			nValue |= (m_bBytes[nOffset + i] & 0xff);
			*/
			
			//Network Order
			nValue |= (m_bBytes[nOffset + i] & 0xff) << (i * 8);
		}
		
		return nValue;
	}

	private void setInteger(int nOffset,int nValue)
		throws CMPPException
	{
		if((nOffset < 0) || (nOffset + SIZEOF_INTEGER) > m_bBytes.length)
		{
			throw new CMPPException("CMPPBuffer.setInteger : not proper offset !");
		}
		
		for(int i = 0;i < SIZEOF_INTEGER;i ++)
		{
			/*Big-Endian Order
			m_bBytes[nOffset + SIZEOF_INTEGER - i - 1] = (byte)nValue;
			nValue >>= 8;
			*/
			
			//Network Order
			m_bBytes[nOffset + i] = (byte)nValue;
			nValue >>= 8;
		}
	}

	private long getLong(int nOffset)
		throws CMPPException
	{
		if((nOffset < 0) || (nOffset + SIZEOF_LONG) > m_bBytes.length)
		{
			throw new CMPPException("CMPPBuffer.getLong : not proper offset !");
		}
		
		long lValue = 0;
		for(int i = 0;i < SIZEOF_LONG;i ++)
		{
			/*Big-Endian Order
			lValue = (lValue << 8);
			lValue |= (m_bBytes[nOffset + i] & 0xff);
			*/
			
			//Network Order
			lValue |= (m_bBytes[nOffset + i] & 0xff) << (i * 8);
		}
		
		return lValue;
	}

	private void setLong(int nOffset,long lValue)
		throws CMPPException
	{
		if((nOffset < 0) || (nOffset + SIZEOF_LONG) > m_bBytes.length)
		{
			throw new CMPPException("CMPPBuffer.setInteger : not proper offset !");
		}
		
		for(int i = 0;i < SIZEOF_LONG;i ++)
		{
			/*Big-Endia Order
			m_bBytes[nOffset + SIZEOF_LONG - i - 1] = (byte)lValue;
			lValue >>= 8;
			*/
			
			//Network Order
			m_bBytes[nOffset + i] = (byte)lValue;
			lValue >>= 8;
		}
	}

	private void getBytes(int nOffset,byte[] bBytes)
		throws CMPPException
	{
		if(bBytes == null || bBytes.length <= 0)
		{
			throw new CMPPException("CMPPBuffer.getBytes : null bBytes !");
		}
			
		if((nOffset < 0) || (nOffset + bBytes.length) > m_bBytes.length)
		{
			throw new CMPPException("CMPPBuffer.getBytes : not proper offset !");
		}
		
		for(int i = 0;i < bBytes.length;i ++)
		{
			bBytes[i] = m_bBytes[nOffset + i];
		}
	}

	private void setBytes(int nOffset,byte[] bBytes)
		throws CMPPException
	{
		if(bBytes == null || bBytes.length <= 0)
		{
			throw new CMPPException("CMPPBuffer.setBytes : null bBytes !");
		}
			
		if((nOffset < 0) || (nOffset + bBytes.length) > m_bBytes.length)
		{
			throw new CMPPException("CMPPBuffer.setBytes : not proper offset !");
		}
		
		for(int i = 0;i < bBytes.length;i ++)
		{
			m_bBytes[nOffset + i] = bBytes[i];
		}
	}

	protected void inputBuffer(InputStream is,int nLength)
		throws IOException, CMPPException
	{
		byte[] bBytes = new byte[nLength];
		
		CMPPUtility.read(is,bBytes);

		CMPPLog.log("CMPPBuffer.inputBuffer : read " + nLength + " bytes !",
			CMPPLog.LOG_CMPP_BUFFER | CMPPLog.LOG_INPUT_METHOD);
			
		append(bBytes);
		
		dumpBuffer(CMPPLog.LOG_INPUT_METHOD);
	}

	protected void outputBuffer(OutputStream os)
		throws IOException, CMPPException
	{
		CMPPLog.log("CMPPBuffer.outputBuffer : write " + m_bBytes.length + " bytes !",
			CMPPLog.LOG_CMPP_BUFFER | CMPPLog.LOG_OUTPUT_METHOD);
		
		dumpBuffer(CMPPLog.LOG_OUTPUT_METHOD);
		
		CMPPUtility.write(os,m_bBytes);
		
		os.flush();
	}

	public void dumpBuffer(long lMethod)
	{
		CMPPLog.log("\tnOffset = " + m_nOffset,
			CMPPLog.LOG_CMPP_BUFFER | lMethod);
			
		CMPPLog.log("\tbBytes = 0x" + CMPPUtility.toHexString(m_bBytes),
			CMPPLog.LOG_CMPP_BUFFER | lMethod);
	}

	public void append(byte[] bytes)
		throws CMPPException
	{
		if(bytes == null || bytes.length <= 0)
		{
			throw new CMPPException("CMPPBuffer.append : null bytes !");
		}
		
		int nLength = (m_bBytes == null) ? 0 : m_bBytes.length;
		
		byte[] bBytes = new byte[nLength + bytes.length];
		
		for(int i = 0;i < nLength;i ++)
		{
			bBytes[i] = m_bBytes[i];
		}
		
		m_bBytes = bBytes;
		
		setBytes(nLength,bytes);
	}

	public void insertByte(byte bValue)
		throws CMPPException
	{
		int nLength = (m_bBytes == null) ? 0 : m_bBytes.length;
		
		byte[] bBytes = new byte[nLength + SIZEOF_BYTE];
		
		for(int i = 0;i < nLength;i ++)
		{
			bBytes[i + SIZEOF_BYTE] = m_bBytes[i];
		}
		
		m_bBytes = bBytes;

		setByte(0,bValue);
	}
	
	public void insertShort(short sValue)
		throws CMPPException
	{
		int nLength = (m_bBytes == null) ? 0 : m_bBytes.length;
		
		byte[] bBytes = new byte[nLength + SIZEOF_SHORT];
		
		for(int i = 0;i < nLength;i ++)
		{
			bBytes[i + SIZEOF_SHORT] = m_bBytes[i];
		}
		
		m_bBytes = bBytes;

		setShort(0,sValue);
	}
	
	public void insertInteger(int nValue)
		throws CMPPException
	{
		int nLength = (m_bBytes == null) ? 0 : m_bBytes.length;
		
		byte[] bBytes = new byte[nLength + SIZEOF_INTEGER];
		
		for(int i = 0;i < nLength;i ++)
		{
			bBytes[i + SIZEOF_INTEGER] = m_bBytes[i];
		}
		
		m_bBytes = bBytes;
		
		setInteger(0,nValue);
	}
	
	public void insertLong(long lValue)
		throws CMPPException
	{
		int nLength = (m_bBytes == null) ? 0 : m_bBytes.length;
		
		byte[] bBytes = new byte[nLength + SIZEOF_LONG];
		
		for(int i = 0;i < nLength;i ++)
		{
			bBytes[i + SIZEOF_LONG] = m_bBytes[i];
		}
		
		m_bBytes = bBytes;
		
		setLong(0,lValue);
	}
	
	public void insertBytes(byte[] bytes)
		throws CMPPException
	{
		if(bytes == null || bytes.length <= 0)
		{
			throw new CMPPException("CMPPBuffer.insertBytes : null bytes !");
		}
		
		int nLength = (m_bBytes == null) ? 0 : m_bBytes.length;
		
		byte[] bBytes = new byte[nLength + bytes.length];
		
		for(int i = 0;i < nLength;i ++)
		{
			bBytes[i + bytes.length] = m_bBytes[i];
		}
		
		m_bBytes = bBytes;
		
		setBytes(0,bytes);
	}
	
	public void insertString(String string)
		throws CMPPException
	{
		if(string == null || string.length() <= 0)
		{
			throw new CMPPException("CMPPBuffer.insertString : null string !");
		}
		
		int nLength = string.length();
		byte[] bBytes = new byte[nLength];
		
		for(int i = 0;i < nLength;i ++)
		{
			bBytes[i] = (byte)(string.charAt(i) & 0x7f);
		}
		
		insertBytes(bBytes);
	}
	
	public byte getByte()
		throws CMPPException
	{
		byte bValue = getByte(m_nOffset);
		m_nOffset += SIZEOF_BYTE;
		
		return bValue;
	}

	public short getShort()
		throws CMPPException
	{
		short sValue = getShort(m_nOffset);
		m_nOffset += SIZEOF_SHORT;
		
		return sValue;
	}
	
	public int getInteger()
		throws CMPPException
	{
		int nValue = getInteger(m_nOffset);
		m_nOffset += SIZEOF_INTEGER;
		
		return nValue;
	}
	
	public long getLong()
		throws CMPPException
	{
		long lValue = getLong(m_nOffset);
		m_nOffset += SIZEOF_LONG;
		
		return lValue;
	}
	
	public void getBytes(byte[] bBytes)
		throws CMPPException
	{
		getBytes(m_nOffset,bBytes);
		m_nOffset += bBytes.length;
	}
	
	public byte[] getBytes(int nLength)
		throws CMPPException
	{
		if(nLength <= 0)
		{
			return null;
		}
		
		byte[] bBytes = new byte[nLength];
		getBytes(bBytes);
		
		return bBytes;
	}
	
	public String getString(int nLength)
		throws CMPPException
	{
		byte[] bBytes = getBytes(nLength);
		
		if(bBytes != null && bBytes.length > 0)
		{
			String string = "";
			for(int i = 0;i < bBytes.length;i ++)
			{
				string += (char)(bBytes[i] & 0x7f);
			}
		
			return new String(string);
		}
		
		return null;
	}
}

⌨️ 快捷键说明

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