如何理解byte array
的定义长度?
例如,在这个例子中,我们定义了byte array
的长度为100。
如果要写入到byte array
中的数据长度超过了100字节
怎么办?
对于结果变量也是一样的。我不明白这些长度是如何工作的,如果您不知道数据有多大,如何选择适当长度的byte array
?
try {
// Encode a String into bytes
String inputString = "blahblahblah";
byte[] input = inputString.getBytes("UTF-8");
// Compress the bytes
**byte[] output = new byte[100];**
Deflater compresser = new Deflater();
compresser.setInput(input);
compresser.finish();
int compressedDataLength = compresser.deflate(output);
compresser.end();
// Decompress the bytes
Inflater decompresser = new Inflater();
decompresser.setInput(output, 0, compressedDataLength);
**byte[] result = new byte[100];**
int resultLength = decompresser.inflate(result);
decompresser.end();
// Decode the bytes into a String
String outputString = new String(result, 0, resultLength, "UTF-8");
} catch(java.io.UnsupportedEncodingException ex) {
// handle
} catch (java.util.zip.DataFormatException ex) {
// handle
}
对于这个例子,这里使用的字节数组作为输入实际上被称为缓冲区,我们如何理解它?