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, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018/*
019 * The MIT License (MIT)
020 * Copyright (c) 2014 Martin Kleppmann
021 *
022 * Permission is hereby granted, free of charge, to any person obtaining a copy
023 * of this software and associated documentation files (the "Software"), to deal
024 * in the Software without restriction, including without limitation the rights
025 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
026 * copies of the Software, and to permit persons to whom the Software is
027 * furnished to do so, subject to the following conditions:
028 *
029 * The above copyright notice and this permission notice shall be included in
030 * all copies or substantial portions of the Software.
031 *
032 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
033 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
034 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
035 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
036 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
037 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
038 * THE SOFTWARE.
039 */
040package org.apache.hadoop.hbase.test.util.warc;
041
042import java.io.DataInput;
043import java.io.DataOutput;
044import java.io.IOException;
045import org.apache.hadoop.io.Writable;
046
047/**
048 * A mutable wrapper around a {@link WARCRecord} implementing the Hadoop Writable interface. This
049 * allows WARC records to be used throughout Hadoop (e.g. written to sequence files when shuffling
050 * data between mappers and reducers). The record is encoded as a single record in standard WARC/1.0
051 * format.
052 */
053public class WARCWritable implements Writable {
054
055  private WARCRecord record;
056
057  /** Creates an empty writable (with a null record). */
058  public WARCWritable() {
059    this.record = null;
060  }
061
062  /** Creates a writable wrapper around a given WARCRecord. */
063  public WARCWritable(WARCRecord record) {
064    this.record = record;
065  }
066
067  /** Returns the record currently wrapped by this writable. */
068  public WARCRecord getRecord() {
069    return record;
070  }
071
072  /** Updates the record held within this writable wrapper. */
073  public void setRecord(WARCRecord record) {
074    this.record = record;
075  }
076
077  /** Appends the current record to a {@link DataOutput} stream. */
078  @Override
079  public void write(DataOutput out) throws IOException {
080    if (record != null) {
081      record.write(out);
082    }
083  }
084
085  /**
086   * Parses a {@link WARCRecord} out of a {@link DataInput} stream, and makes it the writable's
087   * current record.
088   */
089  @Override
090  public void readFields(DataInput in) throws IOException {
091    record = new WARCRecord(in);
092  }
093
094}