001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  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 org.apache.commons.flatfile;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.io.OutputStream;
022
023/**
024 * Stores string representations of objects in a (fixed-length) byte array.
025 * @version $Revision: 757266 $ $Date: 2009-03-22 17:12:27 -0500 (Sun, 22 Mar 2009) $
026 */
027public class Field extends PadJustifyFieldSupport {
028
029    private static final long serialVersionUID = 3939293448043882440L;
030
031    private Overflow overflow;
032    private byte[] buffer;
033
034    /**
035     * Create a new Field.
036     * @param size of field
037     */
038    public Field(int size) {
039        this(new byte[size]);
040    }
041
042    /**
043     * Create and initialize a new Field.
044     * @param buffer byte[] which is not copied, but used directly.
045     */
046    public Field(byte[] buffer) {
047        this.buffer = buffer;
048    }
049
050    /**
051     * {@inheritDoc}
052     */
053    public int length() {
054        return buffer.length;
055    }
056
057    /**
058     * {@inheritDoc}
059     */
060    public synchronized void readFrom(InputStream is) throws IOException {
061        is.read(buffer);
062    }
063
064    /**
065     * {@inheritDoc}
066     */
067    public synchronized void writeTo(OutputStream os) throws IOException {
068        os.write(buffer);
069    }
070
071    /**
072     * {@inheritDoc}
073     */
074    public void setValue(byte[] b) {
075        getOverflow().check(b, buffer.length);
076        super.setValue(b);
077    }
078
079    /**
080     * {@inheritDoc}
081     */
082    public byte[] getValue() {
083        return buffer;
084    }
085
086    /**
087     * {@inheritDoc}
088     */
089    public Field clone() {
090        Field result = (Field) super.clone();
091        result.buffer = new byte[buffer.length];
092        System.arraycopy(buffer, 0, result.buffer, 0, buffer.length);
093        return result;
094    }
095
096    /**
097     * Get the overflow.
098     * @return Overflow
099     */
100    public Overflow getOverflow() {
101        return overflow == null ? Overflow.ERROR : overflow;
102    }
103
104    /**
105     * Set the overflow.
106     * @param overflow Overflow
107     */
108    public void setOverflow(Overflow overflow) {
109        this.overflow = overflow;
110    }
111
112    /**
113     * {@inheritDoc}
114     */
115    protected int getPadJustifyLength() {
116        return length();
117    }
118}