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

📄 cmppsynchronizedio.java

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

import CMPPLog;
import CMPPException;

import CMPPUtility;

public class CMPPSynchronizedIO
{
	// million seconds
	final static int TIMEOUT_RIGHTNOW	= 1;
	final static int DEFAULT_TIMEOUT	= 15000;
	
	Socket m_socket = null;
	
	InputStream m_is = null;
	OutputStream m_os = null;
	
	public CMPPSynchronizedIO(Socket socket)
		throws IOException, CMPPException
	{
		if(socket == null)
		{
			throw new CMPPException("CMPPSynchronizedIO.CMPPSynchronizedIO : null socket !");
		}
		m_socket = socket;
		
		m_is = socket.getInputStream();
		m_os = socket.getOutputStream();
	}
	
	public void setSoTimeout(int nTimeout)
	{
		try
		{
			m_socket.setSoTimeout(nTimeout);
		}
		catch(Exception e)
		{
			CMPPLog.log(e.getMessage(),
				LogRequest.LOG_EXCEPTION | CMPPLog.LOG_CMPP_SYNCHRONIZED_IO);
			CMPPLog.log("CMPPSynchronizedIO.setSoTime : unexpected exit !",
				LogRequest.LOG_EXCEPTION | CMPPLog.LOG_CMPP_SYNCHRONIZED_IO);
		}
	}
	
	public void close()
	{
		try
		{
			//关闭输入流
			m_is.close();
			//关闭输出流
			m_os.close();
		}
		catch(Exception e)
		{
			CMPPLog.log(e.getMessage(),
				LogRequest.LOG_EXCEPTION | CMPPLog.LOG_CMPP_SYNCHRONIZED_IO);
			CMPPLog.log("CMPPSynchronizedIO.close : unexpected exit !",
				LogRequest.LOG_EXCEPTION | CMPPLog.LOG_CMPP_SYNCHRONIZED_IO);
		}
		
		CMPPLog.log("CMPPSynchronizedIO.close : synchronized io closed !",
			LogRequest.LOG_EXCEPTION | CMPPLog.LOG_CMPP_SYNCHRONIZED_IO);
	}
	
	public void read(byte[] bBytes)
		throws IOException, CMPPException
	{
		if(m_is == null)
		{
			throw new CMPPException("CMPPSynchronizedIO.read : null input stream !");
		}
		
		if(bBytes == null || bBytes.length <= 0)
		{
			throw new CMPPException("CMPPSynchronizedIO.read : null buffer !");
		}
		
		int nLength = bBytes.length;
		
		int nOffset = 0;
		do
		{
			int nByteRead = m_is.read(bBytes,nOffset,nLength - nOffset);
			if(nByteRead == -1)
			{
				throw new CMPPException("CMPPSynchronizedIO.read : unexpected end !");
			}
			nOffset += nByteRead;
		}while(nOffset < nLength);
	}
	
	public void write(byte[] bBytes)
		throws IOException, CMPPException
	{
		if(m_os == null)
		{
			throw new CMPPException("CMPPSynchronizedIO.write : null output stream !");
		}
		
		if(bBytes == null || bBytes.length <= 0)
		{
			throw new CMPPException("CMPPSynchronizedIO.write : null buffer !");
		}
		
		m_os.write(bBytes);
	}
	
	public CMPPPacket read()
		throws CMPPException
	{
		if(m_is == null)
		{
			throw new CMPPException("CMPPSynchronizedIO.read : null input stream !");
		}
		
		try
		{
			CMPPPacket packet = new CMPPPacket();
			packet.inputPacket(m_is);
		
			return packet;
		}
		catch(Exception e)
		{
			/*
			CMPPLog.log(e.getMessage(),
				LogRequest.LOG_EXCEPTION | CMPPLog.LOG_CMPP_SYNCHRONIZED_IO);
			CMPPLog.log("CMPPSynchronizedIO.read : unexpected exit !",
				LogRequest.LOG_EXCEPTION | CMPPLog.LOG_CMPP_SYNCHRONIZED_IO);
			*/
		}
		
		return null;
	}
	
	public void write(CMPPPacket packet)
		throws IOException, CMPPException
	{
		if(m_os == null)
		{
			throw new CMPPException("CMPPSynchronizedIO.write : null output stream !");
		}
		
		if(packet == null)
		{
			throw new CMPPException("CMPPSynchronizedIO.write : null packet !");
		}
		
		packet.outputPacket(m_os);
	}
}

⌨️ 快捷键说明

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