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 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.protobuf
;
20 import org
.apache
.yetus
.audience
.InterfaceAudience
;
25 @InterfaceAudience.Private
26 public class ProtobufMagic
{
28 private ProtobufMagic() {
32 * Magic we put ahead of a serialized protobuf message.
33 * For example, all znode content is protobuf messages with the below magic
36 public static final byte [] PB_MAGIC
= new byte [] {'P', 'B', 'U', 'F'};
39 * @param bytes Bytes to check.
40 * @return True if passed <code>bytes</code> has {@link #PB_MAGIC} for a prefix.
42 public static boolean isPBMagicPrefix(final byte [] bytes
) {
43 if (bytes
== null) return false;
44 return isPBMagicPrefix(bytes
, 0, bytes
.length
);
48 * Copied from Bytes.java to here
49 * hbase-common now depends on hbase-protocol
50 * Referencing Bytes.java directly would create circular dependency
52 private static int compareTo(byte[] buffer1
, int offset1
, int length1
,
53 byte[] buffer2
, int offset2
, int length2
) {
54 // Short circuit equal case
55 if (buffer1
== buffer2
&&
60 // Bring WritableComparator code local
61 int end1
= offset1
+ length1
;
62 int end2
= offset2
+ length2
;
63 for (int i
= offset1
, j
= offset2
; i
< end1
&& j
< end2
; i
++, j
++) {
64 int a
= (buffer1
[i
] & 0xff);
65 int b
= (buffer2
[j
] & 0xff);
70 return length1
- length2
;
74 * @param bytes Bytes to check.
75 * @param offset offset to start at
76 * @param len length to use
77 * @return True if passed <code>bytes</code> has {@link #PB_MAGIC} for a prefix.
79 public static boolean isPBMagicPrefix(final byte [] bytes
, int offset
, int len
) {
80 if (bytes
== null || len
< PB_MAGIC
.length
) return false;
81 return compareTo(PB_MAGIC
, 0, PB_MAGIC
.length
, bytes
, offset
, PB_MAGIC
.length
) == 0;
85 * @return Length of {@link #PB_MAGIC}
87 public static int lengthOfPBMagic() {
88 return PB_MAGIC
.length
;