/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.digitalpebble.behemoth.tika;

import org.apache.hadoop.io.Writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 *
 *
 **/
public class TextArrayWritable implements Writable {
    // Hmm, is this the best way to do this?
    private String[] array;

    public TextArrayWritable(String[] array) {
        this.array = array;
    }

    public String[] getArray() {
        return array;
    }

    public void setArray(String[] array) {
        this.array = array;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        if (array != null) {
            dataOutput.writeInt(array.length);
            for (int i = 0; i < array.length; i++) {
                dataOutput.writeUTF(array[i]);
            }
        } else {
            dataOutput.writeInt(0);
        }
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        int len = dataInput.readInt();
        if (len > 0) {
            array = new String[len];
            for (int i = 0; i < len; i++) {
                array[i] = dataInput.readUTF();
            }
        }
    }
}
