HBASE-26481 Consider rolling upgrading from old region replication framework (#3880)
[hbase.git] / hbase-client / src / main / java / org / apache / hadoop / hbase / protobuf / ProtobufMagic.java
blob5268dafb8a6b91ef762ca2c9ebc990c76ae21a42
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 * 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;
22 /**
23 * Protobufs utility.
25 @InterfaceAudience.Private
26 public class ProtobufMagic {
28 private ProtobufMagic() {
31 /**
32 * Magic we put ahead of a serialized protobuf message.
33 * For example, all znode content is protobuf messages with the below magic
34 * for preamble.
36 public static final byte [] PB_MAGIC = new byte [] {'P', 'B', 'U', 'F'};
38 /**
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 &&
56 offset1 == offset2 &&
57 length1 == length2) {
58 return 0;
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);
66 if (a != b) {
67 return a - b;
70 return length1 - length2;
73 /**
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;
84 /**
85 * @return Length of {@link #PB_MAGIC}
87 public static int lengthOfPBMagic() {
88 return PB_MAGIC.length;