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 */
017
018 /**
019   *  @deprecated as of 0.12.0 and will be removed
020   *  in a future release. Use GenericArchiveRecordWritable instead.
021   */
022
023package io.archivesunleashed.io;
024
025import io.archivesunleashed.data.WarcRecordUtils;
026import java.io.DataInput;
027import java.io.DataOutput;
028import java.io.IOException;
029import org.apache.hadoop.io.Writable;
030import org.archive.io.warc.WARCRecord;
031
032/**
033 * Implements Hadoop Writable for WARC Records.
034 */
035@Deprecated
036public class WarcRecordWritable implements Writable {
037
038  /**
039   * Initialize WARC Record to null.
040   */
041  private WARCRecord record = null;
042
043  /**
044   * Utility function.
045   */
046  public WarcRecordWritable() {
047  }
048
049  /**
050   * Initialize WARC Record.
051   *
052   * @param r WARC Record
053   */
054  public WarcRecordWritable(final WARCRecord r) {
055    this.record = r;
056  }
057
058  /**
059   * Set WARC Record.
060   *
061   * @param r WARC Record
062   */
063  public final void setRecord(final WARCRecord r) {
064    this.record = r;
065  }
066
067  /**
068   * Get WARC Record.
069   *
070   * @return record WARC Record
071   */
072  public final WARCRecord getRecord() {
073    return record;
074  }
075
076  @Override
077  public final void readFields(final DataInput in) throws IOException {
078    int len = in.readInt();
079    if (len == 0) {
080      this.record = null;
081      return;
082    }
083
084    byte[] bytes = new byte[len];
085    in.readFully(bytes);
086
087    this.record = WarcRecordUtils.fromBytes(bytes);
088  }
089
090  @Override
091  public final void write(final DataOutput out) throws IOException {
092    if (record == null) {
093      out.writeInt(0);
094    }
095    byte[] bytes = WarcRecordUtils.toBytes(record);
096
097    out.writeInt(bytes.length);
098    out.write(bytes);
099  }
100}