1.9.30 sync.
[gae.git] / java / src / main / com / google / appengine / api / files / RecordConstants.java
blob43c7156b253c3ded38efe75f3a406ee5262abee2
1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.files;
5 /**
6 * A set of constants needed by {@link RecordReadChannelImpl} and {@link RecordWriteChannelImpl}.
8 */
9 @Deprecated
10 final class RecordConstants {
12 /**
13 * Size of a block.
15 public static final int BLOCK_SIZE = 32 * 1024;
17 /**
18 * Header length in data.
20 public static final int HEADER_LENGTH = 7;
22 /**
23 * CRC Mask. Comes from http://leveldb.googlecode.com/svn/trunk/util/crc32c.h
25 public static final int CRC_MASK_DELTA = 0xa282ead8;
27 /**
28 * An enumerated type that describes the type of the record being stored.
29 * These byte values must match those of the leveldb log format for
30 * compatibility between different implementations
31 * of the {@link RecordWriteChannelImpl}.
34 public enum RecordType {
35 NONE((byte) 0x00),
36 FULL((byte) 0x01),
37 FIRST((byte) 0x02),
38 MIDDLE((byte) 0x03),
39 LAST((byte) 0x04),
40 UNKNOWN((byte) 0xFF);
42 private byte value;
44 private RecordType(byte value) {
45 this.value = value;
47 /**
48 * The byte value of the record type is written to the file as part of the
49 * header.
50 * @return the byte value of the record type.
52 public byte value() {
53 return this.value;
56 /**
57 * Converts a byte value into a {@link RecordType} enum.
58 * @param value the byte value of the {@link RecordType} you want.
59 * @return a {@link RecordType} that corresponds to the inputed byte value.
61 public static RecordType get(byte value) {
62 switch(value) {
63 case 0x00: return NONE;
64 case 0x01: return FULL;
65 case 0x02: return FIRST;
66 case 0x03: return MIDDLE;
67 case 0x04: return LAST;
68 default: return UNKNOWN;
73 /**
74 * Masks the crc.
76 * Motivation taken from leveldb:
77 * it is problematic to compute the CRC of a string that
78 * contains embedded CRCs. Therefore we recommend that CRCs stored
79 * somewhere (e.g., in files) should be masked before being stored.
80 * @param crc the crc to mask.
81 * @return the masked crc.
83 public static long maskCrc(long crc) {
84 return ((((crc >> 15) | (crc << 17)) + RecordConstants.CRC_MASK_DELTA) & 0xFFFFFFFFL);
87 /**
88 * Unmasks the crc.
89 * @param maskedCrc a masked crc.
90 * @return an unmasked crc.
92 public static long unmaskCrc(long maskedCrc) {
93 long rot = (maskedCrc - CRC_MASK_DELTA) & 0xFFFFFFFFL;
94 return (((rot >> 17) | (rot << 15)) & 0xFFFFFFFFL);