001/**
002 * Java Web Archive Toolkit - Software to read and validate ARC, WARC
003 * and GZip files. (http://jwat.org/)
004 * Copyright 2011-2012 Netarkivet.dk (http://netarkivet.dk/)
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * 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 */
018package org.jwat.common;
019
020import java.io.IOException;
021import java.io.OutputStream;
022import java.io.RandomAccessFile;
023
024/**
025 * This class wraps a <code>RandomAccessFile</code> into a usable
026 * <code>OutputStream</code> which supports random re-position.
027 * Re-positioning is done by using seek() on the <code>RandomAccessFile</code>
028 * object. (@see RandomAccessFile#seek())
029 *
030 * @author nicl
031 */
032public class RandomAccessFileOutputStream extends OutputStream {
033
034    /** Encapsulated <code>RandomAccessFile</code> used for stream data. */
035    protected RandomAccessFile raf;
036
037    /**
038     * Create a new random access <code>OutputStream</code> with repositioning
039     * capabilities.
040     * @param raf <code>RandomAccessFile</code> used for stream data
041     */
042    public RandomAccessFileOutputStream(RandomAccessFile raf) {
043        this.raf = raf;
044    }
045
046    /**
047     * Closing this stream has no effect.
048     * @throws IOException if an I/O error occurs while closing stream
049     */
050    @Override
051    public void close() throws IOException {
052        raf = null;
053    }
054
055    /**
056     * Flushing this stream has no effect.
057     * @throws IOException if an I/O error occurs while flushing stream
058     */
059    @Override
060    public void flush() throws IOException {
061    }
062
063    @Override
064    public void write(int b) throws IOException {
065        raf.write(b);
066    }
067
068    @Override
069    public void write(byte[] b) throws IOException {
070        raf.write(b);
071    }
072
073    @Override
074    public void write(byte[] b, int off, int len) throws IOException {
075        raf.write(b, off, len);
076    }
077
078}