编辑,合并并返回InputStream

时间:2017-06-22 07:00:38

标签: java livecycle printer-control-language

我正在研究一个Java插件,它接受两个bespoke type变量并返回一个相同类型的变量。此类型可以从和转换为InputStream。我需要在结束时裁剪第一个,在开始时裁剪第二个,然后在返回之前合并两个。这里使用的最佳中间类型是什么,这将使所有裁剪和合并简单易维护?我不想通过字符串,因为我已经尝试过,它搞砸了编码。

1 个答案:

答案 0 :(得分:0)

经过一些更加困难和测试后,我自己找到了一个解决方案:

    public Document concat(final Document base, final Document addOn) throws IOException
{
    // Convert Documents to InputStreams
    InputStream isBase = base.getInputStream();
    InputStream isAddOn = addOn.getInputStream();

    // Create new variable as base minus last 33 bytes
    int baseLength = isBase.available();
    byte[] baBase = IOUtils.toByteArray(isBase);
    byte[] baEndLessBase = Arrays.copyOf(baBase, baseLength-33);

    // Create new variable as addOn minus first 60 bytes
    int addOnLength = isAddOn.available();
    byte[] baAddOn = IOUtils.toByteArray(isAddOn);
    byte[] baHeadLessAddOn = Arrays.copyOfRange(baAddOn, 60, addOnLength);

    // Combine the two new variables
    byte[] baResult = new byte[baEndLessBase.length + baHeadLessAddOn.length];
    System.arraycopy(baEndLessBase, 0, baResult, 0, baEndLessBase.length);
    System.arraycopy(baHeadLessAddOn, 0, baResult, baEndLessBase.length, baHeadLessAddOn.length);

    // Debug
//        FileUtils.writeByteArrayToFile(new File("baEndLessBase.pcl"), baEndLessBase);
//        FileUtils.writeByteArrayToFile(new File("baHeadLessAddOn.pcl"), baHeadLessAddOn);
//        FileUtils.writeByteArrayToFile(new File("baResult.pcl"), baResult);

    // Convert to Document
    Document result = new Document(baResult);
    result.passivate();

    return result;
}

它使用一个简单的字节数组,然后Arrays和IOUtils类完成大部分繁重工作。