I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit a28ddedc authored by zzz's avatar zzz
Browse files

Susimail: Move duplicated Encoding methods to the abstract class

parent e13ce467
No related branches found
No related tags found
No related merge requests found
......@@ -60,19 +60,6 @@ public class Base64 extends Encoding {
}
}
/**
* @see Base64#encode(byte[])
*/
public String encode(String str) throws EncodingException {
try {
StringWriter strBuf = new StringWriter();
encode(new ByteArrayInputStream(DataHelper.getUTF8(str)), strBuf);
return strBuf.toString();
}catch (IOException e) {
throw new EncodingException("encode error", e);
}
}
/**
* More efficient than super
*
......@@ -179,21 +166,6 @@ public class Base64 extends Encoding {
return b;
}
/**
* @param text
* @return Buffer containing a decoded String.
*/
public ReadBuffer decode(String text) throws DecodingException {
return text != null ? decode( DataHelper.getUTF8(text) ) : null;
}
/**
* @see Base64#decode(String)
*/
public ReadBuffer decode(byte[] in) throws DecodingException {
return decode( in, 0, in.length );
}
/**
* @see Base64#decode(String)
*/
......@@ -231,11 +203,4 @@ public class Base64 extends Encoding {
}
return new ReadBuffer(out, 0, written);
}
/*
* @see Base64#decode(String)
*/
public ReadBuffer decode(ReadBuffer in) throws DecodingException {
return decode( in.content, in.offset, in.length );
}
}
......@@ -46,20 +46,6 @@ public class EightBit extends Encoding {
throw new EncodingException("unsupported");
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#encode(java.lang.String)
*/
public String encode(String str) throws EncodingException {
throw new EncodingException("unsupported");
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(byte[])
*/
public ReadBuffer decode(byte[] in) throws DecodingException {
return decode( in, 0, in.length );
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(byte[], int, int)
*/
......@@ -68,18 +54,11 @@ public class EightBit extends Encoding {
return new ReadBuffer(in, offset, length);
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(java.lang.String)
*/
public ReadBuffer decode(String str) throws DecodingException {
return decode( DataHelper.getUTF8(str) );
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(i2p.susi.webmail.util.ReadBuffer)
/**
* @return in unchanged
*/
@Override
public ReadBuffer decode(ReadBuffer in) throws DecodingException {
return in;
}
}
......@@ -36,23 +36,35 @@ import net.i2p.data.DataHelper;
* Interface to encode/decode content transfer encodings like quoted-printable, base64 etc.
*
* @author susi
* @since 0.9.33 changed from interface to abstract class
*/
public abstract class Encoding {
public abstract String getName();
/**
* Encode a byte array to a ASCII or ISO-8859-1 String
*
* @param in
* @return Encoded string.
* @throws EncodingException
*/
public abstract String encode( byte in[] ) throws EncodingException;
/**
* Encode a (UTF-8) String to a ASCII or ISO-8859-1 String
*
* This implementation just converts the string to a byte array
* and then calls encode(byte[]).
* Most classes will not need to override.
*
* @param str
* @see Encoding#encode(byte[])
* @throws EncodingException
* @since 0.9.33 implementation moved from subclasses
*/
public abstract String encode( String str ) throws EncodingException;
public String encode(String str) throws EncodingException {
return encode(DataHelper.getUTF8(str));
}
/**
* This implementation just reads the whole stream into memory
......@@ -69,12 +81,18 @@ public abstract class Encoding {
}
/**
* This implementation just calls decode(in, 0, in.length).
* Most classes will not need to override.
*
* @param in
* @see Encoding#decode(byte[], int, int)
* @throws DecodingException
* @since 0.9.33 implementation moved from subclasses
*/
public abstract ReadBuffer decode( byte in[] ) throws DecodingException;
public ReadBuffer decode(byte in[]) throws DecodingException {
return decode(in, 0, in.length);
}
/**
*
* @param in
......@@ -84,18 +102,32 @@ public abstract class Encoding {
* @throws DecodingException
*/
public abstract ReadBuffer decode( byte in[], int offset, int length ) throws DecodingException;
/**
* This implementation just converts the string to a byte array
* and then calls encode(byte[]).
* Most classes will not need to override.
*
* @param str
* @return null if str is null
* @see Encoding#decode(byte[], int, int)
* @throws DecodingException
* @since 0.9.33 implementation moved from subclasses
*/
public abstract ReadBuffer decode( String str ) throws DecodingException;
public ReadBuffer decode(String str) throws DecodingException {
return str != null ? decode(DataHelper.getUTF8(str)) : null;
}
/**
* This implementation just calls decode(in.content, in.offset, in.length).
* Most classes will not need to override.
*
* @param in
* @see Encoding#decode(byte[], int, int)
* @throws DecodingException
* @since 0.9.33 implementation moved from subclasses
*/
public abstract ReadBuffer decode( ReadBuffer in ) throws DecodingException;
public ReadBuffer decode(ReadBuffer in) throws DecodingException {
return decode(in.content, in.offset, in.length);
}
}
......@@ -47,6 +47,7 @@ public class HTML extends Encoding {
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#encode(java.lang.String)
*/
@Override
public String encode(String str) throws EncodingException
{
return str.replace("&", "&") // must be first
......@@ -55,13 +56,6 @@ public class HTML extends Encoding {
.replaceAll( "\r{0,1}\n", "<br>\r\n" );
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(byte[])
*/
public ReadBuffer decode(byte[] in) throws DecodingException {
throw new UnsupportedOperationException();
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(byte[], int, int)
*/
......@@ -69,19 +63,4 @@ public class HTML extends Encoding {
throws DecodingException {
throw new DecodingException("unsupported");
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(java.lang.String)
*/
public ReadBuffer decode(String str) throws DecodingException {
throw new DecodingException("unsupported");
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(i2p.susi.webmail.util.ReadBuffer)
*/
public ReadBuffer decode(ReadBuffer in) throws DecodingException {
throw new DecodingException("unsupported");
}
}
......@@ -50,12 +50,7 @@ public class HeaderLine extends Encoding {
public String getName() {
return NAME;
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#encode(java.lang.String)
*/
public String encode(String text) throws EncodingException {
return encode( DataHelper.getUTF8(text) );
}
private static final int BUFSIZE = 2;
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#encode(byte[])
......@@ -153,13 +148,6 @@ public class HeaderLine extends Encoding {
return out.toString();
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(java.lang.String)
*/
public ReadBuffer decode( byte in[] ) throws DecodingException {
return decode( in, 0, in.length );
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(java.lang.String)
*/
......@@ -308,17 +296,6 @@ public class HeaderLine extends Encoding {
return new ReadBuffer(out.toByteArray(), 0, out.size());
}
public ReadBuffer decode(String text) throws DecodingException {
return text != null ? decode( DataHelper.getUTF8(text) ) : null;
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(i2p.susi.webmail.util.ReadBuffer)
*/
public ReadBuffer decode(ReadBuffer in) throws DecodingException {
return decode( in.content, in.offset, in.length );
}
/*****
TODO put UTF-8 back and move to a unit test
public static void main( String[] args ) throws EncodingException {
......
......@@ -46,12 +46,6 @@ public class QuotedPrintable extends Encoding {
public String getName() {
return "quoted-printable";
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#encode(java.lang.String)
*/
public String encode(String text) throws EncodingException {
return encode( DataHelper.getUTF8(text) );
}
private static int BUFSIZE = 2;
......@@ -148,19 +142,6 @@ public class QuotedPrintable extends Encoding {
}
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(java.lang.String)
*/
public ReadBuffer decode( byte in[] ) {
return decode( in, 0, in.length );
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(java.lang.String)
*/
public ReadBuffer decode(String text) {
return text != null ? decode( DataHelper.getUTF8(text) ) : null;
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(byte[], int, int)
*/
......@@ -216,11 +197,4 @@ public class QuotedPrintable extends Encoding {
return new ReadBuffer(out, 0, written);
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(i2p.susi.webmail.util.ReadBuffer)
*/
public ReadBuffer decode(ReadBuffer in) {
return decode( in.content, in.offset, in.length );
}
}
......@@ -46,20 +46,6 @@ public class SevenBit extends Encoding {
throw new EncodingException("unsupported");
}
/* (non-Javadoc)
* @see i2p.susi23.mail.encoding.Encoding#encode(java.lang.String)
*/
public String encode(String str) throws EncodingException {
throw new EncodingException("unsupported");
}
/* (non-Javadoc)
* @see i2p.susi23.mail.encoding.Encoding#decode(byte[])
*/
public ReadBuffer decode(byte[] in) throws DecodingException {
return decode( in, 0, in.length );
}
/* (non-Javadoc)
* @see i2p.susi23.mail.encoding.Encoding#decode(byte[], int, int)
*/
......@@ -79,19 +65,4 @@ public class SevenBit extends Encoding {
}
return new ReadBuffer(in, backupOffset, backupLength);
}
/* (non-Javadoc)
* @see i2p.susi23.mail.encoding.Encoding#decode(java.lang.String)
*/
public ReadBuffer decode(String str) throws DecodingException {
return decode( DataHelper.getUTF8(str) );
}
/* (non-Javadoc)
* @see i2p.susi23.mail.encoding.Encoding#decode(i2p.susi.webmail.util.ReadBuffer)
*/
public ReadBuffer decode(ReadBuffer in) throws DecodingException {
return decode( in.content, in.offset, in.length );
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment