App Engine Java SDK version 1.7.0
[gae.git] / java / src / main / com / google / appengine / api / files / RecordConstants.java
blob3ad1ea117569cb454889fbbb0bdd1dfc88e6587c
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 final class RecordConstants {
11 /**
12 * Size of a block.
14 public static final int BLOCK_SIZE = 32 * 1024;
16 /**
17 * Header length in data.
19 public static final int HEADER_LENGTH = 7;
21 /**
22 * CRC Mask. Comes from http://leveldb.googlecode.com/svn/trunk/util/crc32c.h
24 public static final int CRC_MASK_DELTA = 0xa282ead8;
26 /**
27 * An enumerated type that describes the type of the record being stored.
28 * These byte values must match those of the leveldb log format for
29 * compatibility between different implementations
30 * of the {@link RecordWriteChannelImpl}.
33 public enum RecordType {
34 NONE((byte) 0x00),
35 FULL((byte) 0x01),
36 FIRST((byte) 0x02),
37 MIDDLE((byte) 0x03),
38 LAST((byte) 0x04),
39 UNKNOWN((byte) 0xFF);
41 private byte value;
43 private RecordType(byte value) {
44 this.value = value;
46 /**
47 * The byte value of the record type is written to the file as part of the
48 * header.
49 * @return the byte value of the record type.
51 public byte value() {
52 return this.value;
55 /**
56 * Converts a byte value into a {@link RecordType} enum.
57 * @param value the byte value of the {@link RecordType} you want.
58 * @return a {@link RecordType} that corresponds to the inputed byte value.
60 public static RecordType get(byte value) {
61 switch(value) {
62 case 0x00: return NONE;
63 case 0x01: return FULL;
64 case 0x02: return FIRST;
65 case 0x03: return MIDDLE;
66 case 0x04: return LAST;
67 default: return UNKNOWN;
72 /**
73 * Masks the crc.
75 * Motivation taken from leveldb:
76 * it is problematic to compute the CRC of a string that
77 * contains embedded CRCs. Therefore we recommend that CRCs stored
78 * somewhere (e.g., in files) should be masked before being stored.
79 * @param crc the crc to mask.
80 * @return the masked crc.
82 public static long maskCrc(long crc) {
83 return ((((crc >> 15) | (crc << 17)) + RecordConstants.CRC_MASK_DELTA) & 0xFFFFFFFFL);
86 /**
87 * Unmasks the crc.
88 * @param maskedCrc a masked crc.
89 * @return an unmasked crc.
91 public static long unmaskCrc(long maskedCrc) {
92 long rot = (maskedCrc - CRC_MASK_DELTA) & 0xFFFFFFFFL;
93 return (((rot >> 17) | (rot << 15)) & 0xFFFFFFFFL);