1 package pursuer
.pxprpc
;
3 import java
.io
.EOFException
;
4 import java
.io
.IOException
;
5 import java
.nio
.Buffer
;
6 import java
.nio
.ByteBuffer
;
7 import java
.nio
.ByteOrder
;
8 import java
.nio
.channels
.ByteChannel
;
11 // read enough data or EOFException
12 public static void readf(ByteChannel in
,ByteBuffer b
) throws IOException
{
13 //java9 method signature change, which may cause error when runing on lower java version(usually happen on android).
15 for(b2
.mark();b2
.remaining()>0 && in
.isOpen();) {
17 throw new EOFException();
23 public static int readInt32(ByteChannel in
) throws IOException
{
24 ByteBuffer b
= ByteBuffer
.allocate(4);
26 return b
.order(ByteOrder
.LITTLE_ENDIAN
).getInt();
28 public static long readInt64(ByteChannel in
) throws IOException
{
29 ByteBuffer b
= ByteBuffer
.allocate(8);
31 return b
.order(ByteOrder
.LITTLE_ENDIAN
).getLong();
33 public static float readFloat32(ByteChannel in
) throws IOException
{
34 ByteBuffer b
= ByteBuffer
.allocate(4);
36 return b
.order(ByteOrder
.LITTLE_ENDIAN
).getFloat();
38 public static double readFloat64(ByteChannel in
) throws IOException
{
39 ByteBuffer b
= ByteBuffer
.allocate(8);
41 return b
.order(ByteOrder
.LITTLE_ENDIAN
).getDouble();
44 public static void writef(ByteChannel out
,ByteBuffer b
) throws IOException
{
47 b2
.remaining()>0 && out
.isOpen();
51 public static void writeInt32(ByteChannel out
,int d
) throws IOException
{
52 ByteBuffer b
=ByteBuffer
.allocate(4).order(ByteOrder
.LITTLE_ENDIAN
).putInt(d
);
56 public static void writeInt64(ByteChannel out
,long d
) throws IOException
{
57 ByteBuffer b
=ByteBuffer
.allocate(8).order(ByteOrder
.LITTLE_ENDIAN
).putLong(d
);
61 public static void writeFloat32(ByteChannel out
,float d
) throws IOException
{
62 ByteBuffer b
=ByteBuffer
.allocate(4).order(ByteOrder
.LITTLE_ENDIAN
).putFloat(d
);
66 public static void writeFloat64(ByteChannel out
,double d
) throws IOException
{
67 ByteBuffer b
=ByteBuffer
.allocate(8).order(ByteOrder
.LITTLE_ENDIAN
).putDouble(d
);
71 public static ByteBuffer
setPos(ByteBuffer bb
,int pos
){
76 public static int getPos(ByteBuffer bb
){
80 public static byte[] toBytes(ByteBuffer bb
){
81 byte[] b2
=new byte[bb
.remaining()];
85 public static ByteBuffer
ensureBuffer(ByteBuffer bb
,int remainSize
){
87 int size
=remainSize
+b
.position();
88 if(b
.capacity()<size
){
89 ByteBuffer newbuf
=ByteBuffer
.allocate(size
+(size
>>1));
90 newbuf
.put(newbuf
.array(),0,b
.position());
96 public static byte[] bytesGet(ByteBuffer bb
,int off
,int len
) {
99 bb2
.position(bb2
.position()+off
);
100 byte[] b
= new byte[len
];
105 public static void bytesSet(ByteBuffer bb
,int off
,int len
,byte[] b
) {
108 bb2
.position(bb2
.position()+off
);
112 public static String
stringJoin(String delim
,Iterable
<String
> iter
){
113 StringBuilder sb
=new StringBuilder();
118 return sb
.substring(0,sb
.length()-delim
.length());