0

I'm working on a Java plug-in that takes two variables of bespoke type and returns one of the same type. This type can be convertet from and to InputStream. I will need to crop the first one at the end and the second one at the beginning and then merge the two before I return them. What is the best intermediate type to use here that will make all the croping and merging simple and easy to maintain? I don't want to go via string beacause I have tried that and it messed up the encoding.

Björn
  • 583
  • 5
  • 15

1 Answers1

0

After some more diffing around and testing I found a solution myself:

    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;
}

It uses a simple byte Array and then the Arrays and IOUtils classes does most of the heavy lifting.

Björn
  • 583
  • 5
  • 15