Java Concat和Read图像字节和字符串字节

Java Concat和Read图像字节和字符串字节,java,byte,Java,Byte,所以基本上我想把图像字节和字符串字节合并,当转换回字符串格式时,能够读取字符串字节。我无法读取字节的字符串部分。 我已经尝试过将字符串连接到字符串,它工作得很好,我认为它也可以用于图像和字符串,因为它是字节 BufferedImage img = ImageIO.read(new File("src/Client/sample img.jpg")); ImageIO.write(img , "jpg", baos); baos.flush(); String inform

所以基本上我想把图像字节和字符串字节合并,当转换回字符串格式时,能够读取字符串字节。我无法读取字节的字符串部分。 我已经尝试过将字符串连接到字符串,它工作得很好,我认为它也可以用于图像和字符串,因为它是字节

   BufferedImage img = ImageIO.read(new File("src/Client/sample img.jpg"));
   ImageIO.write(img , "jpg", baos);
   baos.flush();
   String information = "\"Image Information\"";
   byte[] buffer = baos.toByteArray();
   byte[] info = information.getBytes();
   byte[] concat = new byte[buffer.length + info.length];
   System.arraycopy(buffer, 0, concat, 0, buffer.length);
   System.arraycopy(info, 0, concat, buffer.length, info.length);
下面是如何读取字节是否为可读字符串

StringBuilder ret = new StringBuilder(); 
int i = 0; 
while (concat[i] != 0) 
{ 
    if((char) concat[i] == '"'){
         ret.append((char) concat[i]);
         i++;
         while ((char) concat[i] != '"') 
         {
             ret.append((char) concat[i]);
             i++;
         }
         break;
    }
    i++; 
} 
是的,有可能这样做吗?如果没有任何方法连接,我认为可以在数组开头添加1 int(4字节)。此int将存储图像数组的长度。您可以从bytearray读取前4个字节,识别图像bytearray大小(例如,N),然后读取N个图像字节,然后读取字符串字节

我认为结果应该是这样的:

byte[] buffer = baos.toByteArray();
byte[] info = information.getBytes();
byte[] length = intToBytes(buffer.length);
byte[] concat = new byte[length.length + buffer.length + info.length];
System.arraycopy(length, 0, concat, 0, buffer.length);
System.arraycopy(buffer, 0, concat, length.length, buffer.length);
System.arraycopy(info, 0, concat, buffer.length + length.length, info.length);
我认为可以在数组开头添加1 int(4字节)。此int将存储图像数组的长度。您可以从bytearray读取前4个字节,识别图像bytearray大小(例如,N),然后读取N个图像字节,然后读取字符串字节

我认为结果应该是这样的:

byte[] buffer = baos.toByteArray();
byte[] info = information.getBytes();
byte[] length = intToBytes(buffer.length);
byte[] concat = new byte[length.length + buffer.length + info.length];
System.arraycopy(length, 0, concat, 0, buffer.length);
System.arraycopy(buffer, 0, concat, length.length, buffer.length);
System.arraycopy(info, 0, concat, buffer.length + length.length, info.length);

解包是个问题,因为图像字节没有大小信息。 读取图像可能会在末尾删除字符串字节

Path imgPath = Paths.get("src/Client/sample img.jpg");

byte[] packed(Path imgPath, String information) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    //Path imgPath = Paths.get("src/Client/sample img.jpg");
    byte[] buffer = Files.readAllBytes(path);
    byte[] len = ByteBuffer.allocate(4).putInt(buffer.length).array());
    baos.write(len); // Would be nice to be able to find the information string.
    baos.write(buffer, 0, buffer.length);

    byte[] info = information.getBytes(StandardCharsets.UTF_8);
    baos.writer(buffer, info);

    byte[] concat = baos.toByteArray();
    return concat;
}

String unpackedInformation(byte[] packedBytes) {
    ByteBuffer bb = ByteBuffer.wrap(packedBytes);
    int imgLength = bb.getInt();

    byte[] info = Arrays.copyOfRange(packedBytes, Integer.SIZE + imgLength,
            packedBytes.length);
    return new String(info, StandardCharsets.UTF_8);
}

还可以将getBytes和new String与字符集一起使用,否则将使用默认值,这因平台而异。

问题在于解包,因为图像字节没有大小信息。 读取图像可能会在末尾删除字符串字节

Path imgPath = Paths.get("src/Client/sample img.jpg");

byte[] packed(Path imgPath, String information) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    //Path imgPath = Paths.get("src/Client/sample img.jpg");
    byte[] buffer = Files.readAllBytes(path);
    byte[] len = ByteBuffer.allocate(4).putInt(buffer.length).array());
    baos.write(len); // Would be nice to be able to find the information string.
    baos.write(buffer, 0, buffer.length);

    byte[] info = information.getBytes(StandardCharsets.UTF_8);
    baos.writer(buffer, info);

    byte[] concat = baos.toByteArray();
    return concat;
}

String unpackedInformation(byte[] packedBytes) {
    ByteBuffer bb = ByteBuffer.wrap(packedBytes);
    int imgLength = bb.getInt();

    byte[] info = Arrays.copyOfRange(packedBytes, Integer.SIZE + imgLength,
            packedBytes.length);
    return new String(info, StandardCharsets.UTF_8);
}

还可以使用getBytes和带有字符集的新字符串,否则会使用默认值,这在不同的平台上是不同的。

这似乎是一个非常简单的答案,我从未想过,现在必须尝试一下,感谢添加图像的长度+4个字节的
长度
值,这为我提供了如何获取图像和字符串数据的方法。谢谢这似乎是一个非常简单的答案,我从来没有想过,现在必须尝试一下,感谢添加图像的长度+4字节的
长度
值,这为我提供了如何获取图像和字符串的数据。谢谢是的,我只是注意到字符串被移动到字节的末尾,即使它是先被复制的。现在要检查你的答案了,谢谢。我刚才注意到字符串被移动到字节的末尾,即使它是先被复制的。现在要检查你的答案了,谢谢