java: uses bit shift operations instead of ByteBuffer.putXxx

This commit is contained in:
FURUHASHI Sadayuki 2010-11-30 21:55:45 +09:00
parent 706293aadc
commit 2c823f1aaa

View File

@ -55,7 +55,7 @@ public class Packer {
public static void load() { } public static void load() { }
protected byte[] castBytes = new byte[9]; protected byte[] castBytes = new byte[9];
protected ByteBuffer castBuffer = ByteBuffer.wrap(castBytes); //protected ByteBuffer castBuffer = ByteBuffer.wrap(castBytes);
protected OutputStream out; protected OutputStream out;
public Packer(OutputStream out) { public Packer(OutputStream out) {
@ -78,7 +78,9 @@ public class Packer {
if(d < -(1<<7)) { if(d < -(1<<7)) {
// signed 16 // signed 16
castBytes[0] = (byte)0xd1; castBytes[0] = (byte)0xd1;
castBuffer.putShort(1, d); // castBuffer.putShort(1, d);
castBytes[1] = (byte)(d >> 8);
castBytes[2] = (byte)(d >> 0);
out.write(castBytes, 0, 3); out.write(castBytes, 0, 3);
} else { } else {
// signed 8 // signed 8
@ -98,7 +100,9 @@ public class Packer {
} else { } else {
// unsigned 16 // unsigned 16
castBytes[0] = (byte)0xcd; castBytes[0] = (byte)0xcd;
castBuffer.putShort(1, d); // castBuffer.putShort(1, d);
castBytes[1] = (byte)(d >> 8);
castBytes[2] = (byte)(d >> 0);
out.write(castBytes, 0, 3); out.write(castBytes, 0, 3);
} }
} }
@ -110,12 +114,18 @@ public class Packer {
if(d < -(1<<15)) { if(d < -(1<<15)) {
// signed 32 // signed 32
castBytes[0] = (byte)0xd2; castBytes[0] = (byte)0xd2;
castBuffer.putInt(1, d); // castBuffer.putInt(1, d);
castBytes[1] = (byte)(d >> 24);
castBytes[2] = (byte)(d >> 16);
castBytes[3] = (byte)(d >> 8);
castBytes[4] = (byte)(d >> 0);
out.write(castBytes, 0, 5); out.write(castBytes, 0, 5);
} else if(d < -(1<<7)) { } else if(d < -(1<<7)) {
// signed 16 // signed 16
castBytes[0] = (byte)0xd1; castBytes[0] = (byte)0xd1;
castBuffer.putShort(1, (short)d); // castBuffer.putShort(1, (short)d);
castBytes[1] = (byte)(d >> 8);
castBytes[2] = (byte)(d >> 0);
out.write(castBytes, 0, 3); out.write(castBytes, 0, 3);
} else { } else {
// signed 8 // signed 8
@ -135,12 +145,18 @@ public class Packer {
} else if(d < (1<<16)) { } else if(d < (1<<16)) {
// unsigned 16 // unsigned 16
castBytes[0] = (byte)0xcd; castBytes[0] = (byte)0xcd;
castBuffer.putShort(1, (short)d); // castBuffer.putShort(1, (short)d);
castBytes[1] = (byte)(d >> 8);
castBytes[2] = (byte)(d >> 0);
out.write(castBytes, 0, 3); out.write(castBytes, 0, 3);
} else { } else {
// unsigned 32 // unsigned 32
castBytes[0] = (byte)0xce; castBytes[0] = (byte)0xce;
castBuffer.putInt(1, d); // castBuffer.putInt(1, d);
castBytes[1] = (byte)(d >> 24);
castBytes[2] = (byte)(d >> 16);
castBytes[3] = (byte)(d >> 8);
castBytes[4] = (byte)(d >> 0);
out.write(castBytes, 0, 5); out.write(castBytes, 0, 5);
} }
} }
@ -153,19 +169,33 @@ public class Packer {
if(d < -(1L<<31)) { if(d < -(1L<<31)) {
// signed 64 // signed 64
castBytes[0] = (byte)0xd3; castBytes[0] = (byte)0xd3;
castBuffer.putLong(1, d); // castBuffer.putLong(1, d);
castBytes[1] = (byte)(d >> 56);
castBytes[2] = (byte)(d >> 48);
castBytes[3] = (byte)(d >> 40);
castBytes[4] = (byte)(d >> 32);
castBytes[5] = (byte)(d >> 24);
castBytes[6] = (byte)(d >> 16);
castBytes[7] = (byte)(d >> 8);
castBytes[8] = (byte)(d >> 0);
out.write(castBytes, 0, 9); out.write(castBytes, 0, 9);
} else { } else {
// signed 32 // signed 32
castBytes[0] = (byte)0xd2; castBytes[0] = (byte)0xd2;
castBuffer.putInt(1, (int)d); // castBuffer.putInt(1, (int)d);
castBytes[1] = (byte)(d >> 24);
castBytes[2] = (byte)(d >> 16);
castBytes[3] = (byte)(d >> 8);
castBytes[4] = (byte)(d >> 0);
out.write(castBytes, 0, 5); out.write(castBytes, 0, 5);
} }
} else { } else {
if(d < -(1<<7)) { if(d < -(1<<7)) {
// signed 16 // signed 16
castBytes[0] = (byte)0xd1; castBytes[0] = (byte)0xd1;
castBuffer.putShort(1, (short)d); // castBuffer.putShort(1, (short)d);
castBytes[1] = (byte)(d >> 8);
castBytes[2] = (byte)(d >> 0);
out.write(castBytes, 0, 3); out.write(castBytes, 0, 3);
} else { } else {
// signed 8 // signed 8
@ -187,7 +217,9 @@ public class Packer {
} else { } else {
// unsigned 16 // unsigned 16
castBytes[0] = (byte)0xcd; castBytes[0] = (byte)0xcd;
castBuffer.putShort(1, (short)d); // castBuffer.putShort(1, (short)d);
castBytes[1] = (byte)((d & 0x0000ff00) >> 8);
castBytes[2] = (byte)((d & 0x000000ff) >> 0);
out.write(castBytes, 0, 3); out.write(castBytes, 0, 3);
//System.out.println("pack uint 16 "+(short)d); //System.out.println("pack uint 16 "+(short)d);
} }
@ -195,12 +227,24 @@ public class Packer {
if(d < (1L<<32)) { if(d < (1L<<32)) {
// unsigned 32 // unsigned 32
castBytes[0] = (byte)0xce; castBytes[0] = (byte)0xce;
castBuffer.putInt(1, (int)d); // castBuffer.putInt(1, (int)d);
castBytes[1] = (byte)((d & 0xff000000) >> 24);
castBytes[2] = (byte)((d & 0x00ff0000) >> 16);
castBytes[3] = (byte)((d & 0x0000ff00) >> 8);
castBytes[4] = (byte)((d & 0x000000ff) >> 0);
out.write(castBytes, 0, 5); out.write(castBytes, 0, 5);
} else { } else {
// unsigned 64 // unsigned 64
castBytes[0] = (byte)0xcf; castBytes[0] = (byte)0xcf;
castBuffer.putLong(1, d); // castBuffer.putLong(1, d);
castBytes[1] = (byte)(d >> 56);
castBytes[2] = (byte)(d >> 48);
castBytes[3] = (byte)(d >> 40);
castBytes[4] = (byte)(d >> 32);
castBytes[5] = (byte)(d >> 24);
castBytes[6] = (byte)(d >> 16);
castBytes[7] = (byte)(d >> 8);
castBytes[8] = (byte)(d >> 0);
out.write(castBytes, 0, 9); out.write(castBytes, 0, 9);
} }
} }
@ -222,7 +266,7 @@ public class Packer {
castBytes[6] = barray[barray.length-3]; castBytes[6] = barray[barray.length-3];
castBytes[7] = barray[barray.length-2]; castBytes[7] = barray[barray.length-2];
castBytes[8] = barray[barray.length-1]; castBytes[8] = barray[barray.length-1];
out.write(castBytes); out.write(castBytes, 0, 9);
return this; return this;
} else { } else {
throw new MessageTypeException("can't pack BigInteger larger than 0xffffffffffffffff"); throw new MessageTypeException("can't pack BigInteger larger than 0xffffffffffffffff");
@ -231,14 +275,28 @@ public class Packer {
public Packer packFloat(float d) throws IOException { public Packer packFloat(float d) throws IOException {
castBytes[0] = (byte)0xca; castBytes[0] = (byte)0xca;
castBuffer.putFloat(1, d); // castBuffer.putFloat(1, d);
int f = Float.floatToRawIntBits(d);
castBytes[1] = (byte)(f >> 24);
castBytes[2] = (byte)(f >> 16);
castBytes[3] = (byte)(f >> 8);
castBytes[4] = (byte)(f >> 0);
out.write(castBytes, 0, 5); out.write(castBytes, 0, 5);
return this; return this;
} }
public Packer packDouble(double d) throws IOException { public Packer packDouble(double d) throws IOException {
castBytes[0] = (byte)0xcb; castBytes[0] = (byte)0xcb;
castBuffer.putDouble(1, d); // castBuffer.putDouble(1, d);
long f = Double.doubleToRawLongBits(d);
castBytes[1] = (byte)(f >> 56);
castBytes[2] = (byte)(f >> 48);
castBytes[3] = (byte)(f >> 40);
castBytes[4] = (byte)(f >> 32);
castBytes[5] = (byte)(f >> 24);
castBytes[6] = (byte)(f >> 16);
castBytes[7] = (byte)(f >> 8);
castBytes[8] = (byte)(f >> 0);
out.write(castBytes, 0, 9); out.write(castBytes, 0, 9);
return this; return this;
} }
@ -268,11 +326,17 @@ public class Packer {
out.write((byte)d); out.write((byte)d);
} else if(n < 65536) { } else if(n < 65536) {
castBytes[0] = (byte)0xdc; castBytes[0] = (byte)0xdc;
castBuffer.putShort(1, (short)n); // castBuffer.putShort(1, (short)n);
castBytes[1] = (byte)(n >> 8);
castBytes[2] = (byte)(n >> 0);
out.write(castBytes, 0, 3); out.write(castBytes, 0, 3);
} else { } else {
castBytes[0] = (byte)0xdd; castBytes[0] = (byte)0xdd;
castBuffer.putInt(1, n); // castBuffer.putInt(1, n);
castBytes[1] = (byte)(n >> 24);
castBytes[2] = (byte)(n >> 16);
castBytes[3] = (byte)(n >> 8);
castBytes[4] = (byte)(n >> 0);
out.write(castBytes, 0, 5); out.write(castBytes, 0, 5);
} }
return this; return this;
@ -284,11 +348,17 @@ public class Packer {
out.write((byte)d); out.write((byte)d);
} else if(n < 65536) { } else if(n < 65536) {
castBytes[0] = (byte)0xde; castBytes[0] = (byte)0xde;
castBuffer.putShort(1, (short)n); // castBuffer.putShort(1, (short)n);
castBytes[1] = (byte)(n >> 8);
castBytes[2] = (byte)(n >> 0);
out.write(castBytes, 0, 3); out.write(castBytes, 0, 3);
} else { } else {
castBytes[0] = (byte)0xdf; castBytes[0] = (byte)0xdf;
castBuffer.putInt(1, n); // castBuffer.putInt(1, n);
castBytes[1] = (byte)(n >> 24);
castBytes[2] = (byte)(n >> 16);
castBytes[3] = (byte)(n >> 8);
castBytes[4] = (byte)(n >> 0);
out.write(castBytes, 0, 5); out.write(castBytes, 0, 5);
} }
return this; return this;
@ -300,11 +370,17 @@ public class Packer {
out.write((byte)d); out.write((byte)d);
} else if(n < 65536) { } else if(n < 65536) {
castBytes[0] = (byte)0xda; castBytes[0] = (byte)0xda;
castBuffer.putShort(1, (short)n); // castBuffer.putShort(1, (short)n);
castBytes[1] = (byte)(n >> 8);
castBytes[2] = (byte)(n >> 0);
out.write(castBytes, 0, 3); out.write(castBytes, 0, 3);
} else { } else {
castBytes[0] = (byte)0xdb; castBytes[0] = (byte)0xdb;
castBuffer.putInt(1, n); // castBuffer.putInt(1, n);
castBytes[1] = (byte)(n >> 24);
castBytes[2] = (byte)(n >> 16);
castBytes[3] = (byte)(n >> 8);
castBytes[4] = (byte)(n >> 0);
out.write(castBytes, 0, 5); out.write(castBytes, 0, 5);
} }
return this; return this;