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 | package org.apache.hadoop.fs; |
---|
19 | |
---|
20 | import java.io.DataInput; |
---|
21 | import java.io.DataOutput; |
---|
22 | import java.io.IOException; |
---|
23 | |
---|
24 | import org.apache.hadoop.io.MD5Hash; |
---|
25 | import org.apache.hadoop.io.WritableUtils; |
---|
26 | import org.xml.sax.Attributes; |
---|
27 | import org.xml.sax.SAXException; |
---|
28 | import org.znerd.xmlenc.XMLOutputter; |
---|
29 | |
---|
30 | /** MD5 of MD5 of CRC32. */ |
---|
31 | public class MD5MD5CRC32FileChecksum extends FileChecksum { |
---|
32 | public static final int LENGTH = MD5Hash.MD5_LEN |
---|
33 | + (Integer.SIZE + Long.SIZE)/Byte.SIZE; |
---|
34 | |
---|
35 | private int bytesPerCRC; |
---|
36 | private long crcPerBlock; |
---|
37 | private MD5Hash md5; |
---|
38 | |
---|
39 | /** Same as this(0, 0, null) */ |
---|
40 | public MD5MD5CRC32FileChecksum() { |
---|
41 | this(0, 0, null); |
---|
42 | } |
---|
43 | |
---|
44 | /** Create a MD5FileChecksum */ |
---|
45 | public MD5MD5CRC32FileChecksum(int bytesPerCRC, long crcPerBlock, MD5Hash md5) { |
---|
46 | this.bytesPerCRC = bytesPerCRC; |
---|
47 | this.crcPerBlock = crcPerBlock; |
---|
48 | this.md5 = md5; |
---|
49 | } |
---|
50 | |
---|
51 | /** {@inheritDoc} */ |
---|
52 | public String getAlgorithmName() { |
---|
53 | return "MD5-of-" + crcPerBlock + "MD5-of-" + bytesPerCRC + "CRC32"; |
---|
54 | } |
---|
55 | |
---|
56 | /** {@inheritDoc} */ |
---|
57 | public int getLength() {return LENGTH;} |
---|
58 | |
---|
59 | /** {@inheritDoc} */ |
---|
60 | public byte[] getBytes() { |
---|
61 | return WritableUtils.toByteArray(this); |
---|
62 | } |
---|
63 | |
---|
64 | /** {@inheritDoc} */ |
---|
65 | public void readFields(DataInput in) throws IOException { |
---|
66 | bytesPerCRC = in.readInt(); |
---|
67 | crcPerBlock = in.readLong(); |
---|
68 | md5 = MD5Hash.read(in); |
---|
69 | } |
---|
70 | |
---|
71 | /** {@inheritDoc} */ |
---|
72 | public void write(DataOutput out) throws IOException { |
---|
73 | out.writeInt(bytesPerCRC); |
---|
74 | out.writeLong(crcPerBlock); |
---|
75 | md5.write(out); |
---|
76 | } |
---|
77 | |
---|
78 | /** Write that object to xml output. */ |
---|
79 | public static void write(XMLOutputter xml, MD5MD5CRC32FileChecksum that |
---|
80 | ) throws IOException { |
---|
81 | xml.startTag(MD5MD5CRC32FileChecksum.class.getName()); |
---|
82 | if (that != null) { |
---|
83 | xml.attribute("bytesPerCRC", "" + that.bytesPerCRC); |
---|
84 | xml.attribute("crcPerBlock", "" + that.crcPerBlock); |
---|
85 | xml.attribute("md5", "" + that.md5); |
---|
86 | } |
---|
87 | xml.endTag(); |
---|
88 | } |
---|
89 | |
---|
90 | /** Return the object represented in the attributes. */ |
---|
91 | public static MD5MD5CRC32FileChecksum valueOf(Attributes attrs |
---|
92 | ) throws SAXException { |
---|
93 | final String bytesPerCRC = attrs.getValue("bytesPerCRC"); |
---|
94 | final String crcPerBlock = attrs.getValue("crcPerBlock"); |
---|
95 | final String md5 = attrs.getValue("md5"); |
---|
96 | if (bytesPerCRC == null || crcPerBlock == null || md5 == null) { |
---|
97 | return null; |
---|
98 | } |
---|
99 | |
---|
100 | try { |
---|
101 | return new MD5MD5CRC32FileChecksum(Integer.valueOf(bytesPerCRC), |
---|
102 | Integer.valueOf(crcPerBlock), new MD5Hash(md5)); |
---|
103 | } catch(Exception e) { |
---|
104 | throw new SAXException("Invalid attributes: bytesPerCRC=" + bytesPerCRC |
---|
105 | + ", crcPerBlock=" + crcPerBlock + ", md5=" + md5, e); |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | /** {@inheritDoc} */ |
---|
110 | public String toString() { |
---|
111 | return getAlgorithmName() + ":" + md5; |
---|
112 | } |
---|
113 | } |
---|