[
https://issues.apache.org/jira/browse/PDFBOX-4932?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17717728#comment-17717728
]
Tilman Hausherr commented on PDFBOX-4932:
-----------------------------------------
I asked chatGPT to refactor my code, but that refactoring failed the unit test.
Here's the code:
{code:java}
@Override
protected void encode(InputStream input, OutputStream encoded,
COSDictionary parameters)
throws IOException
{
int lastVal = -1;
int byt;
int count = 0;
boolean equality = false;
byte[] buf = new byte[128];
while ((byt = input.read()) != -1)
{
if (lastVal == -1)
{
lastVal = byt;
count = 1;
}
else
{
if (count == 128)
{
writeEncodedSegment(encoded, lastVal, count, buf, equality);
equality = false;
lastVal = byt;
count = 1;
}
else if (count == 1)
{
updateCountAndEqualityState(byt, lastVal, buf, count,
equality);
}
else
{
updateCountAndEqualityState(byt, lastVal, buf, count,
equality);
if (count == 128)
{
writeEncodedSegment(encoded, lastVal, count, buf,
equality);
equality = false;
count = 0;
}
}
}
}
if (count > 0)
{
if (count == 1)
{
writeSingleByte(encoded, lastVal);
}
else if (equality)
{
writeEncodedSegment(encoded, lastVal, count, buf, equality);
}
else
{
writeEncodedSegment(encoded, buf, count);
}
}
writeEndOfData(encoded);
}
private void updateCountAndEqualityState(int byt, int lastVal, byte[] buf,
int count, boolean equality) throws IOException
{
if (byt == lastVal)
{
updateEqualityState(equality, count);
}
else
{
updateUnequalityState(buf, count, lastVal);
}
buf[count] = (byte) byt;
count++;
}
private void updateEqualityState(boolean equality, int count)
{
if (equality)
{
count++;
}
else
{
equality = true;
}
}
private void updateUnequalityState(byte[] buf, int count, int lastVal)
{
buf[count] = (byte) lastVal;
count++;
}
private void writeEncodedSegment(OutputStream encoded, int lastVal, int
count, byte[] buf, boolean equality) throws IOException
{
if (equality)
{
encoded.write(129);
encoded.write(lastVal);
}
else
{
encoded.write(127);
encoded.write(buf, 0, 128);
}
}
private void writeEncodedSegment(OutputStream encoded, byte[] buf, int
count) throws IOException
{
encoded.write(count - 2);
encoded.write(buf, 0, count - 1);
}
private void writeSingleByte(OutputStream encoded, int lastVal) throws
IOException
{
encoded.write(0);
encoded.write(lastVal);
}
private void writeEndOfData(OutputStream encoded) throws IOException
{
encoded.write(RUN_LENGTH_EOD);
}
{code}
> Implement /RunLengthDecode encoder
> ----------------------------------
>
> Key: PDFBOX-4932
> URL: https://issues.apache.org/jira/browse/PDFBOX-4932
> Project: PDFBox
> Issue Type: Task
> Reporter: Tilman Hausherr
> Assignee: Tilman Hausherr
> Priority: Major
> Fix For: 3.0.0 PDFBox
>
>
> Implement the encoder to test the decoder to increase code coverage and as a
> brain exercise.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]