Need code for run length encoding(compression) in java


I need to use run length encoding for compressing data. For a given input byte stream, I need to compute the output by tagging each byte with the number of time the particular byte appears consecutively in the intput stream modulo 255. Examples:
Input                                Output
abbc                                        1a2b1c
a(repeated 500 times)      255a245a
Note that in the examples above, the number preceding the character is a byte, not multiple bytes. For example, when we write the output for the second input above, we will write four bytes to the output: the first byte with the value 255, the second byte with the value ord(a), the third byte with the value 245, and finally the fourth byte with the value ord(a). That is four bytes, and not 8 bytes like what it appears in the text representation.

sriramvemaraju2000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
Is this a homework ?
0
sriramvemaraju2000Author Commented:
Its a very minor part of my homework .... I need to complete that by today ...
0
sriramvemaraju2000Author Commented:
I think.. we can find that code easily because its a basic compression right?
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

TommySzalapskiCommented:
It would be easier to write the code yourself in this case given how simple it is. Here's the pseudo code to show how easy it is.

set index = 0
while index < size
{
  c = character at index
  set count = 1
  set go = true
  while go and index < size
  {
    look at character at index + count
    if it is the same as c, count ++ else go = false
  }
  encode count
  encode c
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sriramvemaraju2000Author Commented:
Its a basic algoirthm right.. can you provide me any good links for the implementation in java
0
for_yanCommented:
check this:

http://algs4.cs.princeton.edu/55compression/RunLength.java.html

/*************************************************************************
 *  Compilation:  javac RunLength.java
 *  Execution:    java RunLength - < input.txt   (compress)
 *  Execution:    java RunLength + < input.txt   (expand)
 *  Dependencies: BinaryIn.java BinaryOut.java
 *
 *  Compress or expand binary input from standard input using
 *  run-length encoding.
 *
 *  % java BinaryDump 40 < 4runs.bin 
 *  0000000000000001111111000000011111111111
 *  40 bits
 *
 *  This has runs of 15 0s, 7 1s, 7 0s, and 11 1s.
 *
 *  % java RunLength - < 4runs.bin | java HexDump
 *  0f 07 07 0b
 *  4 bytes
 *
 *************************************************************************/

public class RunLength {
    private static final int R   = 256;
    private static final int lgR = 8;

    public static void expand() { 
        boolean b = false; 
        while (!BinaryStdIn.isEmpty()) {
            int run = BinaryStdIn.readInt(lgR);
            for (int i = 0; i < run; i++)
                BinaryStdOut.write(b);
            b = !b;
        }
        BinaryStdOut.close();
    }

    public static void compress() { 
        char run = 0; 
        boolean old = false;
        while (!BinaryStdIn.isEmpty()) { 
            boolean b = BinaryStdIn.readBoolean();
            if (b != old) {
                BinaryStdOut.write(run, lgR);
                run = 1;
                old = !old;
            }
            else { 
                if (run == R-1) { 
                    BinaryStdOut.write(run, lgR);
                    run = 0;
                    BinaryStdOut.write(run, lgR);
                }
                run++;
            } 
        } 
        BinaryStdOut.write(run, lgR);
        BinaryStdOut.close();
    }


    public static void main(String[] args) {
        if      (args[0].equals("-")) compress();
        else if (args[0].equals("+")) expand();
        else throw new RuntimeException("Illegal command line argument");
    }

}

Open in new window

0
sriramvemaraju2000Author Commented:
didnt provided the complete code....
0
TommySzalapskiCommented:
for_yan did provide a complete code, and he really shouldn't have anyway since it's for homework.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.