View Javadoc

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.hbase;
20  
21  import java.io.DataInput;
22  import java.io.EOFException;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  import java.nio.ByteBuffer;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.apache.hadoop.hbase.classification.InterfaceAudience;
31  import org.apache.hadoop.hbase.KeyValue.Type;
32  import org.apache.hadoop.hbase.io.util.StreamUtils;
33  import org.apache.hadoop.hbase.util.ByteBufferUtils;
34  import org.apache.hadoop.hbase.util.Bytes;
35  import org.apache.hadoop.hbase.util.IterableUtils;
36  import org.apache.hadoop.hbase.util.SimpleMutableByteRange;
37  import org.apache.hadoop.io.IOUtils;
38  import org.apache.hadoop.io.WritableUtils;
39  
40  import com.google.common.base.Function;
41  import com.google.common.collect.Lists;
42  
43  /**
44   * static convenience methods for dealing with KeyValues and collections of KeyValues
45   */
46  @InterfaceAudience.Private
47  public class KeyValueUtil {
48  
49    /**************** length *********************/
50  
51    /**
52     * Returns number of bytes this cell would have been used if serialized as in {@link KeyValue}
53     * @param cell
54     * @return the length
55     */
56    public static int length(final Cell cell) {
57      return length(cell.getRowLength(), cell.getFamilyLength(), cell.getQualifierLength(),
58          cell.getValueLength(), cell.getTagsLength(), true);
59    }
60  
61    private static int length(short rlen, byte flen, int qlen, int vlen, int tlen, boolean withTags) {
62      if (withTags) {
63        return (int) (KeyValue.getKeyValueDataStructureSize(rlen, flen, qlen, vlen, tlen));
64      }
65      return (int) (KeyValue.getKeyValueDataStructureSize(rlen, flen, qlen, vlen));
66    }
67  
68    /**
69     * Returns number of bytes this cell's key part would have been used if serialized as in
70     * {@link KeyValue}. Key includes rowkey, family, qualifier, timestamp and type.
71     * @param cell
72     * @return the key length
73     */
74    public static int keyLength(final Cell cell) {
75      return keyLength(cell.getRowLength(), cell.getFamilyLength(), cell.getQualifierLength());
76    }
77  
78    private static int keyLength(short rlen, byte flen, int qlen) {
79      return (int) KeyValue.getKeyDataStructureSize(rlen, flen, qlen);
80    }
81  
82    public static int lengthWithMvccVersion(final KeyValue kv, final boolean includeMvccVersion) {
83      int length = kv.getLength();
84      if (includeMvccVersion) {
85        length += WritableUtils.getVIntSize(kv.getMvccVersion());
86      }
87      return length;
88    }
89  
90    public static int totalLengthWithMvccVersion(final Iterable<? extends KeyValue> kvs,
91        final boolean includeMvccVersion) {
92      int length = 0;
93      for (KeyValue kv : IterableUtils.nullSafe(kvs)) {
94        length += lengthWithMvccVersion(kv, includeMvccVersion);
95      }
96      return length;
97    }
98  
99  
100   /**************** copy key only *********************/
101 
102   public static KeyValue copyToNewKeyValue(final Cell cell) {
103     byte[] bytes = copyToNewByteArray(cell);
104     KeyValue kvCell = new KeyValue(bytes, 0, bytes.length);
105     kvCell.setSequenceId(cell.getMvccVersion());
106     return kvCell;
107   }
108 
109   /**
110    * The position will be set to the beginning of the new ByteBuffer
111    * @param cell
112    * @return the Bytebuffer containing the key part of the cell
113    */
114   public static ByteBuffer copyKeyToNewByteBuffer(final Cell cell) {
115     byte[] bytes = new byte[keyLength(cell)];
116     appendKeyTo(cell, bytes, 0);
117     ByteBuffer buffer = ByteBuffer.wrap(bytes);
118     return buffer;
119   }
120 
121   public static byte[] copyToNewByteArray(final Cell cell) {
122     int v1Length = length(cell);
123     byte[] backingBytes = new byte[v1Length];
124     appendToByteArray(cell, backingBytes, 0);
125     return backingBytes;
126   }
127 
128   public static int appendKeyTo(final Cell cell, final byte[] output,
129       final int offset) {
130     int nextOffset = offset;
131     nextOffset = Bytes.putShort(output, nextOffset, cell.getRowLength());
132     nextOffset = CellUtil.copyRowTo(cell, output, nextOffset);
133     nextOffset = Bytes.putByte(output, nextOffset, cell.getFamilyLength());
134     nextOffset = CellUtil.copyFamilyTo(cell, output, nextOffset);
135     nextOffset = CellUtil.copyQualifierTo(cell, output, nextOffset);
136     nextOffset = Bytes.putLong(output, nextOffset, cell.getTimestamp());
137     nextOffset = Bytes.putByte(output, nextOffset, cell.getTypeByte());
138     return nextOffset;
139   }
140 
141 
142   /**************** copy key and value *********************/
143 
144   public static int appendToByteArray(final Cell cell, final byte[] output, final int offset) {
145     // TODO when cell instance of KV we can bypass all steps and just do backing single array
146     // copy(?)
147     int pos = offset;
148     pos = Bytes.putInt(output, pos, keyLength(cell));
149     pos = Bytes.putInt(output, pos, cell.getValueLength());
150     pos = appendKeyTo(cell, output, pos);
151     pos = CellUtil.copyValueTo(cell, output, pos);
152     if ((cell.getTagsLength() > 0)) {
153       pos = Bytes.putAsShort(output, pos, cell.getTagsLength());
154       pos = CellUtil.copyTagTo(cell, output, pos);
155     }
156     return pos;
157   }
158 
159   /**
160    * The position will be set to the beginning of the new ByteBuffer
161    * @param cell
162    * @return the ByteBuffer containing the cell
163    */
164   public static ByteBuffer copyToNewByteBuffer(final Cell cell) {
165     byte[] bytes = new byte[length(cell)];
166     appendToByteArray(cell, bytes, 0);
167     ByteBuffer buffer = ByteBuffer.wrap(bytes);
168     return buffer;
169   }
170 
171   public static void appendToByteBuffer(final ByteBuffer bb, final KeyValue kv,
172       final boolean includeMvccVersion) {
173     // keep pushing the limit out. assume enough capacity
174     bb.limit(bb.position() + kv.getLength());
175     bb.put(kv.getBuffer(), kv.getOffset(), kv.getLength());
176     if (includeMvccVersion) {
177       int numMvccVersionBytes = WritableUtils.getVIntSize(kv.getMvccVersion());
178       ByteBufferUtils.extendLimit(bb, numMvccVersionBytes);
179       ByteBufferUtils.writeVLong(bb, kv.getMvccVersion());
180     }
181   }
182 
183 
184   /**************** iterating *******************************/
185 
186   /**
187    * Creates a new KeyValue object positioned in the supplied ByteBuffer and sets the ByteBuffer's
188    * position to the start of the next KeyValue. Does not allocate a new array or copy data.
189    * @param bb
190    * @param includesMvccVersion
191    * @param includesTags
192    */
193   public static KeyValue nextShallowCopy(final ByteBuffer bb, final boolean includesMvccVersion,
194       boolean includesTags) {
195     if (bb.isDirect()) {
196       throw new IllegalArgumentException("only supports heap buffers");
197     }
198     if (bb.remaining() < 1) {
199       return null;
200     }
201     KeyValue keyValue = null;
202     int underlyingArrayOffset = bb.arrayOffset() + bb.position();
203     int keyLength = bb.getInt();
204     int valueLength = bb.getInt();
205     ByteBufferUtils.skip(bb, keyLength + valueLength);
206     int tagsLength = 0;
207     if (includesTags) {
208       // Read short as unsigned, high byte first
209       tagsLength = ((bb.get() & 0xff) << 8) ^ (bb.get() & 0xff);
210       ByteBufferUtils.skip(bb, tagsLength);
211     }
212     int kvLength = (int) KeyValue.getKeyValueDataStructureSize(keyLength, valueLength, tagsLength);
213     keyValue = new KeyValue(bb.array(), underlyingArrayOffset, kvLength);
214     if (includesMvccVersion) {
215       long mvccVersion = ByteBufferUtils.readVLong(bb);
216       keyValue.setSequenceId(mvccVersion);
217     }
218     return keyValue;
219   }
220 
221 
222   /*************** next/previous **********************************/
223 
224   /**
225    * Append single byte 0x00 to the end of the input row key
226    */
227   public static KeyValue createFirstKeyInNextRow(final Cell in){
228     byte[] nextRow = new byte[in.getRowLength() + 1];
229     System.arraycopy(in.getRowArray(), in.getRowOffset(), nextRow, 0, in.getRowLength());
230     nextRow[nextRow.length - 1] = 0;//maybe not necessary
231     return createFirstOnRow(nextRow);
232   }
233 
234   /**
235    * Increment the row bytes and clear the other fields
236    */
237   public static KeyValue createFirstKeyInIncrementedRow(final Cell in){
238     byte[] thisRow = new SimpleMutableByteRange(in.getRowArray(), in.getRowOffset(),
239         in.getRowLength()).deepCopyToNewArray();
240     byte[] nextRow = Bytes.unsignedCopyAndIncrement(thisRow);
241     return createFirstOnRow(nextRow);
242   }
243 
244   /**
245    * Decrement the timestamp.  For tests (currently wasteful)
246    *
247    * Remember timestamps are sorted reverse chronologically.
248    * @param in
249    * @return previous key
250    */
251   public static KeyValue previousKey(final KeyValue in) {
252     return createFirstOnRow(CellUtil.cloneRow(in), CellUtil.cloneFamily(in),
253       CellUtil.cloneQualifier(in), in.getTimestamp() - 1);
254   }
255 
256 
257   /**
258    * Create a KeyValue for the specified row, family and qualifier that would be
259    * larger than or equal to all other possible KeyValues that have the same
260    * row, family, qualifier. Used for reseeking.
261    *
262    * @param row
263    *          row key
264    * @param roffset
265    *         row offset
266    * @param rlength
267    *         row length
268    * @param family
269    *         family name
270    * @param foffset
271    *         family offset
272    * @param flength
273    *         family length
274    * @param qualifier
275    *        column qualifier
276    * @param qoffset
277    *        qualifier offset
278    * @param qlength
279    *        qualifier length
280    * @return Last possible key on passed row, family, qualifier.
281    */
282   public static KeyValue createLastOnRow(final byte[] row, final int roffset, final int rlength,
283       final byte[] family, final int foffset, final int flength, final byte[] qualifier,
284       final int qoffset, final int qlength) {
285     return new KeyValue(row, roffset, rlength, family, foffset, flength, qualifier, qoffset,
286         qlength, HConstants.OLDEST_TIMESTAMP, Type.Minimum, null, 0, 0);
287   }
288   
289   /**
290    * Creates a keyValue for the specified keyvalue larger than or equal to all other possible
291    * KeyValues that have the same row, family, qualifer.  Used for reseeking
292    * @param kv
293    * @return KeyValue
294    */
295   public static KeyValue createLastOnRow(Cell kv) {
296     return createLastOnRow(kv.getRowArray(), kv.getRowOffset(), kv.getRowLength(), null, 0, 0,
297         null, 0, 0);
298   }
299 
300   /**
301    * Similar to
302    * {@link #createLastOnRow(byte[], int, int, byte[], int, int, byte[], int, int)}
303    * but creates the last key on the row/column of this KV (the value part of
304    * the returned KV is always empty). Used in creating "fake keys" for the
305    * multi-column Bloom filter optimization to skip the row/column we already
306    * know is not in the file.
307    * 
308    * @param kv - cell
309    * @return the last key on the row/column of the given key-value pair
310    */
311   public static KeyValue createLastOnRowCol(Cell kv) {
312     return new KeyValue(kv.getRowArray(), kv.getRowOffset(), kv.getRowLength(),
313         kv.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyLength(), kv.getQualifierArray(),
314         kv.getQualifierOffset(), kv.getQualifierLength(), HConstants.OLDEST_TIMESTAMP,
315         Type.Minimum, null, 0, 0);
316   }
317 
318   /**
319    * Creates the first KV with the row/family/qualifier of this KV and the given
320    * timestamp. Uses the "maximum" KV type that guarantees that the new KV is
321    * the lowest possible for this combination of row, family, qualifier, and
322    * timestamp. This KV's own timestamp is ignored. While this function copies
323    * the value from this KV, it is normally used on key-only KVs.
324    * 
325    * @param kv - cell
326    * @param ts
327    */
328   public static KeyValue createFirstOnRowColTS(Cell kv, long ts) {
329     return new KeyValue(kv.getRowArray(), kv.getRowOffset(), kv.getRowLength(),
330         kv.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyLength(), kv.getQualifierArray(),
331         kv.getQualifierOffset(), kv.getQualifierLength(), ts, Type.Maximum, kv.getValueArray(),
332         kv.getValueOffset(), kv.getValueLength());
333   }
334   
335   /**
336    * Create a KeyValue that is smaller than all other possible KeyValues
337    * for the given row. That is any (valid) KeyValue on 'row' would sort
338    * _after_ the result.
339    *
340    * @param row - row key (arbitrary byte array)
341    * @return First possible KeyValue on passed <code>row</code>
342    */
343   public static KeyValue createFirstOnRow(final byte [] row, int roffset, short rlength) {
344     return new KeyValue(row, roffset, rlength,
345         null, 0, 0, null, 0, 0, HConstants.LATEST_TIMESTAMP, Type.Maximum, null, 0, 0);
346   }
347   
348 
349   /**
350    * Creates a KeyValue that is last on the specified row id. That is,
351    * every other possible KeyValue for the given row would compareTo()
352    * less than the result of this call.
353    * @param row row key
354    * @return Last possible KeyValue on passed <code>row</code>
355    */
356   public static KeyValue createLastOnRow(final byte[] row) {
357     return new KeyValue(row, null, null, HConstants.LATEST_TIMESTAMP, Type.Minimum);
358   }
359 
360   /**
361    * Create a KeyValue that is smaller than all other possible KeyValues
362    * for the given row. That is any (valid) KeyValue on 'row' would sort
363    * _after_ the result.
364    *
365    * @param row - row key (arbitrary byte array)
366    * @return First possible KeyValue on passed <code>row</code>
367    */
368   public static KeyValue createFirstOnRow(final byte [] row) {
369     return createFirstOnRow(row, HConstants.LATEST_TIMESTAMP);
370   }
371 
372   /**
373    * Creates a KeyValue that is smaller than all other KeyValues that
374    * are older than the passed timestamp.
375    * @param row - row key (arbitrary byte array)
376    * @param ts - timestamp
377    * @return First possible key on passed <code>row</code> and timestamp.
378    */
379   public static KeyValue createFirstOnRow(final byte [] row,
380       final long ts) {
381     return new KeyValue(row, null, null, ts, Type.Maximum);
382   }
383 
384   /**
385    * Create a KeyValue for the specified row, family and qualifier that would be
386    * smaller than all other possible KeyValues that have the same row,family,qualifier.
387    * Used for seeking.
388    * @param row - row key (arbitrary byte array)
389    * @param family - family name
390    * @param qualifier - column qualifier
391    * @return First possible key on passed <code>row</code>, and column.
392    */
393   public static KeyValue createFirstOnRow(final byte [] row, final byte [] family,
394       final byte [] qualifier) {
395     return new KeyValue(row, family, qualifier, HConstants.LATEST_TIMESTAMP, Type.Maximum);
396   }
397 
398   /**
399    * Create a Delete Family KeyValue for the specified row and family that would
400    * be smaller than all other possible Delete Family KeyValues that have the
401    * same row and family.
402    * Used for seeking.
403    * @param row - row key (arbitrary byte array)
404    * @param family - family name
405    * @return First Delete Family possible key on passed <code>row</code>.
406    */
407   public static KeyValue createFirstDeleteFamilyOnRow(final byte [] row,
408       final byte [] family) {
409     return new KeyValue(row, family, null, HConstants.LATEST_TIMESTAMP,
410         Type.DeleteFamily);
411   }
412 
413   /**
414    * @param row - row key (arbitrary byte array)
415    * @param f - family name
416    * @param q - column qualifier
417    * @param ts - timestamp
418    * @return First possible key on passed <code>row</code>, column and timestamp
419    */
420   public static KeyValue createFirstOnRow(final byte [] row, final byte [] f,
421       final byte [] q, final long ts) {
422     return new KeyValue(row, f, q, ts, Type.Maximum);
423   }
424 
425   /**
426    * Create a KeyValue for the specified row, family and qualifier that would be
427    * smaller than all other possible KeyValues that have the same row,
428    * family, qualifier.
429    * Used for seeking.
430    * @param row row key
431    * @param roffset row offset
432    * @param rlength row length
433    * @param family family name
434    * @param foffset family offset
435    * @param flength family length
436    * @param qualifier column qualifier
437    * @param qoffset qualifier offset
438    * @param qlength qualifier length
439    * @return First possible key on passed Row, Family, Qualifier.
440    */
441   public static KeyValue createFirstOnRow(final byte [] row,
442       final int roffset, final int rlength, final byte [] family,
443       final int foffset, final int flength, final byte [] qualifier,
444       final int qoffset, final int qlength) {
445     return new KeyValue(row, roffset, rlength, family,
446         foffset, flength, qualifier, qoffset, qlength,
447         HConstants.LATEST_TIMESTAMP, Type.Maximum, null, 0, 0);
448   }
449 
450   /**
451    * Create a KeyValue for the specified row, family and qualifier that would be
452    * smaller than all other possible KeyValues that have the same row,
453    * family, qualifier.
454    * Used for seeking.
455    *
456    * @param buffer the buffer to use for the new <code>KeyValue</code> object
457    * @param row the value key
458    * @param family family name
459    * @param qualifier column qualifier
460    *
461    * @return First possible key on passed Row, Family, Qualifier.
462    *
463    * @throws IllegalArgumentException The resulting <code>KeyValue</code> object would be larger
464    * than the provided buffer or than <code>Integer.MAX_VALUE</code>
465    */
466   public static KeyValue createFirstOnRow(byte [] buffer, final byte [] row,
467       final byte [] family, final byte [] qualifier)
468           throws IllegalArgumentException {
469     return createFirstOnRow(buffer, 0, row, 0, row.length,
470         family, 0, family.length,
471         qualifier, 0, qualifier.length);
472   }
473 
474   /**
475    * Create a KeyValue for the specified row, family and qualifier that would be
476    * smaller than all other possible KeyValues that have the same row,
477    * family, qualifier.
478    * Used for seeking.
479    *
480    * @param buffer the buffer to use for the new <code>KeyValue</code> object
481    * @param boffset buffer offset
482    * @param row the value key
483    * @param roffset row offset
484    * @param rlength row length
485    * @param family family name
486    * @param foffset family offset
487    * @param flength family length
488    * @param qualifier column qualifier
489    * @param qoffset qualifier offset
490    * @param qlength qualifier length
491    *
492    * @return First possible key on passed Row, Family, Qualifier.
493    *
494    * @throws IllegalArgumentException The resulting <code>KeyValue</code> object would be larger
495    * than the provided buffer or than <code>Integer.MAX_VALUE</code>
496    */
497   public static KeyValue createFirstOnRow(byte[] buffer, final int boffset, final byte[] row,
498       final int roffset, final int rlength, final byte[] family, final int foffset,
499       final int flength, final byte[] qualifier, final int qoffset, final int qlength)
500       throws IllegalArgumentException {
501 
502     long lLength = KeyValue.getKeyValueDataStructureSize(rlength, flength, qlength, 0);
503 
504     if (lLength > Integer.MAX_VALUE) {
505       throw new IllegalArgumentException("KeyValue length " + lLength + " > " + Integer.MAX_VALUE);
506     }
507     int iLength = (int) lLength;
508     if (buffer.length - boffset < iLength) {
509       throw new IllegalArgumentException("Buffer size " + (buffer.length - boffset) + " < "
510           + iLength);
511     }
512 
513     int len = KeyValue.writeByteArray(buffer, boffset, row, roffset, rlength, family, foffset,
514         flength, qualifier, qoffset, qlength, HConstants.LATEST_TIMESTAMP, KeyValue.Type.Maximum,
515         null, 0, 0, null);
516     return new KeyValue(buffer, boffset, len);
517   }
518 
519   /**
520    * Creates the first KV with the row/family/qualifier of this KV and the
521    * given timestamp. Uses the "maximum" KV type that guarantees that the new
522    * KV is the lowest possible for this combination of row, family, qualifier,
523    * and timestamp. This KV's own timestamp is ignored. While this function
524    * copies the value from this KV, it is normally used on key-only KVs.
525    */
526   public static KeyValue createFirstOnRowColTS(KeyValue kv, long ts) {
527     return new KeyValue(
528         kv.getRowArray(), kv.getRowOffset(), kv.getRowLength(),
529         kv.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyLength(),
530         kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength(),
531         ts, Type.Maximum, kv.getValueArray(), kv.getValueOffset(), kv.getValueLength());
532   }
533 
534   /*************** misc **********************************/
535   /**
536    * @param cell
537    * @return <code>cell</code> if it is an instance of {@link KeyValue} else we will return a
538    * new {@link KeyValue} instance made from <code>cell</code>
539    * @deprecated without any replacement.
540    */
541   @Deprecated
542   public static KeyValue ensureKeyValue(final Cell cell) {
543     if (cell == null) return null;
544     return cell instanceof KeyValue? (KeyValue)cell: copyToNewKeyValue(cell);
545   }
546 
547   /**
548    * @param cell
549    * @return <code>cell</code> if it is an object of class {@link KeyValue} else we will return a
550    * new {@link KeyValue} instance made from <code>cell</code> Note: Even if the cell is an object
551    * of any of the subclass of {@link KeyValue}, we will create a new {@link KeyValue} object
552    * wrapping same buffer. This API should be used only with MR based tools which expect the type
553    * to be exactly KeyValue. That is the reason for doing this way.
554    *
555    * @deprecated without any replacement.
556    */
557   @Deprecated
558   public static KeyValue ensureKeyValueTypeForMR(final Cell cell) {
559     if (cell == null) return null;
560     if (cell instanceof KeyValue) {
561       if (cell.getClass().getName().equals(KeyValue.class.getName())) {
562         return (KeyValue) cell;
563       }
564       // Cell is an Object of any of the sub classes of KeyValue. Make a new KeyValue wrapping the
565       // same byte[]
566       KeyValue kv = (KeyValue) cell;
567       KeyValue newKv = new KeyValue(kv.bytes, kv.offset, kv.length);
568       newKv.setSequenceId(kv.getSequenceId());
569       return newKv;
570     }
571     return copyToNewKeyValue(cell);
572   }
573 
574   @Deprecated
575   public static List<KeyValue> ensureKeyValues(List<Cell> cells) {
576     List<KeyValue> lazyList = Lists.transform(cells, new Function<Cell, KeyValue>() {
577       @Override
578       public KeyValue apply(Cell arg0) {
579         return KeyValueUtil.ensureKeyValue(arg0);
580       }
581     });
582     return new ArrayList<KeyValue>(lazyList);
583   }
584 
585   /**
586    * Create a KeyValue reading from the raw InputStream. Named
587    * <code>iscreate</code> so doesn't clash with the <code>create(DataInput)</code> method
588    * added in 2.0
589    *
590    * @param in
591    * @param withTags
592    *          whether the keyvalue should include tags are not
593    * @return Created KeyValue OR if we find a length of zero, we will return
594    *         null which can be useful marking a stream as done.
595    * @throws IOException
596    */
597   public static KeyValue iscreate(final InputStream in, boolean withTags) throws IOException {
598     byte[] intBytes = new byte[Bytes.SIZEOF_INT];
599     int bytesRead = 0;
600     while (bytesRead < intBytes.length) {
601       int n = in.read(intBytes, bytesRead, intBytes.length - bytesRead);
602       if (n < 0) {
603         if (bytesRead == 0) {
604           throw new EOFException();
605         }
606         throw new IOException("Failed read of int, read " + bytesRead + " bytes");
607       }
608       bytesRead += n;
609     }
610     // TODO: perhaps some sanity check is needed here.
611     byte[] bytes = new byte[Bytes.toInt(intBytes)];
612     IOUtils.readFully(in, bytes, 0, bytes.length);
613     if (withTags) {
614       return new KeyValue(bytes, 0, bytes.length);
615     } else {
616       return new NoTagsKeyValue(bytes, 0, bytes.length);
617     }
618   }
619 
620   public static void oswrite(final Cell cell, final OutputStream out, final boolean withTags)
621       throws IOException {
622     if (cell instanceof KeyValue) {
623       KeyValue.oswrite((KeyValue) cell, out, withTags);
624     } else {
625       short rlen = cell.getRowLength();
626       byte flen = cell.getFamilyLength();
627       int qlen = cell.getQualifierLength();
628       int vlen = cell.getValueLength();
629       int tlen = cell.getTagsLength();
630 
631       // write total length
632       StreamUtils.writeInt(out, length(rlen, flen, qlen, vlen, tlen, withTags));
633       // write key length
634       StreamUtils.writeInt(out, keyLength(rlen, flen, qlen));
635       // write value length
636       StreamUtils.writeInt(out, vlen);
637       // Write rowkey - 2 bytes rk length followed by rowkey bytes
638       StreamUtils.writeShort(out, rlen);
639       out.write(cell.getRowArray(), cell.getRowOffset(), rlen);
640       // Write cf - 1 byte of cf length followed by the family bytes
641       out.write(flen);
642       out.write(cell.getFamilyArray(), cell.getFamilyOffset(), flen);
643       // write qualifier
644       out.write(cell.getQualifierArray(), cell.getQualifierOffset(), qlen);
645       // write timestamp
646       StreamUtils.writeLong(out, cell.getTimestamp());
647       // write the type
648       out.write(cell.getTypeByte());
649       // write value
650       out.write(cell.getValueArray(), cell.getValueOffset(), vlen);
651       // write tags if we have to
652       if (withTags && tlen > 0) {
653         // 2 bytes tags length followed by tags bytes
654         // tags length is serialized with 2 bytes only(short way) even if the type is int. As this
655         // is non -ve numbers, we save the sign bit. See HBASE-11437
656         out.write((byte) (0xff & (tlen >> 8)));
657         out.write((byte) (0xff & tlen));
658         out.write(cell.getTagsArray(), cell.getTagsOffset(), tlen);
659       }
660     }
661   }
662 }