Changeset 3012 for OAIHarvester
- Timestamp:
- 06/10/13 22:02:39 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
OAIHarvester/trunk/OAIHarvester/src/test/java/eu/clarin/cmdi/oai/harvester/HarvesterTest.java
r3011 r3012 1 1 package eu.clarin.cmdi.oai.harvester; 2 2 3 import java.io.BufferedInputStream;4 3 import java.io.File; 4 import java.io.FileInputStream; 5 5 import java.io.FileOutputStream; 6 import java.io.FilterInputStream; 6 7 import java.io.IOException; 7 8 import java.io.InputStream; 9 import java.io.OutputStream; 8 10 import java.util.Date; 9 11 import java.util.List; 10 12 import java.util.Properties; 13 import java.util.zip.GZIPInputStream; 14 import java.util.zip.GZIPOutputStream; 11 15 import java.util.zip.ZipEntry; 12 16 import java.util.zip.ZipException; … … 38 42 private int reqNum; 39 43 private String prefix; 40 private byte[] buffer = new byte[ 8192];44 private byte[] buffer = new byte[16 * 1024]; 41 45 42 46 public MyHarvestHandler(File file, boolean saveHeader) { … … 55 59 if (output != null) { 56 60 output.flush(); 61 output.finish(); 57 62 output.close(); 58 63 output = null; … … 194 199 @Override 195 200 public InputStream wrap(InputStream stream) throws IOException { 196 final int size = 16*1024*1024; 197 BufferedInputStream in = 198 new BufferedInputStream(stream, size); 199 in.mark(size); 200 String x = Integer.toHexString(reqNum++); 201 while (x.length() < 4) { 202 x = "0" + x; 203 } 204 final String filename = "request/0x" + x + ".xml"; 205 System.err.println("FN: " + filename); 206 ZipEntry entry = new ZipEntry(filename); 207 output.putNextEntry(entry); 208 int r = -1; 209 do { 210 r = in.read(buffer, 0, buffer.length - 1); 211 if (r > 0) { 212 output.write(buffer, 0, r); 213 } 214 } while (r > 0); 215 output.closeEntry(); 216 output.flush(); 217 in.reset(); 218 return in; 201 /* 202 * !!! WARNING WARNING WARNING WARNING WARNING WARNING WARNING !!! 203 * Do not use this in production. This is just for demo purposes 204 * to also store the raw requests in the ZIP file. This code is 205 * highly inefficient! 206 * For production, just "return stream" in this method. If you're 207 * using the HarvestHandlerAdapter class as super class, just do 208 * not override this method. 209 * !!! WARNING WARNING WARNING WARNING WARNING WARNING WARNING !!! 210 * 211 */ 212 return new FilterInputStream(stream) { 213 private final File tmpFile = 214 File.createTempFile("oai-harvester", null); 215 private final OutputStream tmpStream = 216 new GZIPOutputStream(new FileOutputStream(tmpFile)); 217 218 @Override 219 public int read(byte[] b, int off, int len) throws IOException { 220 final int read = super.read(b, off, len); 221 if (read > 0) { 222 tmpStream.write(b, off, read); 223 } 224 return read; 225 } 226 227 @Override 228 public void close() throws IOException { 229 super.close(); 230 231 try { 232 tmpStream.close(); 233 234 final String filename = 235 String.format("request/0x%04X.xml", reqNum++); 236 System.err.println("FN: " + filename); 237 238 InputStream in = null; 239 try { 240 ZipEntry entry = new ZipEntry(filename); 241 output.putNextEntry(entry); 242 243 in = new GZIPInputStream( 244 new FileInputStream(tmpFile)); 245 for (;;) { 246 int read = in.read(buffer); 247 if (read <= 0) { 248 break; 249 } 250 output.write(buffer, 0, read); 251 } 252 in.close(); 253 } catch (IOException e) { 254 e.printStackTrace(); 255 } finally { 256 if (in != null) { 257 try { 258 in.close(); 259 } catch (IOException e) { 260 } 261 } 262 output.closeEntry(); 263 } 264 } finally { 265 tmpFile.delete(); 266 } 267 } 268 }; 219 269 } 220 270
Note: See TracChangeset
for help on using the changeset viewer.