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 static org.junit.Assert.assertFalse;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import java.io.InputStream;
025import java.nio.ByteBuffer;
026import java.util.EnumSet;
027import org.apache.hadoop.fs.ByteBufferReadable;
028import org.apache.hadoop.fs.CanSetDropBehind;
029import org.apache.hadoop.fs.CanSetReadahead;
030import org.apache.hadoop.fs.CanUnbuffer;
031import org.apache.hadoop.fs.FSDataInputStream;
032import org.apache.hadoop.fs.FSInputStream;
033import org.apache.hadoop.fs.HasEnhancedByteBufferAccess;
034import org.apache.hadoop.fs.ReadOption;
035import org.apache.hadoop.fs.StreamCapabilities;
036import org.apache.hadoop.hbase.HBaseClassTestRule;
037import org.apache.hadoop.hbase.testclassification.SmallTests;
038import org.apache.hadoop.io.ByteBufferPool;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042
043@Category(SmallTests.class)
044public class TestFSDataInputStreamWrapper {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048    HBaseClassTestRule.forClass(TestFSDataInputStreamWrapper.class);
049
050  @Test
051  public void testUnbuffer() throws Exception {
052    InputStream pc = new ParentClass();
053    InputStream noChecksumPc = new ParentClass();
054    FSDataInputStreamWrapper fsdisw1 =
055      new FSDataInputStreamWrapper(new FSDataInputStream(pc), new FSDataInputStream(noChecksumPc));
056    fsdisw1.unbuffer();
057    // should have called main stream unbuffer, but not no-checksum
058    assertTrue(((ParentClass) pc).getIsCallUnbuffer());
059    assertFalse(((ParentClass) noChecksumPc).getIsCallUnbuffer());
060    // switch to checksums and call unbuffer again. should unbuffer the nochecksum stream now
061    fsdisw1.setShouldUseHBaseChecksum();
062    fsdisw1.unbuffer();
063    assertTrue(((ParentClass) noChecksumPc).getIsCallUnbuffer());
064    fsdisw1.close();
065  }
066
067  private class ParentClass extends FSInputStream implements ByteBufferReadable, CanSetDropBehind,
068    CanSetReadahead, HasEnhancedByteBufferAccess, CanUnbuffer, StreamCapabilities {
069
070    public boolean isCallUnbuffer = false;
071
072    public boolean getIsCallUnbuffer() {
073      return isCallUnbuffer;
074    }
075
076    @Override
077    public void unbuffer() {
078      isCallUnbuffer = true;
079    }
080
081    @Override
082    public int read() throws IOException {
083      return 0;
084    }
085
086    @Override
087    public ByteBuffer read(ByteBufferPool paramByteBufferPool, int paramInt,
088      EnumSet<ReadOption> paramEnumSet) throws IOException, UnsupportedOperationException {
089      return null;
090    }
091
092    @Override
093    public void releaseBuffer(ByteBuffer paramByteBuffer) {
094
095    }
096
097    @Override
098    public void setReadahead(Long paramLong) throws IOException, UnsupportedOperationException {
099
100    }
101
102    @Override
103    public void setDropBehind(Boolean paramBoolean)
104      throws IOException, UnsupportedOperationException {
105
106    }
107
108    @Override
109    public int read(ByteBuffer paramByteBuffer) throws IOException {
110      return 0;
111    }
112
113    @Override
114    public void seek(long paramLong) throws IOException {
115
116    }
117
118    @Override
119    public long getPos() throws IOException {
120      return 0;
121    }
122
123    @Override
124    public boolean seekToNewSource(long paramLong) throws IOException {
125      return false;
126    }
127
128    @Override
129    public boolean hasCapability(String s) {
130      return s.equals(StreamCapabilities.UNBUFFER);
131    }
132  }
133}