0

Hi, i need a like bit of help on understanding how this code actually works.

this program basically encodes files, but can someone tell me what these three methods actually do in short detail?

import java.io.*;
import java.util.*;

public class BWTEncoder
{
	private int _blockSize;
	private byte _block[];
	private BZTableEntry _table[];
	private DataOutputStream _oos;
	
	public BWTEncoder(int blockSize, OutputStream out)
	{
		_blockSize = blockSize;
		_block = new byte[_blockSize];
		_oos = new DataOutputStream(out);
	}

	void encodeStream(InputStream input)
	{
		boolean more;
		try
		{
			do
			{
				more = processBlock(input);
				_oos.flush();
			} while(more);
			
		}
		catch(IOException e)
		{
			System.err.println("File Error");
			return;
		}
	}

	private boolean processBlock(InputStream f) throws IOException
	{
		int read, end;
		int first, last;
		
		read = f.read(_block,0,_blockSize);
		if(read > 0)
		{
			_table = new BZTableEntry[read];
			for(int i = 0; i < read; i++)
			{
				end = read - 1 - i;
				_table[i] = new BZTableEntry(end, read, _block);
			}
			
			Arrays.sort(_table, new BZTableEntryComparator());

			_oos.writeInt(read);

			first = last =0;
			for(int i = 0; i < read; i++)
			{
				if(_table[i].startPoint() == 0)
				{
					last = i;
				}
			}
			_oos.writeInt(last);
						
			for(int i = 0; i < read; i++)
			{
				_oos.write(_table[i].getAt(read-1));
			}
			
		}
		return (read == _block.length);
	}
}

thanks

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by javaAddict
0

If you don't know that the first method is the constructor and that it creates an array of bytes, then there is no point in continuing explaining when all you need to do is some studying on your own.

The code does what it does. It has for-loops, while-loops and it executes the commands one by one. Read the API of the classes that it has. Because the only thing we could do is repeat what the API says.

Example:

read = f.read(_block,0,_blockSize);

'f' is type InputStream from the method processBlock.
Look at the API of the InputStream and read what that method does. If we read the API and repeat the same thing will not do any difference. Only that we will be the ones spending our time searching the net.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.