1 2 3 4 package net.nutch.protocol; 5 6 import java.util.*; 7 import java.io.*; 8 9 import net.nutch.io.*; 10 import net.nutch.fs.*; 11 import net.nutch.util.*; 12 13 public final class Content extends VersionedWritable { 14 15 public static final String DIR_NAME = "content"; 16 17 private final static byte VERSION = 1; 18 19 private String url; 20 private String base; 21 private byte[] content; 22 private String contentType; 23 private Properties metadata; 24 25 public Content() {} 26 27 public Content(String url, String base, byte[] content, String contentType, 28 Properties metadata){ 29 30 if (url == null) throw new IllegalArgumentException ("null url"); 31 if (base == null) throw new IllegalArgumentException ("null base"); 32 if (content == null) throw new IllegalArgumentException ("null content"); 33 if (contentType == null) throw new IllegalArgumentException ("null type"); 34 if (metadata == null) throw new IllegalArgumentException ("null metadata"); 35 36 this.url = url; 37 this.base = base; 38 this.content = content; 39 this.contentType = contentType; 40 this.metadata = metadata; 41 } 42 43 public byte getVersion() { return VERSION; } 44 45 public final void readFields(DataInput in) throws IOException { 46 super.readFields(in); 48 url = UTF8.readString(in); base = UTF8.readString(in); 51 content = WritableUtils.readCompressedByteArray(in); 52 53 contentType = UTF8.readString(in); 55 int propertyCount = in.readInt(); metadata = new Properties(); 57 for (int i = 0; i < propertyCount; i++) { 58 metadata.put(UTF8.readString(in), UTF8.readString(in)); 59 } 60 } 61 62 public final void write(DataOutput out) throws IOException { 63 super.write(out); 65 UTF8.writeString(out, url); UTF8.writeString(out, base); 68 WritableUtils.writeCompressedByteArray(out, content); 70 UTF8.writeString(out, contentType); 72 out.writeInt(metadata.size()); Iterator i = metadata.entrySet().iterator(); 74 while (i.hasNext()) { 75 Map.Entry e = (Map.Entry)i.next(); 76 UTF8.writeString(out, (String )e.getKey()); 77 UTF8.writeString(out, (String )e.getValue()); 78 } 79 } 80 81 public static Content read(DataInput in) throws IOException { 82 Content content = new Content(); 83 content.readFields(in); 84 return content; 85 } 86 87 91 92 public String getUrl() { return url; } 93 94 97 public String getBaseUrl() { return base; } 98 99 100 public byte[] getContent() { return content; } 101 public void setContent(byte[] content) { this.content = content; } 102 103 106 public String getContentType() { return contentType; } 107 public void setContentType(String contentType) { 108 this.contentType = contentType; 109 } 110 111 112 public Properties getMetadata() { return metadata; } 113 114 115 public String get(String name) { return getMetadata().getProperty(name); } 116 117 public boolean equals(Object o) { 118 if (!(o instanceof Content)){ 119 return false; 120 } 121 Content that = (Content)o; 122 return 123 this.url.equals(that.url) && 124 this.base.equals(that.base) && 125 Arrays.equals(this.getContent(), that.getContent()) && 126 this.contentType.equals(that.contentType) && 127 this.metadata.equals(that.metadata); 128 } 129 130 public String toString() { 131 StringBuffer buffer = new StringBuffer (); 132 133 buffer.append("url: " + url + "\n" ); 134 buffer.append("base: " + base + "\n" ); 135 buffer.append("contentType: " + contentType + "\n" ); 136 buffer.append("metadata: " + metadata + "\n" ); 137 buffer.append("Content:\n"); 138 buffer.append(new String (content)); 140 return buffer.toString(); 141 142 } 143 144 public static void main(String argv[]) throws Exception { 145 146 String usage = "Content (-local | -ndfs <namenode:port>) recno segment"; 147 148 if (argv.length < 3) { 149 System.out.println("usage:" + usage); 150 return; 151 } 152 153 NutchFileSystem nfs = NutchFileSystem.parseArgs(argv, 0); 154 try { 155 int recno = Integer.parseInt(argv[0]); 156 String segment = argv[1]; 157 158 File file = new File(segment, DIR_NAME); 159 System.out.println("Reading from file: " + file); 160 161 ArrayFile.Reader contents = new ArrayFile.Reader(nfs, file.toString()); 162 163 Content content = new Content(); 164 contents.get(recno, content); 165 System.out.println("Retrieved " + recno + " from file " + file); 166 167 System.out.println(content); 168 169 contents.close(); 170 } finally { 171 nfs.close(); 172 } 173 } 174 } 175 | Popular Tags |