1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */18package org.apache.giraph.utils;
1920import org.apache.giraph.writable.kryo.KryoWritableWrapper;
21import org.apache.hadoop.conf.Configuration;
2223/**24 * Utility methods for dealing with Hadoop configuration25 */26publicclassConfigurationObjectUtils {
27/** Hide constructor */28privateConfigurationObjectUtils() {
29 }
3031/**32 * Encode bytes to a hex String33 *34 * @param bytes byte[]35 * @return encoded String36 */37publicstatic String encodeBytes(byte[] bytes) {
38 StringBuilder strBuf = new StringBuilder();
39for (int i = 0; i < bytes.length; i++) {
40 strBuf.append((char) (((bytes[i] >> 4) & 0xF) + ('a')));
41 strBuf.append((char) (((bytes[i]) & 0xF) + ('a')));
42 }
43return strBuf.toString();
44 }
4546/**47 * Decode bytes from a hex String48 *49 * @param str String to decode50 * @return decoded byte[]51 */52publicstatic byte[] decodeBytes(String str) {
53 byte[] bytes = new byte[str.length() / 2];
54for (int i = 0; i < str.length(); i += 2) {
55char c = str.charAt(i);
56 bytes[i / 2] = (byte) ((c - 'a') << 4);
57 c = str.charAt(i + 1);
58 bytes[i / 2] += c - 'a';
59 }
60return bytes;
61 }
6263/**64 * Set byte array to a conf option65 *66 * @param data Byte array67 * @param confOption Conf option68 * @param conf Configuration69 */70publicstaticvoid setByteArray(byte[] data, String confOption,
71 Configuration conf) {
72 conf.set(confOption, encodeBytes(data));
73 }
7475/**76 * Get byte array from a conf option77 *78 * @param confOption Conf option79 * @param conf Configuration80 * @return Byte array81 */82publicstatic byte[] getByteArray(String confOption,
83 Configuration conf) {
84return decodeBytes(conf.get(confOption));
85 }
8687/**88 * Set object in a conf option using kryo89 *90 * @param object Object to set91 * @param confOption Conf option92 * @param conf Configuration93 * @param <T> Type of the object94 */95publicstatic <T> void setObjectKryo(T object, String confOption,
96 Configuration conf) {
97 setByteArray(WritableUtils.toByteArrayUnsafe(
98new KryoWritableWrapper<>(object)),
99 confOption, conf);
100 }
101102/**103 * Get object from a conf option using kryo104 *105 * @param confOption Conf option106 * @param conf Configuration107 * @return Object from conf108 * @param <T> Type of the object109 */110publicstatic <T> T getObjectKryo(String confOption,
111 Configuration conf) {
112 KryoWritableWrapper<T> wrapper = new KryoWritableWrapper<>();
113 WritableUtils.fromByteArrayUnsafe(
114 getByteArray(confOption, conf), wrapper,
115newUnsafeReusableByteArrayInput());
116return wrapper.get();
117 }
118 }