Compress String with Run Length Encoding in Java

In this Java example, we’re gonna know how to compress String in Java with Run Length Encoding (RLE) method.

Related Posts:
Java Regular Expression Overview – Syntax
Java Regular Expression to extract data from a Text
Java Regular Expression to mask information from a Text

Run Length Encoding

– RLE is a very simple form of data compression.
– It produces sequences in which the same data values occurs in data elements are stored as a single data value and a counter. For example, AAABB => 3A2B.
– RLE is suitable to use for:
+ palette-based bitmap images such as computer icons or
+ black points collection on a solid white background for example:

run-length-encoding-java

Implementation

Encoding is very simple. For decoding, we use Regular Expression – Regex.


package com.grokonez.compression.rle;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class App {

	public static void main(String[] args) {
		String s = "AAABBBBBBCCDDDEEEEEE";
		
		String e = encode(s);
		String d = decode(e);
		
		System.out.println("Encode: " + e);
		System.out.println("Decode: " + d);
		System.out.println("Result: " + s.equals(d));
	}

	static String encode(String source) {
		StringBuffer stringBuffer = new StringBuffer();

		for (int i = 0; i < source.length(); i++) {
			int runLength = 1;

			while (i + 1 < source.length() && source.charAt(i) == source.charAt(i + 1)) {
				runLength++;
				i++;
			}

			stringBuffer.append(runLength); // AAA -> 3
			stringBuffer.append(source.charAt(i)); // A
		}

		return stringBuffer.toString();
	}

	static String decode(String source) {
		StringBuffer stringBuffer = new StringBuffer();

		Pattern pattern = Pattern.compile("[0-9]+|[a-zA-Z]");
		Matcher matcher = pattern.matcher(source);

		while (matcher.find()) {
			int num = Integer.parseInt(matcher.group());
			matcher.find();
			while (num-- != 0) {
				stringBuffer.append(matcher.group());
			}
		}

		return stringBuffer.toString();
	}
}

Result:


Encode: 3A6B2C3D6E
Decode: AAABBBBBBCCDDDEEEEEE
Result: true


By grokonez | March 24, 2018.

Last updated on May 7, 2021.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*