Skip to content
Snippets Groups Projects
Commit ebe69754 authored by felmer's avatar felmer
Browse files

SP-3: IHierachicalContentNode extend by method getChecksumCRC32(). Implemented...

SP-3: IHierachicalContentNode extend by method getChecksumCRC32(). Implemented for all implementing classes except PathInfoProviderBasedHierarchicalContent. IOUtilities introduced. No new unit tests.

SVN: 25145
parent 86a3abd1
No related branches found
No related tags found
No related merge requests found
Showing
with 184 additions and 0 deletions
...@@ -40,6 +40,8 @@ public class ByteArrayBasedContentNode implements IHierarchicalContentNode ...@@ -40,6 +40,8 @@ public class ByteArrayBasedContentNode implements IHierarchicalContentNode
private final String nameOrNull; private final String nameOrNull;
private long lastModified; private long lastModified;
private Long checksum;
/** /**
* Creates an instance for the specified byte array. * Creates an instance for the specified byte array.
...@@ -114,6 +116,15 @@ public class ByteArrayBasedContentNode implements IHierarchicalContentNode ...@@ -114,6 +116,15 @@ public class ByteArrayBasedContentNode implements IHierarchicalContentNode
return byteArray.length; return byteArray.length;
} }
public long getChecksumCRC32() throws UnsupportedOperationException
{
if (checksum == null)
{
checksum = IOUtilities.getChecksumCRC32(new ByteArrayInputStream(byteArray));
}
return checksum;
}
public IRandomAccessFile getFileContent() throws UnsupportedOperationException, public IRandomAccessFile getFileContent() throws UnsupportedOperationException,
IOExceptionUnchecked IOExceptionUnchecked
{ {
......
...@@ -20,9 +20,12 @@ import java.io.BufferedInputStream; ...@@ -20,9 +20,12 @@ import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.List; import java.util.List;
import org.apache.commons.io.FileUtils;
import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel; import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
import ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked; import ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked;
import ch.systemsx.cisd.base.io.IRandomAccessFile; import ch.systemsx.cisd.base.io.IRandomAccessFile;
...@@ -121,6 +124,17 @@ public class FileBasedContentNode implements IHierarchicalContentNode ...@@ -121,6 +124,17 @@ public class FileBasedContentNode implements IHierarchicalContentNode
return file.length(); return file.length();
} }
public long getChecksumCRC32() throws UnsupportedOperationException
{
try
{
return FileUtils.checksumCRC32(file);
} catch (IOException ex)
{
throw CheckedExceptionTunnel.wrapIfNecessary(ex);
}
}
public IRandomAccessFile getFileContent() throws UnsupportedOperationException, public IRandomAccessFile getFileContent() throws UnsupportedOperationException,
IOExceptionUnchecked IOExceptionUnchecked
{ {
......
...@@ -72,6 +72,11 @@ public class HierarchicalContentNodeBasedHierarchicalContentNode implements ...@@ -72,6 +72,11 @@ public class HierarchicalContentNodeBasedHierarchicalContentNode implements
return 0; return 0;
} }
public long getChecksumCRC32() throws UnsupportedOperationException
{
return 0;
}
public IRandomAccessFile getFileContent() throws UnsupportedOperationException, public IRandomAccessFile getFileContent() throws UnsupportedOperationException,
IOExceptionUnchecked IOExceptionUnchecked
{ {
...@@ -172,6 +177,11 @@ public class HierarchicalContentNodeBasedHierarchicalContentNode implements ...@@ -172,6 +177,11 @@ public class HierarchicalContentNodeBasedHierarchicalContentNode implements
return getContent().getFileLength(); return getContent().getFileLength();
} }
public long getChecksumCRC32() throws UnsupportedOperationException
{
return getContent().getChecksumCRC32();
}
public IRandomAccessFile getFileContent() throws UnsupportedOperationException, public IRandomAccessFile getFileContent() throws UnsupportedOperationException,
IOExceptionUnchecked IOExceptionUnchecked
{ {
......
/*
* Copyright 2012 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.common.io;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.Checksum;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.NullOutputStream;
import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
/**
* Useful utility methods.
*
* @author Franz-Josef Elmer
*/
public class IOUtilities
{
/**
* Calculates the CRC32 checksum of specified input stream.
* Note, that the input stream is closed after invocation of this method.
*/
public static long getChecksumCRC32(InputStream inputStream)
{
Checksum checksummer = new CRC32();
InputStream in = null;
try {
in = new CheckedInputStream(inputStream, checksummer);
IOUtils.copy(in, new NullOutputStream());
} catch (IOException ex)
{
throw CheckedExceptionTunnel.wrapIfNecessary(ex);
} finally {
IOUtils.closeQuietly(in);
}
return checksummer.getValue();
}
}
...@@ -47,6 +47,9 @@ public abstract class AbstractHierarchicalContentNode implements IHierarchicalCo ...@@ -47,6 +47,9 @@ public abstract class AbstractHierarchicalContentNode implements IHierarchicalCo
/** Returns size of a node known NOT to be a directory. */ /** Returns size of a node known NOT to be a directory. */
abstract protected long doGetFileLength(); abstract protected long doGetFileLength();
/** Returns checksum of a node known NOT to be a directory. */
abstract protected long doGetChecksumCRC32();
/** Returns {@link IRandomAccessFile} of a node known NOT to be a directory. */ /** Returns {@link IRandomAccessFile} of a node known NOT to be a directory. */
abstract protected IRandomAccessFile doGetFileContent(); abstract protected IRandomAccessFile doGetFileContent();
...@@ -92,6 +95,12 @@ public abstract class AbstractHierarchicalContentNode implements IHierarchicalCo ...@@ -92,6 +95,12 @@ public abstract class AbstractHierarchicalContentNode implements IHierarchicalCo
return doGetFileLength(); return doGetFileLength();
} }
public long getChecksumCRC32() throws UnsupportedOperationException
{
failOnDirectory();
return doGetChecksumCRC32();
}
public final IRandomAccessFile getFileContent() public final IRandomAccessFile getFileContent()
{ {
failOnDirectory(); failOnDirectory();
......
...@@ -34,6 +34,12 @@ public abstract class AbstractHierarchicalDirectoryContentNode extends ...@@ -34,6 +34,12 @@ public abstract class AbstractHierarchicalDirectoryContentNode extends
throw new UnsupportedOperationException(OPERATION_NOT_SUPPORTED_FOR_A_DIRECTORY); throw new UnsupportedOperationException(OPERATION_NOT_SUPPORTED_FOR_A_DIRECTORY);
} }
@Override
protected long doGetChecksumCRC32()
{
throw new UnsupportedOperationException(OPERATION_NOT_SUPPORTED_FOR_A_DIRECTORY);
}
@Override @Override
protected final IRandomAccessFile doGetFileContent() protected final IRandomAccessFile doGetFileContent()
{ {
......
...@@ -19,10 +19,13 @@ package ch.systemsx.cisd.common.io.hierarchical_content; ...@@ -19,10 +19,13 @@ package ch.systemsx.cisd.common.io.hierarchical_content;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.io.FileUtils;
import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel; import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
import ch.systemsx.cisd.base.io.IRandomAccessFile; import ch.systemsx.cisd.base.io.IRandomAccessFile;
import ch.systemsx.cisd.base.io.RandomAccessFileImpl; import ch.systemsx.cisd.base.io.RandomAccessFileImpl;
...@@ -119,6 +122,18 @@ class DefaultFileBasedHierarchicalContentNode extends AbstractHierarchicalConten ...@@ -119,6 +122,18 @@ class DefaultFileBasedHierarchicalContentNode extends AbstractHierarchicalConten
return file.length(); return file.length();
} }
@Override
protected long doGetChecksumCRC32()
{
try
{
return FileUtils.checksumCRC32(file);
} catch (IOException ex)
{
throw CheckedExceptionTunnel.wrapIfNecessary(ex);
}
}
@Override @Override
public IRandomAccessFile doGetFileContent() public IRandomAccessFile doGetFileContent()
{ {
......
...@@ -27,6 +27,7 @@ import ch.systemsx.cisd.base.io.AdapterIInputStreamToInputStream; ...@@ -27,6 +27,7 @@ import ch.systemsx.cisd.base.io.AdapterIInputStreamToInputStream;
import ch.systemsx.cisd.base.io.IRandomAccessFile; import ch.systemsx.cisd.base.io.IRandomAccessFile;
import ch.systemsx.cisd.common.hdf5.HDF5Container; import ch.systemsx.cisd.common.hdf5.HDF5Container;
import ch.systemsx.cisd.common.hdf5.IHDF5ContainerReader; import ch.systemsx.cisd.common.hdf5.IHDF5ContainerReader;
import ch.systemsx.cisd.common.io.IOUtilities;
import ch.systemsx.cisd.common.io.hierarchical_content.api.IHierarchicalContent; import ch.systemsx.cisd.common.io.hierarchical_content.api.IHierarchicalContent;
import ch.systemsx.cisd.common.io.hierarchical_content.api.IHierarchicalContentNode; import ch.systemsx.cisd.common.io.hierarchical_content.api.IHierarchicalContentNode;
import ch.systemsx.cisd.hdf5.h5ar.ArchiveEntry; import ch.systemsx.cisd.hdf5.h5ar.ArchiveEntry;
...@@ -259,6 +260,8 @@ public class HDF5ContainerBasedHierarchicalContentNode extends ...@@ -259,6 +260,8 @@ public class HDF5ContainerBasedHierarchicalContentNode extends
private HDF5DataSetBasedContent contentOrNull; private HDF5DataSetBasedContent contentOrNull;
private final HDF5ContainerBasedHierarchicalContentNode containerNode; private final HDF5ContainerBasedHierarchicalContentNode containerNode;
private Long checksum;
public HDF5FileNode(HDF5ContainerBasedHierarchicalContentNode containerNode, public HDF5FileNode(HDF5ContainerBasedHierarchicalContentNode containerNode,
ArchiveEntry entry) ArchiveEntry entry)
...@@ -310,6 +313,23 @@ public class HDF5ContainerBasedHierarchicalContentNode extends ...@@ -310,6 +313,23 @@ public class HDF5ContainerBasedHierarchicalContentNode extends
return entry.getSize(); return entry.getSize();
} }
@Override
protected long doGetChecksumCRC32()
{
if (checksum == null)
{
int entryChecksum = entry.getCrc32();
if (entryChecksum != 0)
{
checksum = (long) entryChecksum;
} else
{
checksum = IOUtilities.getChecksumCRC32(doGetInputStream());
}
}
return checksum;
}
@Override @Override
protected IRandomAccessFile doGetFileContent() protected IRandomAccessFile doGetFileContent()
{ {
......
...@@ -437,6 +437,11 @@ class VirtualHierarchicalContent implements IHierarchicalContent ...@@ -437,6 +437,11 @@ class VirtualHierarchicalContent implements IHierarchicalContent
} }
} }
public long getChecksumCRC32() throws UnsupportedOperationException
{
return lastExistingNode().getChecksumCRC32();
}
public IRandomAccessFile getFileContent() throws UnsupportedOperationException, public IRandomAccessFile getFileContent() throws UnsupportedOperationException,
IOExceptionUnchecked IOExceptionUnchecked
{ {
......
...@@ -91,6 +91,15 @@ public interface IHierarchicalContentNode ...@@ -91,6 +91,15 @@ public interface IHierarchicalContentNode
* @throws UnsupportedOperationException if the node is an abstraction of a directory. * @throws UnsupportedOperationException if the node is an abstraction of a directory.
*/ */
long getFileLength() throws UnsupportedOperationException; long getFileLength() throws UnsupportedOperationException;
/**
* Returns the CRC32 checksum of a file abstracted by this node.
* <p>
* NOTE: Call {@link #isDirectory()} first to make sure this node is NOT a directory.
*
* @throws UnsupportedOperationException if the node is an abstraction of a directory.
*/
long getChecksumCRC32() throws UnsupportedOperationException;
/** /**
* Returns a read only {@link IRandomAccessFile} with file content of the node. * * Returns a read only {@link IRandomAccessFile} with file content of the node. *
......
...@@ -171,6 +171,12 @@ public class AbstractHierarchicalContentNodeTest extends AssertJUnit ...@@ -171,6 +171,12 @@ public class AbstractHierarchicalContentNodeTest extends AssertJUnit
return 0; return 0;
} }
@Override
protected long doGetChecksumCRC32()
{
return 0;
}
@Override @Override
protected IRandomAccessFile doGetFileContent() protected IRandomAccessFile doGetFileContent()
{ {
...@@ -238,6 +244,12 @@ public class AbstractHierarchicalContentNodeTest extends AssertJUnit ...@@ -238,6 +244,12 @@ public class AbstractHierarchicalContentNodeTest extends AssertJUnit
return 0; return 0;
} }
@Override
protected long doGetChecksumCRC32()
{
return 0;
}
@Override @Override
protected IRandomAccessFile doGetFileContent() protected IRandomAccessFile doGetFileContent()
{ {
......
...@@ -659,6 +659,11 @@ public class DefaultFileBasedHierarchicalContentTest extends AbstractFileSystemT ...@@ -659,6 +659,11 @@ public class DefaultFileBasedHierarchicalContentTest extends AbstractFileSystemT
throw new UnsupportedOperationException(METHOD_NOT_IMPLEMENTED); throw new UnsupportedOperationException(METHOD_NOT_IMPLEMENTED);
} }
public long getChecksumCRC32() throws UnsupportedOperationException
{
throw new UnsupportedOperationException(METHOD_NOT_IMPLEMENTED);
}
public InputStream getInputStream() throws UnsupportedOperationException, public InputStream getInputStream() throws UnsupportedOperationException,
IOExceptionUnchecked IOExceptionUnchecked
{ {
......
...@@ -237,6 +237,13 @@ class PathInfoProviderBasedHierarchicalContent implements IHierarchicalContent ...@@ -237,6 +237,13 @@ class PathInfoProviderBasedHierarchicalContent implements IHierarchicalContent
return pathInfo.getSizeInBytes(); return pathInfo.getSizeInBytes();
} }
@Override
protected long doGetChecksumCRC32()
{
// TODO Auto-generated method stub
return 0;
}
public File getFile() throws UnsupportedOperationException public File getFile() throws UnsupportedOperationException
{ {
File result = doGetFile(); File result = doGetFile();
......
...@@ -94,6 +94,11 @@ public class ImageUtilTest extends AssertJUnit ...@@ -94,6 +94,11 @@ public class ImageUtilTest extends AssertJUnit
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
public long getChecksumCRC32() throws UnsupportedOperationException
{
throw new UnsupportedOperationException();
}
public IRandomAccessFile getFileContent() throws UnsupportedOperationException, public IRandomAccessFile getFileContent() throws UnsupportedOperationException,
IOExceptionUnchecked IOExceptionUnchecked
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment