Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package AppPack;
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- /**
- *
- * @author Sara
- */
- public class LZ77 {
- //Compress method that take string data to work on, and returns a list of tags as a result of Compressing this data
- public List<LZ77Tupel> compress( String text )
- {
- //Create a list of typy "LZ77Tupel"
- List<LZ77Tupel> list = new ArrayList<LZ77Tupel>();
- //search variable to contain the history characters that occurs for the first time
- String search = "";
- // input variable now = the content of the text variable.
- //Remember that text variable contain the data to be compressed
- String input = text;
- // next variable works as a pointer that points to the next character in the buffer
- String next;
- int indexGood = 0;
- int indexFound;
- int searchLen;
- // while there is a data in the look ahead buffer keep compressing
- while ( input.length() > 0 )
- {
- searchLen = 1;
- // get the first character in the buffer using substring function
- next = input.substring( 0, searchLen );
- // check if the character is in your search history or not
- // in other words check if this is the first time that character happens or not
- if ( search.indexOf( next ) == -1 )
- {
- // Insert the tag values in the list
- list.add( new LZ77Tupel(0,0, next ) );
- System.out.println("tag "+"<0,"+"0,"+next+">");
- // add characters in your search history
- search += next;
- System.out.println("This is search history: "+search);
- // Remove the current character from you input data
- //Because you have finished using it
- input = input.substring( searchLen );
- // Continue to go back at the start of the while loop
- //dont continue in the do-while loop
- continue;
- }
- // When the last if condition is wrong
- //This means that the character you points to now is exist in your search history and used before
- //Go Inside Do-While loop
- do
- {
- next = input.substring( 0, searchLen );
- System.out.println("The current character now is: "+next);
- // Return the index of the current character from the search history
- indexFound = search.indexOf( next );
- System.out.println("the Current character"+next +"Was fount at position "+indexFound+"in the search history");
- if ( indexFound != -1 )
- indexGood = indexFound;
- // increase the search length by 1;
- searchLen++;
- System.out.println("search length now "+searchLen);
- }
- while ( indexFound != -1 );
- // Add the new characters in the search history
- search += next.substring( 0, next.length() );
- System.out.println("This is search history: "+search);
- // Remove the used characters from the input data.
- input = input.substring( next.length() );
- list.add( new LZ77Tupel( indexGood, next.length() - 1, next.substring( next.length() - 1 ) ) );
- System.out.println("tag < "+indexGood+","+(next.length()- 1)+","+next.substring(( next.length()- 1))+">");
- }
- // Return the List of tags
- return list;
- }
- public static void main(String[] args) throws FileNotFoundException, IOException {
- LZ77 lz77 = new LZ77();
- List<LZ77Tupel> list1 = lz77.compress( "abcaaabacaaaa" );
- System.out.println( "Compressed1: " + list1.size() + " tupels." );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement