@@ -40,7 +40,7 @@ public class CompressionCodecFactory {
4040 LoggerFactory .getLogger (CompressionCodecFactory .class .getName ());
4141
4242 private static final ServiceLoader <CompressionCodec > CODEC_PROVIDERS =
43- ServiceLoader .load (CompressionCodec .class );
43+ ServiceLoader .load (CompressionCodec .class );
4444
4545 /**
4646 * A map from the reversed filename suffixes to the codecs.
@@ -49,15 +49,15 @@ public class CompressionCodecFactory {
4949 */
5050 private SortedMap <String , CompressionCodec > codecs = null ;
5151
52- /**
53- * A map from the reversed filename suffixes to the codecs.
54- * This is probably overkill, because the maps should be small, but it
55- * automatically supports finding the longest matching suffix.
56- */
57- private Map <String , CompressionCodec > codecsByName = null ;
52+ /**
53+ * A map from the reversed filename suffixes to the codecs.
54+ * This is probably overkill, because the maps should be small, but it
55+ * automatically supports finding the longest matching suffix.
56+ */
57+ private Map <String , CompressionCodec > codecsByName = null ;
5858
5959 /**
60- * A map from class names to the codecs
60+ * A map from class names to the codecs.
6161 */
6262 private HashMap <String , CompressionCodec > codecsByClassName = null ;
6363
@@ -80,8 +80,8 @@ private void addCodec(CompressionCodec codec) {
8080 @ Override
8181 public String toString () {
8282 StringBuilder buf = new StringBuilder ();
83- Iterator <Map .Entry <String , CompressionCodec >> itr =
84- codecs .entrySet ().iterator ();
83+ Iterator <Map .Entry <String , CompressionCodec >> itr =
84+ codecs .entrySet ().iterator ();
8585 buf .append ("{ " );
8686 if (itr .hasNext ()) {
8787 Map .Entry <String , CompressionCodec > entry = itr .next ();
@@ -110,8 +110,8 @@ public String toString() {
110110 */
111111 public static List <Class <? extends CompressionCodec >> getCodecClasses (
112112 Configuration conf ) {
113- List <Class <? extends CompressionCodec >> result
114- = new ArrayList <Class <? extends CompressionCodec >>();
113+ List <Class <? extends CompressionCodec >> result =
114+ new ArrayList <Class <? extends CompressionCodec >>();
115115 // Add codec classes discovered via service loading
116116 synchronized (CODEC_PROVIDERS ) {
117117 // CODEC_PROVIDERS is a lazy collection. Synchronize so it is
@@ -200,11 +200,13 @@ public CompressionCodec getCodec(Path file) {
200200 String filename = file .getName ();
201201 String reversedFilename =
202202 new StringBuilder (filename ).reverse ().toString ();
203- SortedMap <String , CompressionCodec > subMap =
204- codecs .headMap (reversedFilename );
203+ String lowerReversedFilename =
204+ StringUtils .toLowerCase (reversedFilename );
205+ SortedMap <String , CompressionCodec > subMap =
206+ codecs .headMap (lowerReversedFilename );
205207 if (!subMap .isEmpty ()) {
206208 String potentialSuffix = subMap .lastKey ();
207- if (reversedFilename .startsWith (potentialSuffix )) {
209+ if (lowerReversedFilename .startsWith (potentialSuffix )) {
208210 result = codecs .get (potentialSuffix );
209211 }
210212 }
@@ -224,57 +226,57 @@ public CompressionCodec getCodecByClassName(String classname) {
224226 return codecsByClassName .get (classname );
225227 }
226228
227- /**
228- * Find the relevant compression codec for the codec's canonical class name
229- * or by codec alias.
230- * <p>
231- * Codec aliases are case insensitive.
232- * <p>
233- * The code alias is the short class name (without the package name).
234- * If the short class name ends with 'Codec', then there are two aliases for
235- * the codec, the complete short class name and the short class name without
236- * the 'Codec' ending. For example for the 'GzipCodec' codec class name the
237- * alias are 'gzip' and 'gzipcodec'.
238- *
239- * @param codecName the canonical class name of the codec
240- * @return the codec object
241- */
242- public CompressionCodec getCodecByName (String codecName ) {
243- if (codecsByClassName == null ) {
244- return null ;
245- }
246- CompressionCodec codec = getCodecByClassName (codecName );
247- if (codec == null ) {
248- // trying to get the codec by name in case the name was specified
249- // instead a class
250- codec = codecsByName .get (StringUtils .toLowerCase (codecName ));
251- }
252- return codec ;
229+ /**
230+ * Find the relevant compression codec for the codec's canonical class name
231+ * or by codec alias.
232+ * <p>
233+ * Codec aliases are case insensitive.
234+ * <p>
235+ * The code alias is the short class name (without the package name).
236+ * If the short class name ends with 'Codec', then there are two aliases for
237+ * the codec, the complete short class name and the short class name without
238+ * the 'Codec' ending. For example for the 'GzipCodec' codec class name the
239+ * alias are 'gzip' and 'gzipcodec'.
240+ *
241+ * @param codecName the canonical class name of the codec
242+ * @return the codec object
243+ */
244+ public CompressionCodec getCodecByName (String codecName ) {
245+ if (codecsByClassName == null ) {
246+ return null ;
253247 }
248+ CompressionCodec codec = getCodecByClassName (codecName );
249+ if (codec == null ) {
250+ // trying to get the codec by name in case the name was specified
251+ // instead a class
252+ codec = codecsByName .get (StringUtils .toLowerCase (codecName ));
253+ }
254+ return codec ;
255+ }
254256
255- /**
256- * Find the relevant compression codec for the codec's canonical class name
257- * or by codec alias and returns its implemetation class.
258- * <p>
259- * Codec aliases are case insensitive.
260- * <p>
261- * The code alias is the short class name (without the package name).
262- * If the short class name ends with 'Codec', then there are two aliases for
263- * the codec, the complete short class name and the short class name without
264- * the 'Codec' ending. For example for the 'GzipCodec' codec class name the
265- * alias are 'gzip' and 'gzipcodec'.
266- *
267- * @param codecName the canonical class name of the codec
268- * @return the codec class
269- */
270- public Class <? extends CompressionCodec > getCodecClassByName (
271- String codecName ) {
272- CompressionCodec codec = getCodecByName (codecName );
273- if (codec == null ) {
274- return null ;
275- }
276- return codec .getClass ();
257+ /**
258+ * Find the relevant compression codec for the codec's canonical class name
259+ * or by codec alias and returns its implemetation class.
260+ * <p>
261+ * Codec aliases are case insensitive.
262+ * <p>
263+ * The code alias is the short class name (without the package name).
264+ * If the short class name ends with 'Codec', then there are two aliases for
265+ * the codec, the complete short class name and the short class name without
266+ * the 'Codec' ending. For example for the 'GzipCodec' codec class name the
267+ * alias are 'gzip' and 'gzipcodec'.
268+ *
269+ * @param codecName the canonical class name of the codec
270+ * @return the codec class
271+ */
272+ public Class <? extends CompressionCodec > getCodecClassByName (
273+ String codecName ) {
274+ CompressionCodec codec = getCodecByName (codecName );
275+ if (codec == null ) {
276+ return null ;
277277 }
278+ return codec .getClass ();
279+ }
278280
279281 /**
280282 * Removes a suffix from a filename, if it has it.
@@ -323,8 +325,12 @@ public static void main(String[] args) throws Exception {
323325 len = in .read (buffer );
324326 }
325327 } finally {
326- if (out != null ) { out .close (); }
327- if (in != null ) { in .close (); }
328+ if (out != null ) {
329+ out .close ();
330+ }
331+ if (in != null ) {
332+ in .close ();
333+ }
328334 }
329335 } else {
330336 CompressionInputStream in = null ;
@@ -338,7 +344,9 @@ public static void main(String[] args) throws Exception {
338344 len = in .read (buffer );
339345 }
340346 } finally {
341- if (in != null ) { in .close (); }
347+ if (in != null ) {
348+ in .close ();
349+ }
342350 }
343351 }
344352 }
0 commit comments