001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.io.data.loading.impl; 020 021import org.apache.commons.codec.binary.Base64; 022import org.apache.reef.tang.annotations.Name; 023import org.apache.reef.tang.annotations.NamedParameter; 024 025import java.io.*; 026import java.util.Set; 027 028/** 029 * Serialize and deserialize {@link DistributedDataSetPartition} objects. 030 */ 031public final class DistributedDataSetPartitionSerializer { 032 033 public static String serialize(final DistributedDataSetPartition partition) { 034 try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { 035 final DataOutputStream daos = new DataOutputStream(baos); 036 daos.writeUTF(partition.getPath()); 037 daos.writeUTF(partition.getLocation()); 038 daos.writeInt(partition.getDesiredSplits()); 039 return Base64.encodeBase64String(baos.toByteArray()); 040 } catch (final IOException e) { 041 throw new RuntimeException("Unable to serialize distributed data partition", e); 042 } 043 } 044 045 public static DistributedDataSetPartition deserialize(final String serializedPartition) { 046 try (ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decodeBase64(serializedPartition))) { 047 final DataInputStream dais = new DataInputStream(bais); 048 return new DistributedDataSetPartition(dais.readUTF(), dais.readUTF(), dais.readInt()); 049 } catch (final IOException e) { 050 throw new RuntimeException("Unable to de-serialize distributed data partition", e); 051 } 052 } 053 054 /** 055 * Empty private constructor to prohibit instantiation of utility class. 056 */ 057 private DistributedDataSetPartitionSerializer() { 058 } 059 060 /** 061 * Allows to specify a set of distributed data set partitions. 062 */ 063 @NamedParameter(doc = "Sets of distributed data set partitions") 064 public static final class DistributedDataSetPartitions implements Name<Set<String>> { 065 } 066}