[120] | 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 |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 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. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | package org.apache.hadoop.io; |
---|
| 20 | |
---|
| 21 | import java.io.IOException; |
---|
| 22 | import java.io.DataInput; |
---|
| 23 | import java.io.DataOutput; |
---|
| 24 | |
---|
| 25 | import org.apache.commons.logging.Log; |
---|
| 26 | import org.apache.commons.logging.LogFactory; |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * A byte sequence that is usable as a key or value. |
---|
| 30 | * It is resizable and distinguishes between the size of the seqeunce and |
---|
| 31 | * the current capacity. The hash function is the front of the md5 of the |
---|
| 32 | * buffer. The sort order is the same as memcmp. |
---|
| 33 | */ |
---|
| 34 | public class BytesWritable extends BinaryComparable |
---|
| 35 | implements WritableComparable<BinaryComparable> { |
---|
| 36 | private static final Log LOG = LogFactory.getLog(BytesWritable.class); |
---|
| 37 | private static final int LENGTH_BYTES = 4; |
---|
| 38 | private static final byte[] EMPTY_BYTES = {}; |
---|
| 39 | |
---|
| 40 | private int size; |
---|
| 41 | private byte[] bytes; |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | * Create a zero-size sequence. |
---|
| 45 | */ |
---|
| 46 | public BytesWritable() {this(EMPTY_BYTES);} |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * Create a BytesWritable using the byte array as the initial value. |
---|
| 50 | * @param bytes This array becomes the backing storage for the object. |
---|
| 51 | */ |
---|
| 52 | public BytesWritable(byte[] bytes) { |
---|
| 53 | this.bytes = bytes; |
---|
| 54 | this.size = bytes.length; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | /** |
---|
| 58 | * Get the data from the BytesWritable. |
---|
| 59 | * @return The data is only valid between 0 and getLength() - 1. |
---|
| 60 | */ |
---|
| 61 | public byte[] getBytes() { |
---|
| 62 | return bytes; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * Get the data from the BytesWritable. |
---|
| 67 | * @deprecated Use {@link #getBytes()} instead. |
---|
| 68 | */ |
---|
| 69 | @Deprecated |
---|
| 70 | public byte[] get() { |
---|
| 71 | return getBytes(); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /** |
---|
| 75 | * Get the current size of the buffer. |
---|
| 76 | */ |
---|
| 77 | public int getLength() { |
---|
| 78 | return size; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | /** |
---|
| 82 | * Get the current size of the buffer. |
---|
| 83 | * @deprecated Use {@link #getLength()} instead. |
---|
| 84 | */ |
---|
| 85 | @Deprecated |
---|
| 86 | public int getSize() { |
---|
| 87 | return getLength(); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | * Change the size of the buffer. The values in the old range are preserved |
---|
| 92 | * and any new values are undefined. The capacity is changed if it is |
---|
| 93 | * necessary. |
---|
| 94 | * @param size The new number of bytes |
---|
| 95 | */ |
---|
| 96 | public void setSize(int size) { |
---|
| 97 | if (size > getCapacity()) { |
---|
| 98 | setCapacity(size * 3 / 2); |
---|
| 99 | } |
---|
| 100 | this.size = size; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * Get the capacity, which is the maximum size that could handled without |
---|
| 105 | * resizing the backing storage. |
---|
| 106 | * @return The number of bytes |
---|
| 107 | */ |
---|
| 108 | public int getCapacity() { |
---|
| 109 | return bytes.length; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | * Change the capacity of the backing storage. |
---|
| 114 | * The data is preserved. |
---|
| 115 | * @param new_cap The new capacity in bytes. |
---|
| 116 | */ |
---|
| 117 | public void setCapacity(int new_cap) { |
---|
| 118 | if (new_cap != getCapacity()) { |
---|
| 119 | byte[] new_data = new byte[new_cap]; |
---|
| 120 | if (new_cap < size) { |
---|
| 121 | size = new_cap; |
---|
| 122 | } |
---|
| 123 | if (size != 0) { |
---|
| 124 | System.arraycopy(bytes, 0, new_data, 0, size); |
---|
| 125 | } |
---|
| 126 | bytes = new_data; |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | * Set the BytesWritable to the contents of the given newData. |
---|
| 132 | * @param newData the value to set this BytesWritable to. |
---|
| 133 | */ |
---|
| 134 | public void set(BytesWritable newData) { |
---|
| 135 | set(newData.bytes, 0, newData.size); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | * Set the value to a copy of the given byte range |
---|
| 140 | * @param newData the new values to copy in |
---|
| 141 | * @param offset the offset in newData to start at |
---|
| 142 | * @param length the number of bytes to copy |
---|
| 143 | */ |
---|
| 144 | public void set(byte[] newData, int offset, int length) { |
---|
| 145 | setSize(0); |
---|
| 146 | setSize(length); |
---|
| 147 | System.arraycopy(newData, offset, bytes, 0, size); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | // inherit javadoc |
---|
| 151 | public void readFields(DataInput in) throws IOException { |
---|
| 152 | setSize(0); // clear the old data |
---|
| 153 | setSize(in.readInt()); |
---|
| 154 | in.readFully(bytes, 0, size); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | // inherit javadoc |
---|
| 158 | public void write(DataOutput out) throws IOException { |
---|
| 159 | out.writeInt(size); |
---|
| 160 | out.write(bytes, 0, size); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | public int hashCode() { |
---|
| 164 | return super.hashCode(); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | * Are the two byte sequences equal? |
---|
| 169 | */ |
---|
| 170 | public boolean equals(Object right_obj) { |
---|
| 171 | if (right_obj instanceof BytesWritable) |
---|
| 172 | return super.equals(right_obj); |
---|
| 173 | return false; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | /** |
---|
| 177 | * Generate the stream of bytes as hex pairs separated by ' '. |
---|
| 178 | */ |
---|
| 179 | public String toString() { |
---|
| 180 | StringBuffer sb = new StringBuffer(3*size); |
---|
| 181 | for (int idx = 0; idx < size; idx++) { |
---|
| 182 | // if not the first, put a blank separator in |
---|
| 183 | if (idx != 0) { |
---|
| 184 | sb.append(' '); |
---|
| 185 | } |
---|
| 186 | String num = Integer.toHexString(0xff & bytes[idx]); |
---|
| 187 | // if it is only one digit, add a leading 0. |
---|
| 188 | if (num.length() < 2) { |
---|
| 189 | sb.append('0'); |
---|
| 190 | } |
---|
| 191 | sb.append(num); |
---|
| 192 | } |
---|
| 193 | return sb.toString(); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | /** A Comparator optimized for BytesWritable. */ |
---|
| 197 | public static class Comparator extends WritableComparator { |
---|
| 198 | public Comparator() { |
---|
| 199 | super(BytesWritable.class); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | /** |
---|
| 203 | * Compare the buffers in serialized form. |
---|
| 204 | */ |
---|
| 205 | public int compare(byte[] b1, int s1, int l1, |
---|
| 206 | byte[] b2, int s2, int l2) { |
---|
| 207 | return compareBytes(b1, s1+LENGTH_BYTES, l1-LENGTH_BYTES, |
---|
| 208 | b2, s2+LENGTH_BYTES, l2-LENGTH_BYTES); |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | static { // register this comparator |
---|
| 213 | WritableComparator.define(BytesWritable.class, new Comparator()); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | } |
---|