org.springframework.data.redis.connection.util.DecodeUtils Maven / Gradle / Ivy
/*
* Copyright 2011-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.util;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.lang.Nullable;
/**
* Simple class containing various decoding utilities.
*
* @author Costin Leau
* @auhtor Christoph Strobl
* @auhtor Mark Paluch
*/
public abstract class DecodeUtils {
public static String decode(byte[] bytes) {
return new String(Base64.getDecoder().decode(bytes));
}
public static String[] decodeMultiple(byte[]... bytes) {
String[] result = new String[bytes.length];
for (int i = 0; i < bytes.length; i++) {
result[i] = decode(bytes[i]);
}
return result;
}
@Nullable
public static byte[] encode(@Nullable String string) {
return (string == null ? null : Base64.getEncoder().encode(string.getBytes()));
}
public static Map encodeMap(Map map) {
Map result = new LinkedHashMap<>(map.size());
for (Map.Entry entry : map.entrySet()) {
result.put(encode(entry.getKey()), entry.getValue());
}
return result;
}
public static Map decodeMap(Map tuple) {
Map result = new LinkedHashMap<>(tuple.size());
for (Map.Entry entry : tuple.entrySet()) {
result.put(decode(entry.getKey()), entry.getValue());
}
return result;
}
public static Set convertToSet(Collection keys) {
Set set = new LinkedHashSet<>(keys.size());
for (String string : keys) {
set.add(encode(string));
}
return set;
}
public static List convertToList(Collection keys) {
List set = new ArrayList<>(keys.size());
for (String string : keys) {
set.add(encode(string));
}
return set;
}
}