Commit 200e4ea6 authored by toaster's avatar toaster
Browse files

git-svn-id: https://subversion.umiacs.umd.edu/ace/trunk@41 f1b3a171-7291-4a19-a512-95ad0ad9394a
parent 5fd6ed50
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>ace</artifactId>
<groupId>edu.umiacs.ace</groupId>
<version>1.6-SNAPSHOT</version>
</parent>
<groupId>edu.umiacs.ace</groupId>
<artifactId>ace-common</artifactId>
<version>1.6-SNAPSHOT</version>
<name>ace-common</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
/*
* Copyright (c) 2007-2010, University of Maryland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions
* and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the
* distribution.
*
* Neither the name of the University of Maryland nor the names of its contributors may be used to
* endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ACE Components were written in the ADAPT Project at the University of
* Maryland Institute for Advanced Computer Study.
*/
// $Id$
package edu.umiacs.ace.compound;
import edu.umiacs.ace.hashtree.HashTree;
import edu.umiacs.ace.hashtree.HashTreeBuilder;
import edu.umiacs.ace.hashtree.Proof;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
*
* @author toaster
*/
public class CompoundFileReader {
private CompoundObject co;
private boolean hasRead = false;
private MessageDigest digest;
private HashTree objectTree;
public CompoundFileReader( CompoundObject co ) {
this.co = co;
try {
digest = MessageDigest.getInstance("SHA-256");
} catch ( NoSuchAlgorithmException ex ) {
throw new RuntimeException(ex);
}
}
public void processObject() {
HashTreeBuilder htb = new HashTreeBuilder(digest);
for ( ObjectEntry oe : co ) {
htb.add(oe.getDigest());
}
hasRead = true;
objectTree = htb.build();
}
public HashTree getHashTree() {
return objectTree;
}
public Proof getProofForItem( int index ) {
if ( !hasRead ) {
throw new IllegalStateException("processObject not yet called");
}
return objectTree.proof(index);
}
}
/*
* Copyright (c) 2007-2010, University of Maryland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions
* and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the
* distribution.
*
* Neither the name of the University of Maryland nor the names of its contributors may be used to
* endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ACE Components were written in the ADAPT Project at the University of
* Maryland Institute for Advanced Computer Study.
*/
// $Id$
package edu.umiacs.ace.compound;
/**
* Interface to access complex objects.
*
* @author toaster
*/
public interface CompoundObject extends Iterable<ObjectEntry> {
}
/*
* Copyright (c) 2007-2010, University of Maryland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions
* and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the
* distribution.
*
* Neither the name of the University of Maryland nor the names of its contributors may be used to
* endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ACE Components were written in the ADAPT Project at the University of
* Maryland Institute for Advanced Computer Study.
*/
// $Id$
package edu.umiacs.ace.compound;
/**
*
* @author toaster
*/
public class ObjectEntry {
private byte[] digest;
private long offset;
private String name;
private int index;
private long size;
void setDigest( byte[] digest ) {
this.digest = digest;
}
void setIndex( int index ) {
this.index = index;
}
void setName( String name ) {
this.name = name;
}
void setOffset( long offset ) {
this.offset = offset;
}
public byte[] getDigest() {
return digest;
}
public int getIndex() {
return index;
}
public String getName() {
return name;
}
public long getOffset() {
return offset;
}
void setSize( long size ) {
this.size = size;
}
public long getSize() {
return size;
}
}
/*
* Copyright (c) 2007-2010, University of Maryland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions
* and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the
* distribution.
*
* Neither the name of the University of Maryland nor the names of its contributors may be used to
* endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ACE Components were written in the ADAPT Project at the University of
* Maryland Institute for Advanced Computer Study.
*/
// $Id$
package edu.umiacs.ace.compound;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.activation.DataSource;
/**
*
* @author toaster
*/
public class ZipObject implements CompoundObject {
private DataSource ds;
public ZipObject( DataSource ds ) {
this.ds = ds;
}
public Iterator<ObjectEntry> iterator() {
return new ZipIterator();
}
class ZipIterator implements Iterator<ObjectEntry> {
byte data[] = new byte[2048];
ZipInputStream zin;
ObjectEntry oe;
int index = 0;
private MessageDigest digest;
public ZipIterator() {
try {
digest = MessageDigest.getInstance("SHA-256");
zin = new ZipInputStream(ds.getInputStream());
readNext();
} catch ( NoSuchAlgorithmException ex ) {
throw new RuntimeException(
"Error creating digest " + ds.getName(), ex);
} catch ( IOException ex ) {
throw new RuntimeException(
"Error opening zip file " + ds.getName(), ex);
}
}
public boolean hasNext() {
return oe != null;
}
public ObjectEntry next() {
ObjectEntry previous = oe;
try {
readNext();
} catch ( IOException ioe ) {
throw new RuntimeException(ioe);
}
return previous;
}
private void readNext() throws IOException {
ZipEntry entry = zin.getNextEntry();
int count;
if ( entry == null ) {
if ( zin != null ) {
zin.close();
zin = null;
}
oe = null;
return;
}
oe = new ObjectEntry();
oe.setOffset(-1);
oe.setSize(entry.getSize());
oe.setName(entry.getName());
oe.setIndex(index);
digest.reset();
while ( (count = zin.read(data)) != -1 ) {
digest.update(data, 0, count);
}
oe.setDigest(digest.digest());
index++;
}
public void remove() {
throw new UnsupportedOperationException("Remove not supported");
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment