001/*
002 * Archives Unleashed Toolkit (AUT):
003 * An open-source platform for analyzing web archives.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package io.archivesunleashed.io;
018
019import io.archivesunleashed.data.WarcRecordUtils;
020import java.io.DataInput;
021import java.io.DataOutput;
022import java.io.IOException;
023import org.apache.hadoop.io.Writable;
024import org.archive.io.warc.WARCRecord;
025
026/**
027 * Implements Hadoop Writable for WARC Records.
028 */
029public class WarcRecordWritable implements Writable {
030
031  /**
032   * Initialize WARC Record to null.
033   */
034  private WARCRecord record = null;
035
036  /**
037   * Utility function.
038   */
039  public WarcRecordWritable() {
040  }
041
042  /**
043   * Initialize WARC Record.
044   *
045   * @param r WARC Record
046   */
047  public WarcRecordWritable(final WARCRecord r) {
048    this.record = r;
049  }
050
051  /**
052   * Set WARC Record.
053   *
054   * @param r WARC Record
055   */
056  public final void setRecord(final WARCRecord r) {
057    this.record = r;
058  }
059
060  /**
061   * Get WARC Record.
062   *
063   * @return record WARC Record
064   */
065  public final WARCRecord getRecord() {
066    return record;
067  }
068
069  @Override
070  public final void readFields(final DataInput in) throws IOException {
071    int len = in.readInt();
072    if (len == 0) {
073      this.record = null;
074      return;
075    }
076
077    byte[] bytes = new byte[len];
078    in.readFully(bytes);
079
080    this.record = WarcRecordUtils.fromBytes(bytes);
081  }
082
083  @Override
084  public final void write(final DataOutput out) throws IOException {
085    if (record == null) {
086      out.writeInt(0);
087    }
088    byte[] bytes = WarcRecordUtils.toBytes(record);
089
090    out.writeInt(bytes.length);
091    out.write(bytes);
092  }
093}