Skip to content
Snippets Groups Projects
ByteArrayBasedContentNode.java 3.54 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
     * Copyright 2010 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.ByteArrayInputStream;
    
    gpawel's avatar
    gpawel committed
    import java.io.File;
    
    import java.nio.ByteBuffer;
    
    gpawel's avatar
    gpawel committed
    import java.util.Date;
    import java.util.List;
    
    gpawel's avatar
    gpawel committed
    import ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked;
    
    brinn's avatar
    brinn committed
    import ch.systemsx.cisd.base.io.ByteBufferRandomAccessFile;
    import ch.systemsx.cisd.base.io.IRandomAccessFile;
    
    gpawel's avatar
    gpawel committed
    import ch.systemsx.cisd.common.io.hierarchical_content.api.IHierarchicalContentNode;
    
    
    /**
     * Content based on an array of bytes.
    
    tpylak's avatar
    tpylak committed
     * 
    
    gpawel's avatar
    gpawel committed
    public class ByteArrayBasedContentNode implements IHierarchicalContentNode
    
    {
        private final byte[] byteArray;
    
    tpylak's avatar
    tpylak committed
    
    
        private final String nameOrNull;
    
    tpylak's avatar
    tpylak committed
    
    
    gpawel's avatar
    gpawel committed
        private long lastModified;
    
        /**
         * Creates an instance for the specified byte array.
    
    tpylak's avatar
    tpylak committed
         * 
    
         * @param nameOrNull Name of the content or null
    
    gpawel's avatar
    gpawel committed
        public ByteArrayBasedContentNode(byte[] byteArray, String nameOrNull)
    
            this.nameOrNull = nameOrNull;
    
    gpawel's avatar
    gpawel committed
            this.lastModified = new Date().getTime();
    
    tpylak's avatar
    tpylak committed
    
    
        /**
         * Returns always <code>true</code>.
         */
    
        public boolean exists()
        {
            return true;
        }
    
        /**
         * Returns an instance of {@link ByteArrayInputStream}.
         */
    
        public InputStream getInputStream()
        {
            return new ByteArrayInputStream(byteArray);
        }
    
    gpawel's avatar
    gpawel committed
        public String getName()
        {
            return nameOrNull;
        }
    
    
    gpawel's avatar
    gpawel committed
        public String getRelativePath()
        {
            throw new UnsupportedOperationException();
        }
    
    
    gpawel's avatar
    gpawel committed
        public String getParentRelativePath()
        {
            throw new UnsupportedOperationException();
        }
    
    
    gpawel's avatar
    gpawel committed
        public boolean isDirectory()
        {
            return false;
        }
    
    
    gpawel's avatar
    gpawel committed
        public long getLastModified()
        {
            return lastModified;
        }
    
    
    gpawel's avatar
    gpawel committed
        public List<IHierarchicalContentNode> getChildNodes() throws UnsupportedOperationException
        {
            throw new UnsupportedOperationException();
        }
    
    
    gpawel's avatar
    gpawel committed
        public File getFile() throws UnsupportedOperationException
        {
            throw new UnsupportedOperationException();
        }
    
    gpawel's avatar
    gpawel committed
        public long getFileLength() throws UnsupportedOperationException
        {
            return byteArray.length;
        }
    
    
        public int getChecksumCRC32() throws UnsupportedOperationException
    
        {
            if (checksum == null)
            {
                checksum = IOUtilities.getChecksumCRC32(new ByteArrayInputStream(byteArray));
            }
            return checksum;
        }
    
    
        @Override
        public boolean isChecksumCRC32Precalculated()
        {
            return false;
        }
    
    
    gpawel's avatar
    gpawel committed
        public IRandomAccessFile getFileContent() throws UnsupportedOperationException,
                IOExceptionUnchecked
    
        {
            return new ByteBufferRandomAccessFile(ByteBuffer.wrap(byteArray));
        }