|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | +
|
| 6 | + * regarding copyright ownership. The ASF licenses this file |
| 7 | + * to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance |
| 9 | + * with the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.apache.bookkeeper.proto; |
| 20 | + |
| 21 | +import com.google.protobuf.ByteString; |
| 22 | +import com.google.protobuf.UnsafeByteOperations; |
| 23 | +import io.netty.buffer.ByteBuf; |
| 24 | +import java.nio.ByteBuffer; |
| 25 | +import org.apache.bookkeeper.util.ByteBufList; |
| 26 | + |
| 27 | +public class ByteStringUtil { |
| 28 | + |
| 29 | + /** |
| 30 | + * Wrap the internal buffers of a ByteBufList into a single ByteString. |
| 31 | + * The lifecycle of the wrapped ByteString is tied to the ByteBufList. |
| 32 | + * |
| 33 | + * @param bufList ByteBufList to wrap |
| 34 | + * @return ByteString wrapping the internal buffers of the ByteBufList |
| 35 | + */ |
| 36 | + public static ByteString byteBufListToByteString(ByteBufList bufList) { |
| 37 | + ByteString aggregated = null; |
| 38 | + for (int i = 0; i < bufList.size(); i++) { |
| 39 | + aggregated = byteBufToByteString(aggregated, bufList.getBuffer(i)); |
| 40 | + } |
| 41 | + return aggregated != null ? aggregated : ByteString.EMPTY; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Wrap the internal buffers of a ByteBuf into a single ByteString. |
| 46 | + * The lifecycle of the wrapped ByteString is tied to the ByteBuf. |
| 47 | + * |
| 48 | + * @param byteBuf ByteBuf to wrap |
| 49 | + * @return ByteString wrapping the internal buffers of the ByteBuf |
| 50 | + */ |
| 51 | + public static ByteString byteBufToByteString(ByteBuf byteBuf) { |
| 52 | + return byteBufToByteString(null, byteBuf); |
| 53 | + } |
| 54 | + |
| 55 | + // internal method to aggregate a ByteBuf into a single aggregated ByteString |
| 56 | + private static ByteString byteBufToByteString(ByteString aggregated, ByteBuf byteBuf) { |
| 57 | + if (byteBuf.nioBufferCount() > 1) { |
| 58 | + for (ByteBuffer nioBuffer : byteBuf.nioBuffers()) { |
| 59 | + ByteString piece = UnsafeByteOperations.unsafeWrap(nioBuffer); |
| 60 | + aggregated = (aggregated == null) ? piece : aggregated.concat(piece); |
| 61 | + } |
| 62 | + } else { |
| 63 | + ByteString piece; |
| 64 | + if (byteBuf.hasArray()) { |
| 65 | + piece = UnsafeByteOperations.unsafeWrap(byteBuf.array(), byteBuf.arrayOffset(), |
| 66 | + byteBuf.readableBytes()); |
| 67 | + } else { |
| 68 | + piece = UnsafeByteOperations.unsafeWrap(byteBuf.nioBuffer()); |
| 69 | + } |
| 70 | + aggregated = (aggregated == null) ? piece : aggregated.concat(piece); |
| 71 | + } |
| 72 | + return aggregated; |
| 73 | + } |
| 74 | +} |
0 commit comments