KLALB2.3
This commit is contained in:
@@ -1,9 +1,17 @@
|
||||
package org.kne.cloud.network.klalb;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutput;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.net.Inet6Address;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
public class ADDLINESPacket extends KLALBPacket {
|
||||
private String lines;
|
||||
@@ -18,43 +26,49 @@ public class ADDLINESPacket extends KLALBPacket {
|
||||
this.lines=lines;
|
||||
}
|
||||
|
||||
public ADDLINESPacket() {
|
||||
super(ADDLINES);
|
||||
}
|
||||
private int UTFlength(String str) {
|
||||
int strlen = str.length();
|
||||
int utflen = 0;
|
||||
for (int i = 0; i < strlen; i++) {
|
||||
int c = str.charAt(i);
|
||||
if ((c >= 0x0001) && (c <= 0x007F)) {
|
||||
utflen++;
|
||||
} else if (c > 0x07FF) {
|
||||
utflen += 3;
|
||||
} else {
|
||||
utflen += 2;
|
||||
}
|
||||
}
|
||||
return utflen;
|
||||
|
||||
public ADDLINESPacket(ByteBuffer bb) {
|
||||
super(bb);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ADDLINES\n"+lines;
|
||||
return "ADDLINES "+getLines();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected void writeToChannel(WritableByteChannel dto) throws IOException {
|
||||
byte[]bta=lines.getBytes(Charset.forName("UTF-8"));
|
||||
header.putChar(1,(char) bta.length);
|
||||
super.writeToChannel(dto);
|
||||
dto.write(ByteBuffer.wrap(bta));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void readFromChannel(ReadableByteChannel din) throws IOException {
|
||||
super.readFromChannel(din);
|
||||
int lth=header.getChar(1);
|
||||
byte[]b=new byte[lth];
|
||||
ByteBuffer wp= ByteBuffer.wrap(b);
|
||||
while(wp.hasRemaining()){
|
||||
if(din.read(wp)==-1) {
|
||||
throw new EOFException();
|
||||
}
|
||||
}
|
||||
lines=new String(b, Charset.forName("UTF-8"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getHeaderSize() {
|
||||
return super.getHeaderSize()+2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLength() {
|
||||
return super.getLength()+2+UTFlength(lines);
|
||||
return super.getLength()+2+lines.getBytes(Charset.forName("UTF-8")).length;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeToStream(DataOutput dto) throws IOException {
|
||||
super.writeToStream(dto);
|
||||
dto.writeUTF(lines);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void readFromStream(DataInput din) throws IOException {
|
||||
super.readFromStream(din);
|
||||
lines=din.readUTF();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user