|
34 | 34 | import com.google.common.base.Preconditions;
|
35 | 35 | import com.google.common.base.Throwables;
|
36 | 36 | import com.google.common.collect.ImmutableMap;
|
| 37 | +import io.netty.buffer.ByteBuf; |
37 | 38 | import io.netty.buffer.Unpooled;
|
38 | 39 | import io.netty.handler.codec.base64.Base64;
|
39 | 40 | import org.slf4j.Logger;
|
@@ -187,14 +188,31 @@ public void handle(Callback[] callbacks) throws IOException, UnsupportedCallback
|
187 | 188 | /* Encode a byte[] identifier as a Base64-encoded string. */
|
188 | 189 | public static String encodeIdentifier(String identifier) {
|
189 | 190 | Preconditions.checkNotNull(identifier, "User cannot be null if SASL is enabled");
|
190 |
| - return Base64.encode(Unpooled.wrappedBuffer(identifier.getBytes(StandardCharsets.UTF_8))) |
191 |
| - .toString(StandardCharsets.UTF_8); |
| 191 | + return getBase64EncodedString(identifier); |
192 | 192 | }
|
193 | 193 |
|
194 | 194 | /** Encode a password as a base64-encoded char[] array. */
|
195 | 195 | public static char[] encodePassword(String password) {
|
196 | 196 | Preconditions.checkNotNull(password, "Password cannot be null if SASL is enabled");
|
197 |
| - return Base64.encode(Unpooled.wrappedBuffer(password.getBytes(StandardCharsets.UTF_8))) |
198 |
| - .toString(StandardCharsets.UTF_8).toCharArray(); |
| 197 | + return getBase64EncodedString(password).toCharArray(); |
| 198 | + } |
| 199 | + |
| 200 | + /** Return a Base64-encoded string. */ |
| 201 | + private static String getBase64EncodedString(String str) { |
| 202 | + ByteBuf byteBuf = null; |
| 203 | + ByteBuf encodedByteBuf = null; |
| 204 | + try { |
| 205 | + byteBuf = Unpooled.wrappedBuffer(str.getBytes(StandardCharsets.UTF_8)); |
| 206 | + encodedByteBuf = Base64.encode(byteBuf); |
| 207 | + return encodedByteBuf.toString(StandardCharsets.UTF_8); |
| 208 | + } finally { |
| 209 | + // The release is called to suppress the memory leak error messages raised by netty. |
| 210 | + if (byteBuf != null) { |
| 211 | + byteBuf.release(); |
| 212 | + if (encodedByteBuf != null) { |
| 213 | + encodedByteBuf.release(); |
| 214 | + } |
| 215 | + } |
| 216 | + } |
199 | 217 | }
|
200 | 218 | }
|
0 commit comments