class RunLengthDecodeFilter extends Filter
{
    private static final int RUN_LENGTH_EOD = 128;

    @Override
    public DecodeResult decode(InputStream encoded, OutputStream decoded,
                                         COSDictionary parameters, int index) throws IOException
    {
        int dupAmount;
        byte[] buffer = new byte[128];
        while ((dupAmount = encoded.read()) != -1 && dupAmount != RUN_LENGTH_EOD)
        {
            if (dupAmount <= 127)
            {
                int amountToCopy = dupAmount + 1;
                int compressedRead;
                while (amountToCopy > 0)
                {
                    compressedRead = encoded.read(buffer, 0, amountToCopy);
                    // EOF reached?
                    if (compressedRead == -1)
                    {
                        break;
                    }
                    decoded.write(buffer, 0, compressedRead);
                    amountToCopy -= compressedRead;
                }
            }
            else
            {
                int dupByte = encoded.read();
                // EOF reached?
                if (dupByte == -1)
                {
                    break;
                }
                for (int i = 0; i < 257 - dupAmount; i++)
                {
                    decoded.write(dupByte);
                }
            }
        }
        return new DecodeResult(parameters);
    }

    @Override
    protected void encode(InputStream input, OutputStream encoded, COSDictionary parameters)
            throws IOException
    {<FILL_FUNCTION_BODY>}
}

        // Not used in PDFBox except for testing the decoder.
        int lastVal = -1;
        int byt;
        int count = 0;
        boolean equality = false;

        // buffer for "unequal" runs, size between 2 and 128
        byte[] buf = new byte[128];

        while ((byt = input.read()) != -1)
        {
            if (lastVal == -1)
            {
                // first time
                lastVal = byt;
                count = 1;
            }
            else
            {
                if (count == 128)
                {
                    if (equality)
                    {
                        // max length of equals
                        encoded.write(129); // = 257 - 128
                        encoded.write(lastVal);
                    }
                    else
                    {
                        // max length of unequals
                        encoded.write(127);
                        encoded.write(buf, 0, 128);
                    }
                    equality = false;
                    lastVal = byt;
                    count = 1;
                }
                else if (count == 1)
                {
                    if (byt == lastVal)
                    {
                        equality = true;
                    }
                    else
                    {
                        buf[0] = (byte) lastVal;
                        buf[1] = (byte) byt;
                        lastVal = byt;
                    }
                    count = 2;
                }
                else
                {
                    // 1 < count < 128
                    if (byt == lastVal)
                    {
                        if (equality)
                        {
                            ++count;
                        }
                        else
                        {
                            // write all we got except the last
                            encoded.write(count - 2);
                            encoded.write(buf, 0, count - 1);
                            count = 2;
                            equality = true;
                        }
                    }
                    else
                    {
                        if (equality)
                        {
                            // equality ends here
                            encoded.write(257 - count);
                            encoded.write(lastVal);
                            equality = false;
                            count = 1;
                        }
                        else
                        {
                            buf[count] = (byte) byt;
                            ++count;
                        }
                        lastVal = byt;
                    }
                }
            }
        }
        if (count > 0)
        {
            if (count == 1)
            {
                encoded.write(0);
                encoded.write(lastVal);
            }
            else if (equality)
            {
                encoded.write(257 - count);
                encoded.write(lastVal);
            }
            else
            {
                encoded.write(count - 1);
                encoded.write(buf, 0, count);
            }
        }
        encoded.write(RUN_LENGTH_EOD);
    
<methods>public abstract org.apache.pdfbox.filter.DecodeResult decode(java.io.InputStream, java.io.OutputStream, org.apache.pdfbox.cos.COSDictionary, int) throws java.io.IOException,public org.apache.pdfbox.filter.DecodeResult decode(java.io.InputStream, java.io.OutputStream, org.apache.pdfbox.cos.COSDictionary, int, org.apache.pdfbox.filter.DecodeOptions) throws java.io.IOException,public static org.apache.pdfbox.io.RandomAccessRead decode(java.io.InputStream, List<org.apache.pdfbox.filter.Filter>, org.apache.pdfbox.cos.COSDictionary, org.apache.pdfbox.filter.DecodeOptions, List<org.apache.pdfbox.filter.DecodeResult>) throws java.io.IOException,public final void encode(java.io.InputStream, java.io.OutputStream, org.apache.pdfbox.cos.COSDictionary, int) throws java.io.IOException,public static final javax.imageio.ImageReader findImageReader(java.lang.String, java.lang.String) throws org.apache.pdfbox.filter.MissingImageReaderException,public static int getCompressionLevel() <variables>private static final Logger LOG,public static final java.lang.String SYSPROP_DEFLATELEVEL