package com.example.hcig.asr; import java.io.FileInputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class test { public static int byteArrayToInt(byte[] b, int offset) { return b[3 + offset] & 0xFF | (b[2 + offset] & 0xFF) << 8 | (b[1 + offset] & 0xFF) << 16 | (b[0 + offset] & 0xFF) << 24; } public static void main(String [] args){ int type = 200; int len = 7788; try { InputStream is = new FileInputStream("1.wav"); len = is.available(); byte [] head = new byte[12]; byte [] buf = new byte[len]; is.read(buf, 0, len); is.close(); System.out.println(len); head[0] = (byte) ((type >> 24) & 0xff); head[1] = (byte) ((type >> 16) & 0xff); head[2] = (byte) ((type >> 8 ) & 0xff); head[3] = (byte) ((type ) & 0xff); head[4] = 0; head[5] = 0; head[6] = 0; head[7] = 0; head[8] = (byte) ((len >> 24) & 0xff); head[9] = (byte) ((len >> 16) & 0xff); head[10] = (byte) ((len >> 8) & 0xff); head[11] = (byte) ((len ) & 0xff); Socket s = new Socket("202.85.222.27", 11238); OutputStream os = s.getOutputStream(); os.write(head); os.write(buf); is = s.getInputStream(); boolean isHead = true; int offset = 0; while(true){ if(isHead){ int readLen = is.read(head, offset, 12 - offset); System.out.println("read head " + readLen); if(readLen <=0 ){ System.out.println("read head close"); s.close(); break; } offset += readLen; if(offset == 12){ isHead = false; offset = 0; len = byteArrayToInt(head, 8); if(len <= 0){ System.out.println("error head"); s.close(); break; } buf = new byte[len]; } }else{ int readLen = is.read(buf, offset, len - offset); System.out.println("read body " + readLen); if(readLen <=0 ){ System.out.println("read body close"); s.close(); break; } offset += readLen; if(offset == len){ System.out.println("recv str: " + new String(buf, "utf8")); s.close(); break; } } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
字节序
最新推荐文章于 2024-06-24 02:02:37 发布