ネットワーク転送、byteへのデータ型変換の例


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class LearnInOutPutBuffer {

	ByteArrayOutputStream os = new ByteArrayOutputStream();

	public ByteArrayOutputStream writeOut(){
		byte[] bmap = new byte[250];
		for(int i=0; i<200; i++) bmap[i] = (byte) i;
		
		String name = "helloq";
		byte[] bname = name.getBytes();
		int i=200;
		for(; i<220 && i<200+bname.length; i++) 
			bmap[i] = bname[i-200];
		for(; i<220 ; i++) 
			bmap[i] = 0;
		
		int score = 250;
		byte[] bscore = intToByteArray1(score);
		for( i=220; i<224; i++)
			bmap[i] = bscore[i-220];
		
		byte state = 2;//running
		bmap[224] = state;
		
		try {
			os.write(bmap);
			os.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return os;		
	}

	public void readIn(ByteArrayInputStream is) {
		byte[] receive = new byte[225];
		int receiveLen = 0;
		
		try {
			receiveLen = is.read(receive);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println(" ----------------receiver len = " + receiveLen );
		
		//print len
		byte[] map = new byte[200];
		int i=0;
		for(; i<200; i++){
			map[i] = receive[i];
			System.out.println(" -- " + map[i]);
		}
		

		System.out.println(" ------------------------------------- " );
		
		String name = new String();
		byte[] bname = new byte[20];
		
		for(i=200; i<220 && receive[i]!=0; i++) 
			bname[i-200] = receive[i];
		name = bname.toString();

		System.out.println(" name = " + name );

		System.out.println(" ------------------------------------- " );
		
		byte[] bscore = new byte[4];
		for(i=220; i<224 ; i++) 
			bscore[i-220] = receive[i];
		int score = byteArrayToInt(bscore, 0);
		System.out.println("score = " + score);
		System.out.println(" ------------------------------------- " );
		
		byte state = receive[224];

		System.out.println(" state = " + state );
		System.out.println(" ------------------------------------- " );
		
	}

	public static void main(String args[]) {
		LearnInOutPutBuffer lb = new LearnInOutPutBuffer();
		lb.readIn(new ByteArrayInputStream(lb.writeOut().toByteArray()));
	}

	public static byte[] intToByteArray1(int i) {
		byte[] result = new byte[4];
		result[0] = (byte) ((i >> 24) & 0xFF);
		result[1] = (byte) ((i >> 16) & 0xFF);
		result[2] = (byte) ((i >> 8) & 0xFF);
		result[3] = (byte) (i & 0xFF);
		return result;
	}
/*
	public static byte[] intToByteArray2(int i) throws Exception {
		ByteArrayOutputStream buf = new ByteArrayOutputStream();
		DataOutputStream out = new DataOutputStream(buf);
		out.writeInt(i);
		byte[] b = buf.toByteArray();
		out.close();
		buf.close();
		return b;
	}
*/
	
	public static int byteArrayToInt(byte[] b, int offset) {
	       int value= 0;
	       for (int i = 0; i < 4; i++) {
	           int shift= (4 - 1 - i) * 8;
	           value +=(b[i + offset] & 0x000000FF) << shift;
	       }
	       return value;
	 }
}