This project has retired. For details please refer to its Attic page.
Source code
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 */
019
020package org.apache.reef.javabridge.utils;
021
022import org.apache.avro.io.*;
023import org.apache.avro.specific.SpecificDatumReader;
024import org.apache.avro.specific.SpecificDatumWriter;
025import org.apache.reef.javabridge.avro.DefinedRuntimes;
026
027import java.io.ByteArrayInputStream;
028import java.io.ByteArrayOutputStream;
029import java.io.IOException;
030import java.io.InputStream;
031
032/**
033 * Serializer for MultiRuntimeDefinition.
034 */
035public final class DefinedRuntimesSerializer {
036
037  private static final String CHARSET_NAME = "UTF-8";
038
039  /**
040   * Serializes DefinedRuntimes.
041   * @param definedRuntimes the Avro object to toString
042   * @return Serialized avro string
043   */
044  public byte[] toBytes(final DefinedRuntimes definedRuntimes){
045    final DatumWriter<DefinedRuntimes> configurationWriter =
046            new SpecificDatumWriter<>(DefinedRuntimes.class);
047    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
048      final BinaryEncoder binaryEncoder = EncoderFactory.get().binaryEncoder(out, null);
049      configurationWriter.write(definedRuntimes, binaryEncoder);
050      binaryEncoder.flush();
051      out.flush();
052      return out.toByteArray();
053    } catch (final IOException e) {
054      throw new RuntimeException("Unable to serialize DefinedRuntimes", e);
055    }
056  }
057
058   /**
059   * Deserializes avro definition.
060   * @param serializedDefinedRuntimes serialized definition
061   * @return Avro object
062   * @throws IOException
063   */
064  public DefinedRuntimes fromBytes(final byte[] serializedDefinedRuntimes) throws
065          IOException{
066    try(InputStream is = new ByteArrayInputStream(serializedDefinedRuntimes)) {
067      final BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(is, null);
068      final SpecificDatumReader<DefinedRuntimes> reader = new SpecificDatumReader<>(DefinedRuntimes.class);
069      final DefinedRuntimes rd = reader.read(null, decoder);
070      return rd;
071    }
072  }
073}