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.util; 019 020import static org.junit.jupiter.api.Assertions.assertTrue; 021 022import java.nio.ByteBuffer; 023import org.apache.hadoop.hbase.nio.ByteBuff; 024import org.apache.hadoop.hbase.nio.MultiByteBuff; 025import org.apache.hadoop.hbase.nio.SingleByteBuff; 026import org.apache.hadoop.hbase.testclassification.MiscTests; 027import org.apache.hadoop.hbase.testclassification.SmallTests; 028import org.junit.jupiter.api.Tag; 029import org.junit.jupiter.api.Test; 030 031@Tag(MiscTests.TAG) 032@Tag(SmallTests.TAG) 033public class TestByteBuffUtils { 034 035 @Test 036 public void testCopyAndCompare() throws Exception { 037 ByteBuffer bb1 = ByteBuffer.allocate(50); 038 ByteBuffer bb2 = ByteBuffer.allocate(50); 039 MultiByteBuff src = new MultiByteBuff(bb1, bb2); 040 for (int i = 0; i < 7; i++) { 041 src.putLong(8L); 042 } 043 src.put((byte) 1); 044 src.put((byte) 1); 045 ByteBuffer bb3 = ByteBuffer.allocate(50); 046 ByteBuffer bb4 = ByteBuffer.allocate(50); 047 MultiByteBuff mbbDst = new MultiByteBuff(bb3, bb4); 048 // copy from MBB to MBB 049 mbbDst.put(0, src, 0, 100); 050 int compareTo = ByteBuff.compareTo(src, 0, 100, mbbDst, 0, 100); 051 assertTrue(compareTo == 0); 052 // Copy from MBB to SBB 053 bb3 = ByteBuffer.allocate(100); 054 SingleByteBuff sbbDst = new SingleByteBuff(bb3); 055 src.rewind(); 056 sbbDst.put(0, src, 0, 100); 057 compareTo = ByteBuff.compareTo(src, 0, 100, sbbDst, 0, 100); 058 assertTrue(compareTo == 0); 059 // Copy from SBB to SBB 060 bb3 = ByteBuffer.allocate(100); 061 SingleByteBuff sbb = new SingleByteBuff(bb3); 062 for (int i = 0; i < 7; i++) { 063 sbb.putLong(8L); 064 } 065 sbb.put((byte) 1); 066 sbb.put((byte) 1); 067 bb4 = ByteBuffer.allocate(100); 068 sbbDst = new SingleByteBuff(bb4); 069 sbbDst.put(0, sbb, 0, 100); 070 compareTo = ByteBuff.compareTo(sbb, 0, 100, sbbDst, 0, 100); 071 assertTrue(compareTo == 0); 072 // copy from SBB to MBB 073 sbb.rewind(); 074 mbbDst = new MultiByteBuff(bb3, bb4); 075 mbbDst.rewind(); 076 mbbDst.put(0, sbb, 0, 100); 077 compareTo = ByteBuff.compareTo(sbb, 0, 100, mbbDst, 0, 100); 078 assertTrue(compareTo == 0); 079 } 080}