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 */
018package org.apache.hadoop.hbase.io;
019
020import java.io.IOException;
021import java.io.OutputStream;
022import java.nio.ByteBuffer;
023
024import org.apache.hadoop.hbase.io.util.StreamUtils;
025import org.apache.hadoop.hbase.util.ByteBufferUtils;
026import org.apache.yetus.audience.InterfaceAudience;
027
028/**
029 * When deal with OutputStream which is not ByteBufferWriter type, wrap it with this class. We will
030 * have to write offheap ByteBuffer (DBB) data into the OS. This class is having a temp byte array
031 * to which we can copy the DBB data for writing to the OS.
032 * <br>
033 * This is used while writing Cell data to WAL. In case of AsyncWAL, the OS created there is
034 * ByteBufferWriter. But in case of FSHLog, the OS passed by DFS client, is not of type
035 * ByteBufferWriter. We will need this temp solution until DFS client supports writing ByteBuffer
036 * directly to the OS it creates.
037 * <br>
038 * Note: This class is not thread safe.
039 */
040@InterfaceAudience.Private
041public class ByteBufferWriterOutputStream extends OutputStream
042    implements ByteBufferWriter {
043
044  private static final int DEFAULT_BUFFER_SIZE = 4096;
045
046  private final OutputStream os;
047  private final int bufSize;
048  private byte[] buf;
049
050  public ByteBufferWriterOutputStream(OutputStream os) {
051    this(os, DEFAULT_BUFFER_SIZE);
052  }
053
054  public ByteBufferWriterOutputStream(OutputStream os, int size) {
055    this.os = os;
056    this.bufSize = size;
057    this.buf = null;
058  }
059
060  /**
061   * Writes len bytes from the specified ByteBuffer starting at offset off to
062   * this OutputStream. If b is null, a NullPointerException is thrown. If off
063   * is negative or larger than the ByteBuffer then an ArrayIndexOutOfBoundsException
064   * is thrown. If len is greater than the length of the ByteBuffer, then an
065   * ArrayIndexOutOfBoundsException is thrown. This method does not change the
066   * position of the ByteBuffer.
067   *
068   * @param b    the ByteBuffer
069   * @param off  the start offset in the data
070   * @param len  the number of bytes to write
071   * @throws IOException
072   *             if an I/O error occurs. In particular, an IOException is thrown
073   *             if the output stream is closed.
074   */
075  @Override
076  public void write(ByteBuffer b, int off, int len) throws IOException {
077    // Lazily load in the event that this version of 'write' is not invoked
078    if (this.buf == null) {
079      this.buf = new byte[this.bufSize];
080    }
081    int totalCopied = 0;
082    while (totalCopied < len) {
083      int bytesToCopy = Math.min((len - totalCopied), this.bufSize);
084      ByteBufferUtils.copyFromBufferToArray(this.buf, b, off + totalCopied, 0,
085          bytesToCopy);
086      this.os.write(this.buf, 0, bytesToCopy);
087      totalCopied += bytesToCopy;
088    }
089  }
090
091  @Override
092  public void writeInt(int i) throws IOException {
093    StreamUtils.writeInt(this.os, i);
094  }
095
096  @Override
097  public void write(int b) throws IOException {
098    this.os.write(b);
099  }
100
101  @Override
102  public void write(byte[] b, int off, int len) throws IOException {
103    this.os.write(b, off, len);
104  }
105
106  @Override
107  public void flush() throws IOException {
108    this.os.flush();
109  }
110
111  @Override
112  public void close() throws IOException {
113    this.os.close();
114  }
115}