001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.hadoop.hdfs.protocol;
019    
020    import org.apache.hadoop.io.Writable;
021    import org.apache.hadoop.io.Text;
022    import java.io.DataInput;
023    import java.io.DataOutput;
024    import java.io.IOException;
025    import java.util.Arrays;
026    
027    /**
028     * Contains a list of paths corresponding to corrupt files and a cookie
029     * used for iterative calls to NameNode.listCorruptFileBlocks.
030     *
031     */
032    public class CorruptFileBlocks implements Writable {
033      // used for hashCode
034      private static final int PRIME = 16777619;
035    
036      private String[] files;
037      private String cookie;
038    
039      public CorruptFileBlocks() {
040        this(new String[0], "");
041      }
042    
043      public CorruptFileBlocks(String[] files, String cookie) {
044        this.files = files;
045        this.cookie = cookie;
046      }
047    
048      public String[] getFiles() {
049        return files;
050      }
051    
052      public String getCookie() {
053        return cookie;
054      }
055    
056      /**
057       * {@inheritDoc}
058       */
059      @Override
060      public void readFields(DataInput in) throws IOException {
061        int fileCount = in.readInt();
062        files = new String[fileCount];
063        for (int i = 0; i < fileCount; i++) {
064          files[i] = Text.readString(in);
065        }
066        cookie = Text.readString(in);
067      }
068    
069      /**
070       * {@inheritDoc}
071       */
072      @Override
073      public void write(DataOutput out) throws IOException {
074        out.writeInt(files.length);
075        for (int i = 0; i < files.length; i++) {
076          Text.writeString(out, files[i]);
077        }
078        Text.writeString(out, cookie);
079      }
080    
081      /**
082       * {@inheritDoc}
083       */
084      public boolean equals(Object obj) {
085        if (this == obj) {
086          return true;
087        }
088        if (!(obj instanceof CorruptFileBlocks)) {
089          return false;
090        }
091        CorruptFileBlocks other = (CorruptFileBlocks) obj;
092        return cookie.equals(other.cookie) &&
093          Arrays.equals(files, other.files);
094      }
095    
096      /**
097       * {@inheritDoc}
098       */
099      public int hashCode() {
100        int result = cookie.hashCode();
101    
102        for (String file : files) {
103          result = PRIME * result + file.hashCode();
104        }
105    
106        return result;
107      }
108    }