1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.util;
20
21 import java.io.IOException;
22 import java.lang.reflect.Constructor;
23 import java.util.zip.Checksum;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28
29
30
31
32
33
34 public enum ChecksumType {
35
36 NULL((byte)0) {
37 @Override
38 public String getName() {
39 return "NULL";
40 }
41 @Override
42 public void initialize() {
43
44 }
45 @Override
46 public Checksum getChecksumObject() throws IOException {
47 return null;
48 }
49 },
50
51 CRC32((byte)1) {
52 private transient Constructor<?> ctor;
53
54 @Override
55 public String getName() {
56 return "CRC32";
57 }
58
59 @Override
60 public void initialize() {
61 final String PURECRC32 = "org.apache.hadoop.util.PureJavaCrc32";
62 final String JDKCRC = "java.util.zip.CRC32";
63 LOG = LogFactory.getLog(ChecksumType.class);
64
65
66 try {
67 ctor = ChecksumFactory.newConstructor(PURECRC32);
68 LOG.info("Checksum using " + PURECRC32);
69 } catch (Exception e) {
70 LOG.trace(PURECRC32 + " not available.");
71 }
72 try {
73
74
75 if (ctor == null) {
76 ctor = ChecksumFactory.newConstructor(JDKCRC);
77 LOG.info("Checksum can use " + JDKCRC);
78 }
79 } catch (Exception e) {
80 LOG.trace(JDKCRC + " not available.");
81 }
82 }
83
84 @Override
85 public Checksum getChecksumObject() throws IOException {
86 if (ctor == null) {
87 throw new IOException("Bad constructor for " + getName());
88 }
89 try {
90 return (Checksum)ctor.newInstance();
91 } catch (Exception e) {
92 throw new IOException(e);
93 }
94 }
95 },
96
97 CRC32C((byte)2) {
98 private transient Constructor<?> ctor;
99
100 @Override
101 public String getName() {
102 return "CRC32C";
103 }
104
105 @Override
106 public void initialize() {
107 final String PURECRC32C = "org.apache.hadoop.util.PureJavaCrc32C";
108 LOG = LogFactory.getLog(ChecksumType.class);
109 try {
110 ctor = ChecksumFactory.newConstructor(PURECRC32C);
111 LOG.info("Checksum can use " + PURECRC32C);
112 } catch (Exception e) {
113 LOG.trace(PURECRC32C + " not available.");
114 }
115 }
116
117 @Override
118 public Checksum getChecksumObject() throws IOException {
119 if (ctor == null) {
120 throw new IOException("Bad constructor for " + getName());
121 }
122 try {
123 return (Checksum)ctor.newInstance();
124 } catch (Exception e) {
125 throw new IOException(e);
126 }
127 }
128 };
129
130 private final byte code;
131 protected Log LOG;
132
133
134 abstract void initialize();
135
136
137 public abstract String getName();
138
139 private ChecksumType(final byte c) {
140 this.code = c;
141 initialize();
142 }
143
144
145 public abstract Checksum getChecksumObject() throws IOException;
146
147 public byte getCode() {
148 return this.code;
149 }
150
151
152
153
154
155
156
157 public static ChecksumType codeToType(final byte b) {
158 for (ChecksumType t : ChecksumType.values()) {
159 if (t.getCode() == b) {
160 return t;
161 }
162 }
163 throw new RuntimeException("Unknown checksum type code " + b);
164 }
165
166
167
168
169
170
171
172 public static ChecksumType nameToType(final String name) {
173 for (ChecksumType t : ChecksumType.values()) {
174 if (t.getName().equals(name)) {
175 return t;
176 }
177 }
178 throw new RuntimeException("Unknown checksum type name " + name);
179 }
180 }