我有一个String对象,现在因为它包含一个包含不同文件类型的文件夹的差异,而不是其中的所有内容都编码在同一个字符集中。
正确的代码在字符串中,但每当我尝试访问字符串时,groovy会尝试提供帮助并解码字符串,这会使事情变得混乱。
现在以下似乎做了我需要的事情
String decoded_diff = "String that contains codes from different character encodings"
patch_file_name = 'changes.patch'
patch_file = new File(pwd(), patch_file_name)
patch_file.delete()
max_block_size = 1024 * 1024
char[] char_buffer = new char[max_block_size]
block_start = 0
patch_length = decoded_diff.length()
while (true) {
block_size = Math.min(patch_length - block_start, max_block_size)
decoded_diff.getChars(block_start, block_start + block_size, char_buffer, 0)
block_start += block_size
byte[] byte_buffer = new byte[block_size]
for (int i = 0; i < block_size; i++) {
byte_buffer[i] = (int) char_buffer[i]
}
patch_file.append(byte_buffer)
if (block_start == patch_length) break
}
然而,它是sloooow
有没有更快的方法来实现同样的目标?最终的补丁文件必须与原始差异相同才能工作。不幸的是我无法发送文件本身(jenkins目前不支持管道作业中的文件参数)所以我必须将其转义并将其作为json参数列表的一部分发送,因此接收端的这种痛苦的严重性。
答案 0 :(得分:0)
为什么不:
String decoded_diff = "String that contains codes from different character encodings"
patch_file_name = 'changes.patch'
patch_file = new File(pwd(), patch_file_name)
patch_file.delete()
patchFile.withOutputStream { os ->
os << decoded_diff.bytes
}