protected void loadKeys()

in commons-jcs3-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/block/BlockDiskKeyStore.java [388:471]


    protected void loadKeys()
    {
        log.info("{0}: Loading keys for {1}", () -> logCacheName, keyFile::toString);

        // create a key map to use.
        initKeyMap();

        final HashMap<K, int[]> keys = new HashMap<>();

        synchronized (keyFile)
        {
            // Check file type
            int fileSignature = 0;

            try (FileChannel bc = FileChannel.open(keyFile.toPath(), StandardOpenOption.READ))
            {
                final ByteBuffer signature = ByteBuffer.allocate(4);
                bc.read(signature);
                signature.flip();
                fileSignature = signature.getInt();

                if (fileSignature == KEY_FILE_SIGNATURE)
                {
                    while (true)
                    {
                        try
                        {
                            final BlockDiskElementDescriptor<K> descriptor =
                                    serializer.deSerializeFrom(bc, null);
                            if (descriptor != null)
                            {
                                keys.put(descriptor.getKey(), descriptor.getBlocks());
                            }
                        }
                        catch (EOFException e)
                        {
                            break;
                        }
                    }
                }
            }
            catch (final IOException | ClassNotFoundException e)
            {
                log.error("{0}: Problem loading keys for file {1}", logCacheName, fileName, e);
            }

            if (fileSignature != KEY_FILE_SIGNATURE)
            {
                try (InputStream fis = Files.newInputStream(keyFile.toPath());
                     ObjectInputStream ois = new ObjectInputStreamClassLoaderAware(fis, null))
                {
                    while (true)
                    {
                        @SuppressWarnings("unchecked")
                        final
                        // Need to cast from Object
                        BlockDiskElementDescriptor<K> descriptor = (BlockDiskElementDescriptor<K>) ois.readObject();
                        if (descriptor != null)
                        {
                            keys.put(descriptor.getKey(), descriptor.getBlocks());
                        }
                    }
                }
                catch (final EOFException eof)
                {
                    // nothing
                }
                catch (final IOException | ClassNotFoundException e)
                {
                    log.error("{0}: Problem loading keys (old style) for file {1}", logCacheName, fileName, e);
                }
            }
        }

        if (!keys.isEmpty())
        {
            keyHash.putAll(keys);

            log.debug("{0}: Found {1} in keys file.", () -> logCacheName, keys::size);
            log.info("{0}: Loaded keys from [{1}], key count: {2}; up to {3} will be available.",
                    () -> logCacheName, () -> fileName, this::size,
                    () -> maxKeySize);
        }
    }