2.由于时间比较紧迫,限制最大管道数量200
3.代码如下
package PUB;import java.io.*;
public class PipeManager
{
private static PipedInputStream[] pipedIS ;
private static PipedOutputStream[] pipedOS ;
private static int[] inchat ;
private static int maxpipe=20;
private static String[] mysessions ;
private static PipeManager instance = null;
private PipeManager()
{
mysessions = new String[maxpipe];
pipedIS = new PipedInputStream[maxpipe];
pipedOS = new PipedOutputStream[maxpipe];
inchat = new int[maxpipe];
for(int i=0;i<maxpipe;i++)
{
mysessions[i]="";
inchat[i]=0;
pipedIS[i] = new PipedInputStream();
pipedOS[i] = new PipedOutputStream();
try
{
pipedIS[i].connect(pipedOS[i]);
}
catch (IOException e)
{
System.err.println("连接失败");
}
}
System.out.println("PipeManager init");
}
/** *//**
* @return 单一实例
*/
public static PipeManager getInstance(){
if(instance==null) instance = new PipeManager();
return instance;
}
//找到管道名touser,内容mysessionid-msg
public synchronized boolean writemsg(String mysessionid,String touser,String msg)
{
if(msg.length()==0) return false;
// if(1==1) return true;
int nget=-1;
int nullget=-1;
for(int i=0;i<maxpipe;i++)
{
if(mysessions[i]!=null&&mysessions[i].equals(mysessionid))
{
nget=i;
}
if(mysessions[i]==null||mysessions[i].length()==0)
{
nullget=i;
}
if(nget>=0&&nullget>=0) break;
}
if(nullget==-1)
{
//有问题
return false;
}
if(nget==-1)
{
mysessions[nullget]=mysessionid;
nget=nullget;
}
//写数据
try
{
String writestr=touser+"-"+msg;
byte[] b = writestr.getBytes("utf-8");
int len = b.length;
// 在该线程阻塞之前,有最多1024字节的数据被写入
pipedOS[nget].write( b, 0,len );
inchat[nget]++;
}
catch (IOException e)
{
System.err.println("写操作错误");
System.exit(1);
}
return true;
}
//找到管道名mysessionid,读出内容fromuser-msg
public synchronized String readmsg(String mysessionid)
{
int nget=-1;
int nullget=-1;
for(int i=0;i<maxpipe;i++)
{
if(mysessions[i]!=null&&mysessions[i].equals(mysessionid))
{
nget=i;
}
if(mysessions[i]==null||mysessions[i].length()==0)
{
nullget=i;
}
if(nget>=0&&nullget>=0) break;
}
if(nget==-1) return "";
if( inchat[nget]>0) inchat[nget]--;
else return "";
//读取数据
byte[] inArray = new byte[1000];
int bytesRead = 0;
try
{
int canread = pipedIS[nget].available();
bytesRead = pipedIS[nget].read(inArray, 0, canread);
System.out.println("已经读取" + bytesRead + "字节...");
if(bytesRead>0)
{
return new String(inArray,"utf-8");
}
}
catch (IOException e)
{
System.err.println("读取输入错误.");
System.exit(1);
}
return "";
}
//删除管道,找到对应管道对,close,并=null
public boolean removepipe(String mysessionid)
{
int nget=-1;
int nullget=-1;
for(int i=0;i<maxpipe;i++)
{
if(mysessions[i]!=null&&mysessions[i].equals(mysessionid))
{
nget=i;
}
if(mysessions[i]==null||mysessions[i].length()==0)
{
nullget=i;
}
if(nget>=0&&nullget>=0) break;
}
if(nget==-1) return false;
readmsg(mysessionid);
mysessions[nget]="";
return true;
}
public int getinchat(String mysessionid)
{
int nget=-1;
int nullget=-1;
for(int i=0;i<maxpipe;i++)
{
if(mysessions[i]!=null&&mysessions[i].equals(mysessionid))
{
nget=i;
}
if(mysessions[i]==null||mysessions[i].length()==0)
{
nullget=i;
}
if(nget>=0&&nullget>=0) break;
}
if(nget==-1) return 0;
if( inchat[nget]>0) return inchat[nget];
else return 0;
}
}