Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.google.flatbuffers; |
| 18 | |
| 19 | import static com.google.flatbuffers.Constants.*; |
| 20 | |
| 21 | import java.nio.ByteBuffer; |
| 22 | |
| 23 | /// @file |
| 24 | /// @addtogroup flatbuffers_java_api |
| 25 | /// @{ |
| 26 | |
| 27 | /** |
| 28 | * Class that collects utility functions around `ByteBuffer`. |
| 29 | */ |
| 30 | public class ByteBufferUtil { |
| 31 | |
| 32 | /** |
| 33 | * Extract the size prefix from a `ByteBuffer`. |
| 34 | * |
| 35 | * @param bb a size-prefixed buffer |
| 36 | * @return the size prefix |
| 37 | */ |
| 38 | public static int getSizePrefix(ByteBuffer bb) { |
| 39 | return bb.getInt(bb.position()); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Create a duplicate of a size-prefixed `ByteBuffer` that has its position |
| 44 | * advanced just past the size prefix. |
| 45 | * |
| 46 | * @param bb a size-prefixed buffer |
| 47 | * @return a new buffer on the same underlying data that has skipped the |
| 48 | * size prefix |
| 49 | */ |
| 50 | public static ByteBuffer removeSizePrefix(ByteBuffer bb) { |
| 51 | ByteBuffer s = bb.duplicate(); |
| 52 | s.position(s.position() + SIZE_PREFIX_LENGTH); |
| 53 | return s; |
| 54 | } |
| 55 | |
| 56 | } |
| 57 | |
| 58 | /// @} |