org.yenc
Class YEncDecoder

java.lang.Object
  |
  +--org.yenc.YEncDecoder

public class YEncDecoder
extends java.lang.Object

Decodes data encoded with yEnc (http://www.yencode.org). It supports both single and multipart archives. The method to decode the files is the following:

For single part archives:

  1. Set the input stream using setInputStream(InputStream).
  2. Set the output stream using setOutputStream(OutputStream).
  3. Call decode().
(Note: It doesn't matter if step 1 or 2 is done first, but both must be done before step 3).

For multi part archives:

  1. Set the output stream using setOutputStream(OutputStream).
  2. Set the input stream for the next part setInputStream(InputStream, false).
  3. Call decode().
  4. If there are more parts left go to step 2.
What's missing in the last steps is to take all the files that make the multipart archive and put them in the correct order. Code to do this and also to make some integrity tests on each part can be found in org.yenc.ydecode.sortFiles(YEncDecoder, String[]).

Note that this class is not synchronized.



BUGS:

Author:
Luis Parravicini
See Also:
yEncode, setInputStream(InputStream), setInputStream(InputStream, boolean), setOutputStream(OutputStream), decode(), ydecode.sortFiles(YEncDecoder, String[])

Field Summary
 int MAX_LENGTH
          Maximum line length allowed
 
Constructor Summary
YEncDecoder()
          Creates a new instance which can read lines up to 1024 characters of length.
YEncDecoder(int maxlength)
          Creates a new instance which can read lines up to maxlength characters of length.
 
Method Summary
 boolean debugMessages()
          Returns if debug messages are enabled or not.
 void debugMessages(boolean enable)
          Enables or disables debugging messages to System.out.
 void decode()
          Read encoded data from the stream set with setInputStream and writes decoded data to the stream set with setOutputStream.
protected  boolean decodeNext()
          Decodes the next character.
 java.lang.String getFileName()
          Returns the filename set in the header.
 int getLineNumber()
          Returns the actual line number.
 int getPartBegin()
          Returns the beginning offset of the current part.
 int getPartEnd()
          Returns the ending offset of the current part.
 int getPartNumber()
          Returns the current part number set in the header.
protected  boolean getProperty(java.lang.String s)
           
 long getSize()
          Returns the file size set in the header.
 int getTotalParts()
          Returns the total parts number set in the header.
 boolean isMultiPart()
          Returns true if the current input stream is a multipart archive.
protected  void processHeader(java.util.StringTokenizer tokenizer)
           
protected  void processHeaderArg(java.util.StringTokenizer tokenizer)
          Process the next parameter from the header.
protected  void processPart(java.util.StringTokenizer tokenizer)
           
protected  void processPartArg()
          Process the next parameter from the part header.
protected  void processTrailer(java.util.StringTokenizer tokenizer)
           
protected  void processTrailerArg()
          Process the next parameter from the trailer.
protected  boolean readNextLine(boolean skip)
          Reads the next line from the input stream.
 void reset()
          Resets the instance internal state.
 void reset(boolean resetcrc)
          Resets the instance internal state.
 void setInputStream(java.io.InputStream in)
          Sets the input stream from which encoded data is read.
 void setInputStream(java.io.InputStream in, boolean isNextPart)
          Sets the input stream from which encoded data is read.
 void setOutputStream(java.io.OutputStream out)
          Sets the input stream to which decoded data is written.
protected  java.lang.Integer strToInt(java.lang.String s, java.lang.String name)
          Converts the string s to an Integer.
protected  java.lang.Long strToLong(java.lang.String s, int radix, java.lang.String name)
          Converts the string s to a Long in the specified radix.
protected  java.lang.Long strToLong(java.lang.String s, java.lang.String name)
          Calls strToLong() with radix equal to 10.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_LENGTH

public int MAX_LENGTH
Maximum line length allowed
Constructor Detail

YEncDecoder

public YEncDecoder()
Creates a new instance which can read lines up to 1024 characters of length.

YEncDecoder

public YEncDecoder(int maxlength)
Creates a new instance which can read lines up to maxlength characters of length.
Method Detail

reset

public void reset()
Resets the instance internal state. It calls reset(true).
See Also:
reset(boolean)

reset

public void reset(boolean resetcrc)
Resets the instance internal state. This is used to be able to decode several files withouth the need of creating more instances. If resetcrc is true, then the CRC computed so far is also reset.

getProperty

protected boolean getProperty(java.lang.String s)

strToInt

protected java.lang.Integer strToInt(java.lang.String s,
                                     java.lang.String name)
                              throws InvalidParameterException
Converts the string s to an Integer. In case an error occurs, a YEncException will be thrown using name as part of the exception message.

strToLong

protected java.lang.Long strToLong(java.lang.String s,
                                   java.lang.String name)
                            throws InvalidParameterException
Calls strToLong() with radix equal to 10.
See Also:
strToLong(String, int, String)

strToLong

protected java.lang.Long strToLong(java.lang.String s,
                                   int radix,
                                   java.lang.String name)
                            throws InvalidParameterException
Converts the string s to a Long in the specified radix. In case an error occurs, a YEncException will be thrown using name as part of the exception message.

processHeaderArg

protected void processHeaderArg(java.util.StringTokenizer tokenizer)
                         throws InvalidParameterException
Process the next parameter from the header. A call to getProperty(String) must be made before calling this method. The tokenizer is needed as the filename parameter eats up everything from the parameter definition till the end of the line.

processHeader

protected void processHeader(java.util.StringTokenizer tokenizer)
                      throws InvalidParameterException,
                             MissingParameterException

processTrailerArg

protected void processTrailerArg()
                          throws InvalidParameterException
Process the next parameter from the trailer. A call to getProperty(String) must be made before calling this method.

processTrailer

protected void processTrailer(java.util.StringTokenizer tokenizer)
                       throws InvalidParameterException,
                              MissingParameterException

processPartArg

protected void processPartArg()
                       throws InvalidParameterException
Process the next parameter from the part header. A call to getProperty(String) must be made before calling this method.

processPart

protected void processPart(java.util.StringTokenizer tokenizer)
                    throws java.io.IOException,
                           InvalidParameterException,
                           MissingParameterException

readNextLine

protected boolean readNextLine(boolean skip)
                        throws java.io.IOException,
                               YEncException
Reads the next line from the input stream. Maximum line length acepted is MAX_LENGTH bytes. The line terminator is "CR*LF" (actually, the CRs are ignored and LF is the line terminator).
If skip is true, it returns a line with encoded data or with a header, everything else is skipped. The skipped lines are not subject to the maximum line length mentioned above; i.e., all headers and encoded data lines' length must be less than MAX_LENGTH but the rest of the lines can be of any length.
Note that if before the encoded data there is text that starts like a yEnc header ("=y"), then that line will have to conform to the requeriment mentionend before.
See Also:
MAX_LENGTH

decodeNext

protected boolean decodeNext()
                      throws java.io.IOException,
                             YEncException
Decodes the next character. Returns false when EOF is found.

decode

public void decode()
            throws java.io.IOException,
                   YEncException
Read encoded data from the stream set with setInputStream and writes decoded data to the stream set with setOutputStream. If you haven't used those two methods mentioned to set the input/output streams before calling this method, it will fail.
See Also:
setInputStream(InputStream), setOutputStream(OutputStream)

debugMessages

public void debugMessages(boolean enable)
Enables or disables debugging messages to System.out.

debugMessages

public boolean debugMessages()
Returns if debug messages are enabled or not.

getFileName

public java.lang.String getFileName()
                             throws java.io.IOException,
                                    YEncException
Returns the filename set in the header. If the filename is not yet known, it scans the stream until it finds the header. If no headers are found, returns null.

getSize

public long getSize()
             throws java.io.IOException,
                    YEncException
Returns the file size set in the header. If the file size is not yet known, it scans the stream until it finds the header. If no headers are found returns -1.

getPartNumber

public int getPartNumber()
                  throws java.io.IOException,
                         YEncException
Returns the current part number set in the header. If the part number is not yet known, it scans the stream until it finds the header. If no headers are found or no part number is set, returns -1.

getTotalParts

public int getTotalParts()
                  throws java.io.IOException,
                         YEncException
Returns the total parts number set in the header. If the total parts are not yet known, it scans the stream until it finds the header. If no headers are found or no total parts is set, returns -1.

isMultiPart

public boolean isMultiPart()
                    throws java.io.IOException,
                           YEncException
Returns true if the current input stream is a multipart archive.

getPartBegin

public int getPartBegin()
                 throws java.io.IOException,
                        YEncException
Returns the beginning offset of the current part. If the offset is not yet known, it scans the stream until it finds the part header. If no part header is found returns -1.

getPartEnd

public int getPartEnd()
               throws java.io.IOException,
                      YEncException
Returns the ending offset of the current part. If the offset is not yet known, it scans the stream until it finds the part header. If no part header is found returns -1.

getLineNumber

public int getLineNumber()
Returns the actual line number. It's also the number of lines read from the input stream.

setInputStream

public void setInputStream(java.io.InputStream in)
Sets the input stream from which encoded data is read. Setting the input stream resets the instance internal state. It calls setInputStream(InputStream, false).
See Also:
setInputStream(InputStream, boolean)

setInputStream

public void setInputStream(java.io.InputStream in,
                           boolean isNextPart)
Sets the input stream from which encoded data is read. isNextPart determines whether the new input stream is a new archive or the next part for the previous stream.
You may want to use some kind of buffered input for better performance.

setOutputStream

public void setOutputStream(java.io.OutputStream out)
Sets the input stream to which decoded data is written.