¡¡¡¡Ï°¹ßÁËTCP±à³Ì£¬ÈÏΪUDP¿ÉÒÔ°ü°ìÕâЩÎÊÌâÊÇ´íÎóµÄ¡£Ò»¸öUDPÓ¦ÓóÌÐòÒª³Ðµ£¿É¿¿ÐÔ·½ÃæµÄÈ«²¿¹¤×÷£¬°üÀ¨±¨ÎĵĶªÊ§¡¢Öظ´¡¢Ê±ÑÓ¡¢ÂÒÐòÒÔ¼°Á¬½ÓʧЧµÈÎÊÌâ¡£
¡¡¡¡Í¨³£ÎÒÃÇÔÚ¿É¿¿ÐԺ㬴«ÊäʱÑÓСµÄ¾ÖÓòÍøÉÏ¿ª·¢²âÊÔ£¬Ò»Ð©ÎÊÌâ²»ÈÝÒ×±©Â¶£¬µ«ÔÚ´óÐÍ»¥ÁªÍøÉÏÈ´»á³öÏÖ´íÎó¡£
¡¡¡¡UDPÐÒé°ÑµÝË͵Ŀɿ¿ÐÔÔðÈÎÍƵ½ÁËÉϲ㼴ӦÓò㣬ÏÂÃæ¼òµ¥±àдÁ˼¸¸öÀàÀ´×¨ÃÅ´¦ÀíÁ½¸öÎÊÌ⣺ÂÒÐòºÍ¶ª°ü¡£
¡¡¡¡ËĸöÀࣺDataPacket À࣬PacketHeaderÀ࣬PacketBodyÀà £¬DataEntryÀ࣬λÓÚͬһ¸öÎļþDataPacket .javaÖС£
¡¡¡¡DataPacket ÀàÏ൱ÓÚÒ»¸öÃÅÃæģʽ£¬Ìṩ¸øÍⲿʹÓã¬Í¨ÐÅÊý¾ÝÒ²ÔÚÕâ¸öÀàÖд¦Àí¡£
package com.skysoft.pcks;
import java.io.*;
import java.net.*;
import java.util.*;
public class DataPacket {
InputStream is;
OutputStream os;
PacketHeader header;
PacketBody body;
ArrayList al;
public static final int DataSwapSize = 64532;
/**
* ÔÚ½ÓÊÕÊý¾Ý±¨Ê¹ÓÃ
*/
public DataPacket() {
header = new PacketHeader();
body = new PacketBody();
al = new ArrayList();
}
/**
* ÔÚ·¢ËÍÊý¾Ý±¨Ê±Ê¹ÓÃ,Ëüµ÷Óñ¨ÎÄ·Ö¸î²Ù×÷.
* @param file String Ó²ÅÌÎļþ
*/
public DataPacket(String file) {
this();
try {
is = new FileInputStream(file);
header.CalcHeaderInfo(is.available());
this.madeBody();
is.close();
//this.Gereratedata();
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex1) {
ex1.printStackTrace();
}
}
/**
* ÔÚ·¢ËÍÊý¾Ý±¨Ê±Ê¹ÓÃ,Ëüµ÷Óñ¨ÎÄ·Ö¸î²Ù×÷.
* @param url URL urlµØÖ·
*/
public DataPacket(URL url) {
this();
try {
//is = url.openStream();
URLConnection conn=url.openConnection();
is=conn.getInputStream();
int total=conn.getContentLength();
header.CalcHeaderInfo(total);
this.madeBody();
//System.out.println(total+":"+total);
is.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* Ϊ·¢Ë͹¹Ôì·Ö×é,ʹÓÃPackageHeader´¦ÀíÁ˱¨Í·¸ñʽ,²¢Îª·Ö×é±àÐòºÅ.
*/
private void madeBody() {
al.clear();
byte[] buffer;
DataEntry de;
for (int i = 0; i < header.fragmentcounter; i++) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//is.skip(i * body.BODY_BUFFER_SIZE);
header.ArrageSort(i);
de = new DataEntry(PacketBody.BODY_BUFFER_SIZE);
de.setSn(i);
de.setStreamsize(header.getStreamsize());
de.setFragmentcounter(header.getFragmentcounter());
if (header.isWTailFragment(i)) {
buffer = new byte[header.getMinfragment()];
is.read(buffer, 0, buffer.length);
header.setActByteSize(header.getMinfragment());
de.setActByteSize(header.getMinfragment());
}
else {
buffer = new byte[body.BODY_BUFFER_SIZE];
is.read(buffer, 0, buffer.length);
}
//System.out.println("length-------"+i+" "+body.getBody().length+" "+header.getMinfragment());
body.setBody(buffer);
//System.out.println("length:" + i + " " + header.toString());
bos.write(header.getByte(), 0, header.HEADER_BUFFER_SIZE);
bos.write(body.getBody(), 0, body.getBody().length);
de.setBytes(bos.toByteArray());
al.add(de);
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
/**
* Ϊ·¢Ë͹¹Ôì·Ö×é,ûÓп¼ÂDZ¨Í·¸ñʽ,ҲûÓÐΪ·Ö×é±àÐòºÅ.
*/
private void madeBody1() {
al.clear();
for (int i = 0; i < header.fragmentcounter; i++) {
try {
if (header.isWTailFragment(i))
is.read(body.getBody(), i * body.BODY_BUFFER_SIZE,
header.getMinfragment());
else
is.read(body.getBody(), i * body.BODY_BUFFER_SIZE,
body.BODY_BUFFER_SIZE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(header.getByte(), 0, header.HEADER_BUFFER_SIZE);
bos.write(body.getBody(), header.HEADER_BUFFER_SIZE,
body.getBody().length);
al.add(bos);
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
/**
* ÔÚ½ÓÊÕµ½±¨Îĺó,¶Ô´Ë±¨ÎÄÖ´ÐÐ×é×°,²¢´¦Àí±¨ÎĶªÊ§ºÍÂÒÐòÇé¿ö.
* @param b1 byte[]
*/
public void Add(byte[] b1) {
byte[] buffer = (byte[]) b1.clone();
handlerText(buffer);
DataEntry de = new DataEntry(buffer, header.getActByteSize());
de.setSn(header.getSn());
de.setStreamsize(header.getStreamsize());
de.setFragmentcounter(header.getFragmentcounter());
al.add(de);
}
private void handlerText(byte[] buffer) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(buffer, 0, header.HEADER_BUFFER_SIZE);
byte[] b=new byte[header.HEADER_BUFFER_SIZE];
System.arraycopy(buffer,0,b,0,b.length);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
InputStreamReader isr = new InputStreamReader(bais);
BufferedReader br = new BufferedReader(isr);
try {
header = new PacketHeader(br.readLine());
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private String calFileSize(int size) {
return size / 1024 + "K";
}
public ArrayList getDataPackets() {
return al;
}
/**
* ÊÇ·ñ½ÓÊÕÍê±Ï,ͨ¹ýÐòºÅÊÇ·ñµÈÓÚ×î´ó¶ÎÊýÀ´ÅжÏ,ÕâÒ²ÐíÓÐÎÊÌâ,±ÈÈç,ÕýºÃÊÇ×îºóÒ»¸ö¶Î¶ªÊ§ÁË,ÕâÑù
* Õâ¸ö°üÕû¸ö¾Í¶ªÊ§ÁË.
* @return
*/
public boolean isFull() {
return this.header.getSn() == this.header.getFragmentcounter() - 1 ? true : false;
}
/**
* ÅжÏÊÇ·ñÖ»ÓÐÒ»¸ö¶Î.
* @return
*/
public boolean isZero() {
return this.header.getSn() == 0 ? true : false;
}
/**
* ¸Ãº¯ÊýÖ´Ðб¨ÎÄ×é×°,²»¿¼ÂǶªÊ§µÄ±¨ÎÄ.
* @return
*/
private ByteArrayOutputStream fetchDataPackets() {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = null;
DataEntry de;
for (int i = 0; i < al.size(); i++) {
try {
de = this.getSnData(i);
buffer = de.getByte();
if (header.getStreamsize() == de.getStreamsize()) {
bos.write(de.getByte(), header.HEADER_BUFFER_SIZE, de.getActByteSize());
System.out.println(de.toString() + " -- fetchDataPackets");
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
return bos;
}
/**
* ¸Ãº¯ÊýÖ´Ðб¨ÎÄ×é×°,¶ÔÓÚ¶ªÊ§µÄ±¨ÎÄ,дÈë¿Õ±¨ÎÄ.
* @return ByteArrayOutputStream
*/
private ByteArrayOutputStream fetchDataPackets_sn() {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer;
DataEntry de;
for (int i = 0; i < header.getFragmentcounter(); i++) {
try {
de = this.getSnData(i);
if (de == null) {
de = seachDeData(i);
}
buffer = de.getByte();
//System.out.println(de.getSn() + ":" + i);
//handlerText(buffer);
//bos.write(buffer, header.HEADER_BUFFER_SIZE,
// buffer.length - header.HEADER_BUFFER_SIZE);
if (header.getStreamsize() == de.getStreamsize()) {
bos.write(de.getByte(), header.HEADER_BUFFER_SIZE,
de.getActByteSize());
//System.out.println(de.toString());
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
return bos;
}
/**
* ¶Ô»º³åµÄÊý¾Ý°ü½øÐÐÅÅÐò´¦Àí,¼´°´Ë³ÐòÌáȡͬһ֡µÄÊý¾Ý,Èç¹ûûÓÐÕÒµ½¸ÃÐòºÅµÄÖ¡,Ôò·µ»Ø¿ÕÖµ.
* @param sn int ÒªÕÒµÄÖ¡ÐòºÅ.
* @return DataEntry
*/
private DataEntry getSnData(int sn) {
DataEntry de = null;
for (int i = 0; i < al.size(); i++) {
de = (DataEntry) al.get(i);
if (header.getStreamsize() == de.getStreamsize()) {
if (sn == de.getSn())
break;
else
de = null;
}
}
return de;
}
/**
* °´ÐòºÅ¿ªÊ¼ÏòÇ°»òÕßÊÇÏòºóÑ°ÕÒ×î½üµÄ֡Ƭ¶Î,ÈÕºó¿ÉÒÔÔö¼ÓÇëÇóÖØ·¢¹¦ÄÜ,ͨ¹ý¿ªÒ»¸öͨÐÅÁ¬½Ó.
* @param sn int
* @return DataEntry
*/
private DataEntry seachDeData(int sn) {
DataEntry de = null;
int initvalue, minvalue = 10000;
DataEntry back, fore = null;
for (int i = 0; i < al.size(); i++) {
de = (DataEntry) al.get(i);
if (header.getStreamsize() == de.getStreamsize()) {
initvalue = Math.abs(de.getSn() - sn);
if (de.getFragmentcounter() != de.getSn() && initvalue < minvalue) {
minvalue = initvalue;
fore = de;
}
}
}
return fore;
}
/**
* ³ýÈ¥×îºóÒ»Ö¡Íâ,Ëæ»ú³éÈ¡Ò»Ö¡.
* @return DataEntry
*/
private DataEntry seachDeData() {
DataEntry de = null;
for (int i = 0; i < al.size(); i++) {
de = (DataEntry) al.get(i);
System.out.println("sky ::::" + de.getFragmentcounter() + ":" + de.getSn() +
":" + i);
if (header.getStreamsize() == de.getStreamsize()) {
if (de.getFragmentcounter() != de.getSn()) {
break;
}
}
}
return de;
}
/**
* Éú³É×é×°ÍêµÄ½á¹ûÊý¾Ý.ÒòΪÓÃͼÏñÀ´×ö²âÊÔ,ËùÒÔÁîÆä·µ»ØͼÏñ.
* @return Image
*/
public java.awt.Image Gereratedata() {
ByteArrayInputStream bis;
java.awt.image.BufferedImage bimage = null;
try {
byte[] b = fetchDataPackets_sn().toByteArray();
//fetchDataPackets_old1()
bis = new ByteArrayInputStream(b);
bimage = javax.imageio.ImageIO.read(bis);
}
catch (Exception ex1) {
ex1.printStackTrace();
}
return bimage;
}
public static void main(String args[]) {
DataPacket dp = new DataPacket("e:\\nature\\14.jpg");
}
}
/**
* Êý¾ÝʵÌå,³äµ±ÁÙʱ´¦Àí³¡Ëù.
* @author Administrator
*
*/
class DataEntry {
byte[] bytes;
int fragmentcounter, sn, actbytesize;
long streamsize;
int minfragment;
public DataEntry() {
}
public DataEntry(int size) {
this.actbytesize = size;
}
public DataEntry(byte[] b, int i) {
this.bytes = b;
this.actbytesize = i;
}
public byte[] getByte() {
return this.bytes;
}
public void setBytes(byte[] b) {
this.bytes = b;
}
public void setStreamsize(long size) {
this.streamsize = size;
}
public long getStreamsize() {
return this.streamsize;
}
public int getMinfragment() {
return minfragment;
}
public synchronized void setSn(int i) {
this.sn = i;
}
public synchronized int getSn() {
return sn;
}
public synchronized int getFragmentcounter() {
return fragmentcounter;
}
public synchronized void setFragmentcounter(int c) {
this.fragmentcounter = c;
}
public void setActByteSize(int size) {
actbytesize = size;
}
public int getActByteSize() {
return actbytesize;
}
public String toString() {
return this.streamsize + "::" + this.fragmentcounter + "::" + this.sn +
"::" + this.actbytesize + " recv DataEntry";
}
}
/**
* ±¨Í·,´¦Àí±¨Í·¸ñʽ
* @author Administrator
*
*/
class PacketHeader implements Serializable{
public static final int HEADER_BUFFER_SIZE = 1024;
int fragmentcounter, sn;
int actbytesize = PacketBody.BODY_BUFFER_SIZE;
byte[] header; //= new byte[HEADER_BUFFER_SIZE];
long streamsize;
int minfragment;
public PacketHeader() {
}
public PacketHeader(long l) {
this.setStreamsize(l);
}
public PacketHeader(String s) {
String[] tm = s.split("::");
this.setActByteSize(Integer.parseInt(tm[3]));
this.setSn(Integer.parseInt(tm[2]));
this.setFragmentcounter(Integer.parseInt(tm[1]));
this.setStreamsize(Long.parseLong(tm[0]));
}
/**
* ¸ù¾ÝÎļþµÄ¶ÎµÄ˳ÐòÉú³ÉÊý¾ÝÍ·.
* @param sn ÎļþÐòÁÐ
*/
public void ArrageSort(int sn) {
this.setSn(sn);
this.setByte();
}
public void CalcHeaderInfo(long l) {
this.setStreamsize(l);
CalcHeaderInfo();
}
/**
* ¼ÆËãÁ÷Òª±»·Ö³ÉµÄƬ¶ÎÊýÁ¿,²¢µÃ³ö×îСƬ¶ÎÓàÁ¿.
*/
public void CalcHeaderInfo() {
fragmentcounter = Math.round( (float) streamsize /
PacketBody.BODY_BUFFER_SIZE);
float critical = (float) streamsize / PacketBody.BODY_BUFFER_SIZE;
if (critical - fragmentcounter < 0.5 && critical - fragmentcounter > 0)
fragmentcounter++;
minfragment = (int) (streamsize % PacketBody.BODY_BUFFER_SIZE);
}
public byte[] getHeader() {
Long it = new Long(this.streamsize);
return new byte[] {it.byteValue()};
}
public byte[] getByte() {
return header; //this.toString().getBytes();
}
/**
* Éú³É±¨Í·×Ö½Ú,Ê×ÏÈÈ¡µÃÊý¾Ý°üÍ· Á÷³ß´ç::¶ÎƬÊý::¶Î˳Ðò::¶Îʵ¼Ê³ß´ç µÄ×Ö½ÚÐÎʽ,
* È»ºó¼ÓÈë»Ø³µ»»ÐзûºÅ,¶ÔÓÚ1024×Ö½ÚÖÐÊ£ÓàµÄ²¿·ÖÒ»ÂÉдÈëÔªËØΪ0µÄ×Ö½ÚÊý×é.
*/
public void setByte() {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = this.toByte();
try {
bos.write(buffer);
bos.write("\r\n".getBytes());
bos.write(new byte[PacketHeader.HEADER_BUFFER_SIZE - buffer.length], 0,
PacketHeader.HEADER_BUFFER_SIZE - buffer.length);
header = bos.toByteArray();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
public void setStreamsize(long size) {
this.streamsize = size;
}
public long getStreamsize() {
return this.streamsize;
}
public int getMinfragment() {
return minfragment;
}
public synchronized void setSn(int i) {
this.sn = i;
}
public int getSn() {
return sn;
}
public int getFragmentcounter() {
return fragmentcounter;
}
public synchronized void setFragmentcounter(int c) {
this.fragmentcounter = c;
}
public void setActByteSize(int size) {
actbytesize = size;
setByte();
}
public int getActByteSize() {
return actbytesize;
}
/**
* Êý¾Ý°üÍ·µÄ¸ñʽΪ:Á÷³ß´ç::¶ÎƬÊý::¶Î˳Ðò::¶Îʵ¼Ê³ß´ç
* ±¨Í·×Ö½Ú³¤¶ÈÊǿɱ仯µÄ,±ÈÈç,¿ÉÒÔ¼ÓÈëÁ÷µÄ¾ßÌåÐÅÏ¢Èç:Á÷ËùÊôÎļþµÄÃû³Æ,ÎļþÀàÐÍÒÔ¼°Ò»Ð©ÆäËûÐÅÏ¢.
* @return String
*/
public String toString() {
return streamsize + "::" + this.fragmentcounter + "::" + this.getSn() +
"::" + this.getActByteSize();
}
public byte[] toByte() {
return this.toString().getBytes();
}
/**
* ÊÇ·ñΪβ¶Î
* @param i int
* @return boolean
*/
public boolean isWTailFragment(int i) {
return (i == fragmentcounter - 1) ? true : false;
}
}
/**
* Óû§Êý¾ÝÇø
* @author Administrator
*
*/
class PacketBody implements Serializable{
public static final int BODY_BUFFER_SIZE = 63508; //65508
byte[] body;
public PacketBody() {
}
public void setBody(byte[] b) {
this.body = b;
}
public byte[] getBody() {
return body;
}
}
¡¡¡¡Õâ¸öÊý¾Ý´¦ÀíÀ࣬½«ÔÚ½ÓÏÂÀ´Ê¹Óá£