protected ICacheElement processGet()

in commons-jcs3-sandbox/commons-jcs3-filecache/src/main/java/org/apache/commons/jcs/auxiliary/disk/file/FileDiskCache.java [234:304]


    protected ICacheElement<K, V> processGet( K key )
        throws IOException
    {
        File file = file( key );

        if ( !file.exists() )
        {
            if ( log.isDebugEnabled() )
            {
                log.debug( "File does not exist.  Returning null from Get." + file );
            }
            return null;
        }

        ICacheElement<K, V> element = null;

        FileInputStream fis = null;
        try
        {
            fis = new FileInputStream( file );

            long length = file.length();
            // Create the byte array to hold the data
            byte[] bytes = new byte[(int) length];

            int offset = 0;
            int numRead = 0;
            while ( offset < bytes.length && ( numRead = fis.read( bytes, offset, bytes.length - offset ) ) >= 0 )
            {
                offset += numRead;
            }

            // Ensure all the bytes have been read in
            if ( offset < bytes.length )
            {
                throw new IOException( "Could not completely read file " + file.getName() );
            }

            element = getElementSerializer().deSerialize( bytes, null );

            // test that the retrieved object has equal key
            if ( element != null && !key.equals( element.getKey() ) )
            {
                if ( log.isInfoEnabled() )
                {
                    log.info( logCacheName + "key: [" + key + "] point to cached object with key: [" + element.getKey()
                        + "]" );
                }
                element = null;
            }
        }
        catch ( IOException e )
        {
            log.error( logCacheName + "Failure getting element, key: [" + key + "]", e );
        }
        catch ( ClassNotFoundException e )
        {
            log.error( logCacheName + "Failure getting element, key: [" + key + "]", e );
        }
        finally
        {
            silentClose( fis );
        }

        // If this is true and we have a max file size, the Least Recently Used file will be removed.
        if ( element != null && diskFileCacheAttributes.isTouchOnGet() )
        {
            touchWithRetry( file );
        }
        return element;
    }