From a28ddedce70f5b8ab5f7e17a8258a0f56609cfc4 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Wed, 6 Dec 2017 02:10:31 +0000
Subject: [PATCH] Susimail: Move duplicated Encoding methods to the abstract
 class

---
 .../src/i2p/susi/webmail/encoding/Base64.java | 35 ----------------
 .../i2p/susi/webmail/encoding/EightBit.java   | 27 ++-----------
 .../i2p/susi/webmail/encoding/Encoding.java   | 40 +++++++++++++++++--
 .../src/i2p/susi/webmail/encoding/HTML.java   | 23 +----------
 .../i2p/susi/webmail/encoding/HeaderLine.java | 25 +-----------
 .../webmail/encoding/QuotedPrintable.java     | 26 ------------
 .../i2p/susi/webmail/encoding/SevenBit.java   | 29 --------------
 7 files changed, 41 insertions(+), 164 deletions(-)

diff --git a/apps/susimail/src/src/i2p/susi/webmail/encoding/Base64.java b/apps/susimail/src/src/i2p/susi/webmail/encoding/Base64.java
index e45642221a..0572ec4bfd 100644
--- a/apps/susimail/src/src/i2p/susi/webmail/encoding/Base64.java
+++ b/apps/susimail/src/src/i2p/susi/webmail/encoding/Base64.java
@@ -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 );
-	}
 }
diff --git a/apps/susimail/src/src/i2p/susi/webmail/encoding/EightBit.java b/apps/susimail/src/src/i2p/susi/webmail/encoding/EightBit.java
index bdb0eae278..b385aadd8d 100644
--- a/apps/susimail/src/src/i2p/susi/webmail/encoding/EightBit.java
+++ b/apps/susimail/src/src/i2p/susi/webmail/encoding/EightBit.java
@@ -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;
 	}
-
 }
diff --git a/apps/susimail/src/src/i2p/susi/webmail/encoding/Encoding.java b/apps/susimail/src/src/i2p/susi/webmail/encoding/Encoding.java
index 886d80466c..77123b062b 100644
--- a/apps/susimail/src/src/i2p/susi/webmail/encoding/Encoding.java
+++ b/apps/susimail/src/src/i2p/susi/webmail/encoding/Encoding.java
@@ -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);
+	}
 }
diff --git a/apps/susimail/src/src/i2p/susi/webmail/encoding/HTML.java b/apps/susimail/src/src/i2p/susi/webmail/encoding/HTML.java
index 969937cf99..1be6733ae9 100644
--- a/apps/susimail/src/src/i2p/susi/webmail/encoding/HTML.java
+++ b/apps/susimail/src/src/i2p/susi/webmail/encoding/HTML.java
@@ -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("&", "&amp;")  // 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");
-	}
-
 }
diff --git a/apps/susimail/src/src/i2p/susi/webmail/encoding/HeaderLine.java b/apps/susimail/src/src/i2p/susi/webmail/encoding/HeaderLine.java
index 7ab7b6f1a0..509d889d36 100644
--- a/apps/susimail/src/src/i2p/susi/webmail/encoding/HeaderLine.java
+++ b/apps/susimail/src/src/i2p/susi/webmail/encoding/HeaderLine.java
@@ -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 {
diff --git a/apps/susimail/src/src/i2p/susi/webmail/encoding/QuotedPrintable.java b/apps/susimail/src/src/i2p/susi/webmail/encoding/QuotedPrintable.java
index 78d1cf24a8..e4b769657d 100644
--- a/apps/susimail/src/src/i2p/susi/webmail/encoding/QuotedPrintable.java
+++ b/apps/susimail/src/src/i2p/susi/webmail/encoding/QuotedPrintable.java
@@ -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 );
-	}
 }
diff --git a/apps/susimail/src/src/i2p/susi/webmail/encoding/SevenBit.java b/apps/susimail/src/src/i2p/susi/webmail/encoding/SevenBit.java
index ee42e7831c..0cf84d063f 100644
--- a/apps/susimail/src/src/i2p/susi/webmail/encoding/SevenBit.java
+++ b/apps/susimail/src/src/i2p/susi/webmail/encoding/SevenBit.java
@@ -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 );
-	}
-
 }
-- 
GitLab