1   package com.atlassian.security.auth.trustedapps;
2   
3   import org.bouncycastle.util.encoders.Base64;
4   
5   import java.io.UnsupportedEncodingException;
6   
7   /**
8    * Encode/decode byte[] to Strings.
9    */
10  interface Transcoder
11  {
12      String encode(byte[] data);
13  
14      byte[] decode(String encoded);
15  
16      byte[] getBytes(String data);
17  
18      /**
19       * Standard implemetation.
20       */
21      static class Base64Transcoder implements Transcoder
22      {
23          public String encode(byte[] data)
24          {
25              try
26              {
27                  return new String(Base64.encode(data), TrustedApplicationUtils.Constant.CHARSET_NAME);
28              }
29              // /CLOVER:OFF
30              catch (UnsupportedEncodingException e)
31              {
32                  throw new RuntimeException(e);
33              }
34              // /CLOVER:ON
35          }
36  
37          public byte[] decode(String encoded)
38          {
39              return decode(getBytes(encoded));
40          }
41  
42          byte[] decode(byte[] encoded)
43          {
44              return Base64.decode(encoded);
45          }
46  
47          public byte[] getBytes(String data)
48          {
49              try
50              {
51                  return data.getBytes(TrustedApplicationUtils.Constant.CHARSET_NAME);
52              }
53              // /CLOVER:OFF
54              catch (UnsupportedEncodingException e)
55              {
56                  throw new AssertionError(e);
57              }
58              // /CLOVER:ON
59          }
60      }
61  }