2015-12-12 125 views
-3

在java中是否有任何方法可以从byte []读取/写入整数,布尔值和字符串?从字节读取/写入[]

Like: 
write(50550); 
write(true); 
write("bla bla"); 

And then: 
int i = readInt(); 
int j = readBoolean(); 
String res = readUTF(); 

Walter和fge回答了两种不同的方法来实现这一点。唯一的问题仍然是:“哪种方式更快?”

+0

'字符串(字节[]字节,字符集的字符集)'从'字节转换[]'到'字符串' – Yoda

+2

@Yoda不!这将失去信息 – fge

+0

@fge他没有说明目的。好吧,好的。 – Yoda

回答

2
ByteBuffer buffer = ByteBuffer.allocate(0x1000); 
    buffer.order(ByteOrder.BIG_ENDIAN); 

    buffer.putInt(100); 
    //store booleans as 1 or 0 in a byte 
    buffer.put((byte) (true ? 1 : 0)); 
    buffer.put((byte) (false ? 1 : 0)); 

    //store string as [int length, byte array] 
    String str = "Hello World!"; 
    byte[] strBytes = str.getBytes(StandardCharsets.UTF_8); //use any charset you want here 
    buffer.putInt(strBytes.length); 
    buffer.put(strBytes); 


    buffer.flip(); 
    byte[] data = new byte[buffer.remaining()]; 
    buffer.get(data); 
    System.out.println("Total Bytes: " + data.length); 


    ByteBuffer bb = ByteBuffer.wrap(data); 

    System.out.println(bb.getInt()); 
    System.out.println(bb.get() != 0); 
    System.out.println(bb.get() != 0); 
    byte[] inStr = new byte[bb.getInt()]; 
    bb.get(inStr); 
    String myStr = new String(inStr, StandardCharsets.UTF_8); 
    System.out.println(myStr); 
+0

注意:OP要求Little Endian和UTF-16等,但我不知道你为什么得到了否决票。 –

4

你在找什么是DataInputStreamDataOutputStream

两个包裹在resp.an InputStreamOutputStream,可以读/写各种基本类型,“转化UTF”数据等

至于本身写入字节数组,你想用一个ByteArrayOutputStream和换行DataOutputStream了它:

final ByteArrayOutputStream out = new ByteArrayOutputStream(); 
final DataOutputStream dataOut = new DataOutputStream(out); 

// use dataOut; then: 

final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); 
final DataInputStream dataIn = new DataInputStream(in); 

// read back 

不管怎样,在您的意见之一,目前还不清楚你想要做什么;如果这只是二进制数据,则改为使用ByteBuffer;你可以在使用它之前随意更改它的字节顺序(默认情况下它是大端字节),将字节数组包裹在其中......等等,不同之处在于Java没有无符号的基本整数类型(好吧,除了char,一个是特殊的野兽)。总而言之,这看起来像一个XY问题,所以我建议你编辑你的问题,并完全解释你正在尝试做什么。

+0

非常感谢:) 这就是我一直在寻找! – Dragomirus

+0

好吧,但请阅读完整的问题。在我看来,你没有解释一切...... – fge

+0

Walter和fge回答了两种不同的方式来实现这一点。唯一的问题仍然是:“哪种方式更快?” – Dragomirus

0

您似乎在从DataInputStream和DataOutputStream进行搜索。

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
DataOutputStream dos = new DataOutputStream(baos); 
// Like: 
dos.writeInt(50550); 
dos.writeBoolean(true); 
dos.writeUTF("bla bla"); 
byte[] bytes = baos.toByteArray(); 

DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes)); 
// And then: 
int i = dis.readInt(); 
boolean flag = dis.readBoolean(); 
String res = dis.readUTF(); 

System.out.println("i: " + i + ", flag: " + flag + ", res: '" + res + "'"); 

打印

i: 50550, flag: true, res: 'bla bla' 

stdext :: writeULE16,stdext :: writeULE32,stdext :: writeULE64和stdext :: readULE16,stdext :: readULE32,stdext :: readULE64

这意味着你需要一个文本格式,在这种情况下,我建议使用PrintWriter

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, "UTF-16LE")); 
// Like: 
pw.println(50550); 
pw.println(true); 
pw.println("bla bla"); 
pw.close(); 
byte[] bytes = baos.toByteArray(); 

Scanner in = new Scanner(new InputStreamReader(new ByteArrayInputStream(bytes), "UTF-16LE")); 
// And then: 
int i = in.nextInt(); 
boolean flag = in.nextBoolean(); in.nextLine(); // discard the rest of the line 
String res = in.nextLine(); 

System.out.println("i: " + i + ", flag: " + flag + ", res: '" + res + "'"); 

打印相同,但底层字节将采用UTF-16LE编码。这种方法还支持UTF-16BE,UTF-32LE,UTF-32BE(和UTF-8)