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.ArcRecordUtils; 020import java.io.DataInput; 021import java.io.DataOutput; 022import java.io.IOException; 023import org.apache.hadoop.io.Writable; 024import org.archive.io.arc.ARCRecord; 025 026/** 027 * Implements Hadoop Writable for ARC Records. 028 */ 029public class ArcRecordWritable implements Writable { 030 031 /** 032 * Initialize ARC Record to null. 033 */ 034 private ARCRecord record = null; 035 036 /** 037 * Utility function. 038 */ 039 public ArcRecordWritable() { 040 } 041 042 /** 043 * Initialize ARC Record. 044 * 045 * @param r ARC Record 046 */ 047 public ArcRecordWritable(final ARCRecord r) { 048 this.record = r; 049 } 050 051 /** 052 * Set ARC Record. 053 * 054 * @param r ARC Record 055 */ 056 public final void setRecord(final ARCRecord r) { 057 this.record = r; 058 } 059 060 /** 061 * Get ARC Record. 062 * 063 * @return record ARC Record 064 */ 065 public final ARCRecord 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 = ArcRecordUtils.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 = ArcRecordUtils.toBytes(record); 089 090 out.writeInt(bytes.length); 091 out.write(bytes); 092 } 093}