Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 org.onosproject.yangutils.datamodel; |
| 18 | |
| 19 | import java.io.Serializable; |
| 20 | import java.util.Base64; |
| 21 | |
| 22 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangBuiltInDataTypeInfo; |
| 23 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
| 24 | |
| 25 | /* |
| 26 | * Reference RFC 6020. |
| 27 | * |
| 28 | * The binary built-in type represents any binary data, |
| 29 | * i.e., a sequence of octets. |
| 30 | */ |
| 31 | public class YangBinary implements YangBuiltInDataTypeInfo<YangBinary>, Serializable, Comparable<YangBinary> { |
| 32 | |
| 33 | private static final long serialVersionUID = 2106201608L; |
| 34 | |
| 35 | // Binary data is a decoded value by base64 decoding scheme from data input (jason) |
| 36 | private byte[] binaryData; |
| 37 | |
| 38 | /** |
| 39 | * Creates a binary object corresponding to the base 64 encoding value. |
| 40 | * |
| 41 | * @param strValue base64 encoded value |
| 42 | */ |
| 43 | public YangBinary(String strValue) { |
| 44 | setBinaryData(Base64.getDecoder().decode(strValue)); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Retrieves decoded binary data. |
| 49 | * |
| 50 | * @return binary data |
| 51 | */ |
| 52 | public byte[] getBinaryData() { |
| 53 | return binaryData; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Sets binary data. |
| 58 | * |
| 59 | * @param binaryData binary data |
| 60 | */ |
| 61 | public void setBinaryData(byte[] binaryData) { |
| 62 | this.binaryData = binaryData; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Encodes binary data by base64 encoding scheme. |
| 67 | * |
| 68 | * @return encoded binary data |
| 69 | */ |
| 70 | public String toString() { |
| 71 | return Base64.getEncoder() |
| 72 | .encodeToString(binaryData); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public YangDataTypes getYangType() { |
| 77 | return YangDataTypes.BINARY; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public int compareTo(YangBinary o) { |
| 82 | for (int i = 0, j = 0; i < this.binaryData.length && j < o.binaryData.length; i++, j++) { |
| 83 | int a = (this.binaryData[i] & 0xff); |
| 84 | int b = (o.binaryData[j] & 0xff); |
| 85 | if (a != b) { |
| 86 | return a - b; |
| 87 | } |
| 88 | } |
| 89 | return this.binaryData.length - o.binaryData.length; |
| 90 | } |
| 91 | } |