Package: Base64Helper

Base64Helper

nameinstructionbranchcomplexitylinemethod
Base64Helper()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
decodeBytes(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
decodeString(String)
M: 31 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
discardWhitespace(byte[])
M: 60 C: 0
0%
M: 20 C: 0
0%
M: 11 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%
encodeBytes(byte[])
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
encodeString(String)
M: 21 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.debug
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.debug.core.dbgp.internal.utils;
23:
24: import java.io.UnsupportedEncodingException;
25:
26: import org.overture.ide.debug.core.VdmDebugPlugin;
27:
28: public class Base64Helper
29: {
30:
31:         /**
32:          * Encoding of the data
33:          */
34:         private static final String DATA_ENCODING = "UTF-8"; //$NON-NLS-1$
35:
36:         /**
37:          * Encoding of the base64 digits - to be used instead of the default encoding.
38:          */
39:         private static final String BYTE_ENCODING = "ISO-8859-1"; //$NON-NLS-1$
40:
41:         /**
42:          * Empty string constant
43:          */
44:         private static final String EMPTY = ""; //$NON-NLS-1$
45:
46:         public static String encodeString(String s)
47:         {
48:•                if (s != null && s.length() != 0)
49:                 {
50:                         try
51:                         {
52:                                 final byte[] encode = Base64.encode(s.getBytes(DATA_ENCODING));
53:                                 return new String(encode, BYTE_ENCODING);
54:                         } catch (UnsupportedEncodingException e)
55:                         {
56:                                 VdmDebugPlugin.log(e);
57:                         }
58:                 }
59:                 return EMPTY;
60:         }
61:
62:         public static String decodeString(String base64)
63:         {
64:•                if (base64 != null && base64.length() != 0)
65:                 {
66:                         try
67:                         {
68:                                 final byte[] bytes = base64.getBytes(BYTE_ENCODING);
69:                                 final int length = discardWhitespace(bytes);
70:•                                if (length > 0)
71:                                 {
72:                                         final int decodedLength = Base64.decodeInlplace(bytes, length);
73:                                         return new String(bytes, 0, decodedLength, DATA_ENCODING);
74:                                 }
75:                         } catch (UnsupportedEncodingException e)
76:                         {
77:                                 VdmDebugPlugin.log(e);
78:                         }
79:                 }
80:                 return EMPTY;
81:         }
82:
83:         /**
84:          * Discards any whitespace from a base-64 encoded block. The base64 data in responses could be chunked in the
85:          * multiple lines, so we need to remove extra whitespaces. The bytes are copied in-place and the length of the
86:          * actual data bytes is returned.
87:          *
88:          * @param bytes
89:          * @return
90:          */
91:         private static int discardWhitespace(byte[] data)
92:         {
93:                 final int length = data.length;
94:                 int i = 0;
95:•                while (i < length)
96:                 {
97:                         byte c = data[i++];
98:•                        if (c == (byte) ' ' || c == (byte) '\n' || c == (byte) '\r'
99:•                                        || c == (byte) '\t')
100:                         {
101:                                 int count = i - 1;
102:•                                while (i < length)
103:                                 {
104:                                         c = data[i++];
105:•                                        if (c != (byte) ' ' && c != (byte) '\n' && c != (byte) '\r'
106:•                                                        && c != (byte) '\t')
107:                                         {
108:                                                 data[count++] = c;
109:                                         }
110:                                 }
111:                                 return count;
112:                         }
113:                 }
114:                 return length;
115:         }
116:
117:         public static String encodeBytes(byte[] bytes)
118:         {
119:                 return new String(Base64.encode(bytes));
120:         }
121:
122:         public static byte[] decodeBytes(String base64)
123:         {
124:                 return Base64.decode(base64.getBytes());
125:         }
126: }