0
private byte[] splitKeyAlgorithm(byte[] key) {
        var splitStream = 55;
        for (int i = 0, x = 0; i < key.length; i++, x++) {
            if (x == splitStream) {
                key[i] = '\n';
                x = -1;
            }
        }
        return key;
    }

I want to split array of bytes to print into this same length line by new line byte '\n'(splitStream). My question is: I have elegant splitted byte array on the screen but what does with element represented by key[i] ? Is overwritten by '\n'? Its impossible because i do:

Arrays.compare(original byte[], splitted byte[]);
Arrays.mismatch(original byte[], splitted byte[]);

Above codes return that this two arrays are equals and don't have any mismatch.

Edit:// Comparing looks like this:

Arrays.compare(List<ByteArrayOutputStream>outputStream.get(0).toByteArray(), primitive byte[] array);
1
  • It is overwritten with the char value of \n. If there was no \n at that place before (which it wasn't, because \n is 10 and not 55), the two arrays are not equal (unless they are both empty). Commented Nov 15, 2019 at 10:03

4 Answers 4

1

Yes, you are destroying your array now.

Instead you should make a new array with enough extra bytes for the inserted characters:

int splitStream=55;
byte[] copy=new byte[key.length+key.length/splitStream];
int kidx=0,ccount=0;
for(int i=0;i<copy.length;i++){
  if(ccount==splitStream){
    ccount=0;
    copy[i]='\n';
  } else {
    ccount++;
    copy[i]=key[kidx];
    kidx++;
  }
}
return copy;

... probably, but I have not tested it.

Now I did. Fixed the count-ccount typos, and there is a test here: https://ideone.com/efDvE2

Sign up to request clarification or add additional context in comments.

3 Comments

It have sense but both arrays are equals before and after splitting.
@Matthew see the full test on ideone.com/efDvE2, it does not seem to lose data (the entire sequence survives, plus the line breaks appear).
Yeah I saw it. Thanks but I can't find answer on my question. Are my arrays are equals: first - before splitting, and second - after splitting. Wtf?
0

You are overwriting the same array. Hence original == splitted.

private byte[] splitKeyAlgorithm(byte[] key) {
    var lineLength = 55;
    int copyLength = key.length + key.length / lineLength;
    byte[] copy = new byte[copyLength];
    int ci = 0;
    for (int i = 0; i < key.length; i += lineLength) {
        int i2 = Math.min(i + lineLength, key.length);
        System.arraycopy(key, i, copy, ci, i2 - i);
        ci += i2 - i;
        if (ci < copy.length) {
            copy[ci++] = '\n';
        }
    }
    return copy;
}

Comments

0

You are not inserting the '\n' into the array, you are overwriting whatever on key[i] was.

It could be, the Arrays.compare returns 0 (equal) because there where already '\n' on those positions?

1 Comment

It's impossible. Byte arrays passed to this method is random RSA algorithm encoded by BASE64.
0

Solved. Original array before splitting was overwritten and element byte[55] on original and new array = 10, so this is my '\n'. Confusion was by comparing which was performed after splitting and both object was identical. Good lesson for me. Thanks for all answers.

1 Comment

Good catch. To clarify, there is only one byte[] object here and both original and splitted are point to it. As a result the comparison will always match. You'll need to clone/copy the array object to perform a real before-to-after comparison.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.