/** * Base64-encode the given byte array to a String. * @param src the original byte array (may be {@code null}) * @return the encoded byte array as a UTF-8 String */ public static String encodeToString(byte[] src) { if (src.length == 0) { return ""; } return new String(encode(src), DEFAULT_CHARSET); }
@Test public void encode() throws UnsupportedEncodingException { byte[] bytes = new byte[] {-0x4f, 0xa, -0x73, -0x4f, 0x64, -0x20, 0x75, 0x41, 0x5, -0x49, -0x57, -0x65, -0x19, 0x2e, 0x3f, -0x1b}; assertArrayEquals(bytes, Base64Utils.decode(Base64Utils.encode(bytes))); bytes = "Hello World".getBytes("UTF-8"); assertArrayEquals(bytes, Base64Utils.decode(Base64Utils.encode(bytes))); bytes = "Hello World\r\nSecond Line".getBytes("UTF-8"); assertArrayEquals(bytes, Base64Utils.decode(Base64Utils.encode(bytes))); bytes = "Hello World\r\nSecond Line\r\n".getBytes("UTF-8"); assertArrayEquals(bytes, Base64Utils.decode(Base64Utils.encode(bytes))); bytes = new byte[] { (byte) 0xfb, (byte) 0xf0 }; assertArrayEquals("+/A=".getBytes(), Base64Utils.encode(bytes)); assertArrayEquals(bytes, Base64Utils.decode(Base64Utils.encode(bytes))); assertArrayEquals("-_A=".getBytes(), Base64Utils.encodeUrlSafe(bytes)); assertArrayEquals(bytes, Base64Utils.decodeUrlSafe(Base64Utils.encodeUrlSafe(bytes))); }
/** * Base64-encode the given byte array to a String. * @param src the original byte array (may be {@code null}) * @return the encoded byte array as a UTF-8 String */ public static String encodeToString(byte[] src) { if (src.length == 0) { return ""; } return new String(encode(src), DEFAULT_CHARSET); }
private void addAuthorizationToken(ConfigClientProperties configClientProperties, HttpHeaders httpHeaders, String username, String password) { String authorization = configClientProperties.getHeaders().get(AUTHORIZATION); if (password != null && authorization != null) { throw new IllegalStateException( "You must set either 'password' or 'authorization'"); } if (password != null) { byte[] token = Base64Utils.encode((username + ":" + password).getBytes()); httpHeaders.add("Authorization", "Basic " + new String(token)); } else if (authorization != null) { httpHeaders.add("Authorization", authorization); } }
@Override public ClientMetadata mapRow(ResultSet rs, int rowNum) throws SQLException { ClientMetadata clientMetadata = new ClientMetadata(); int pos = 1; clientMetadata.setClientId(rs.getString("client_id")); clientMetadata.setIdentityZoneId(rs.getString("identity_zone_id")); clientMetadata.setShowOnHomePage(rs.getBoolean("show_on_home_page")); try { clientMetadata.setAppLaunchUrl(new URL(rs.getString("app_launch_url"))); } catch (MalformedURLException mue) { // it is safe to ignore this as client_metadata rows are always created from a ClientMetadata instance whose launch url property is strongly typed to URL } byte[] iconBytes = rs.getBytes("app_icon"); if(iconBytes != null) { clientMetadata.setAppIcon(new String(Base64Utils.encode(iconBytes))); } clientMetadata.setCreatedBy(rs.getString("created_by")); String json = rs.getString("additional_information"); if (hasText(json)) { Map<String,Object> additionalInformation = JsonUtils.readValue(json, new TypeReference<Map<String,Object>>() {}); clientMetadata.setClientName((String)additionalInformation.get(CLIENT_NAME)); } return clientMetadata; } }
/** * Base64-encode the given byte array to a String. * @param src the original byte array (may be {@code null}) * @return the encoded byte array as a UTF-8 String */ public static String encodeToString(byte[] src) { if (src.length == 0) { return ""; } return new String(encode(src), DEFAULT_CHARSET); }
static String base64Encode(byte[] bytes) { try { return UTF8.newDecoder().decode(ByteBuffer.wrap(Base64Utils.encode(bytes))) .toString(); } catch (CharacterCodingException e) { throw new RuntimeException(e); } }
@Override public String encrypt(String text) { return new String(Base64Utils.encode(encrypt(text.getBytes(this.charset))), this.defaultCharset); }
@Override public String encrypt(String text) { return new String(Base64Utils.encode(encrypt(text.getBytes(this.charset))), this.defaultCharset); }
private void addAuthorizationToken(ConfigClientProperties configClientProperties, HttpHeaders httpHeaders, String username, String password) { String authorization = configClientProperties.getHeaders().get(AUTHORIZATION); if (password != null && authorization != null) { throw new IllegalStateException( "You must set either 'password' or 'authorization'"); } if (password != null) { byte[] token = Base64Utils.encode((username + ":" + password).getBytes()); httpHeaders.add("Authorization", "Basic " + new String(token)); } else if (authorization != null) { httpHeaders.add("Authorization", authorization); } }
byte[] credentialsEncoded = Base64Utils.encode( ( username + ":" + password ).getBytes() ); httpHeaders.add( HEADER_AUTHORIZATION, "Basic " + new String( credentialsEncoded ) );
protected String buildOriginatingIdentityHeader() throws JsonProcessingException { Map<String, Object> propMap = new HashMap<>(); propMap.put(ORIGINATING_USER_KEY, ORIGINATING_USER_VALUE); propMap.put(ORIGINATING_EMAIL_KEY, ORIGINATING_EMAIL_VALUE); ObjectMapper mapper = Jackson2ObjectMapperBuilder.json().build(); String properties = mapper.writeValueAsString(propMap); String encodedProperties = new String(Base64Utils.encode(properties.getBytes())); return ORIGINATING_IDENTITY_PLATFORM + " " + encodedProperties; }