@VisibleForTesting
    static Path resolveEntropy(Path path, EntropyInjectingFileSystem efs, boolean injectEntropy)
            throws IOException {
        final String entropyInjectionKey = efs.getEntropyInjectionKey();

        if (entropyInjectionKey == null) {
            return path;
        } else {
            final URI originalUri = path.toUri();
            final String checkpointPath = originalUri.getPath();

            final int indexOfKey = checkpointPath.indexOf(entropyInjectionKey);
            if (indexOfKey == -1) {
                return path;
            } else {
                final StringBuilder buffer = new StringBuilder(checkpointPath.length());
                buffer.append(checkpointPath, 0, indexOfKey);

                if (injectEntropy) {
                    buffer.append(efs.generateEntropy());
                }

                buffer.append(
                        checkpointPath,
                        indexOfKey + entropyInjectionKey.length(),
                        checkpointPath.length());

                final String rewrittenPath = buffer.toString();
                try {
                    return new Path(
                            new URI(
                                            originalUri.getScheme(),
                                            originalUri.getAuthority(),
                                            rewrittenPath,
                                            originalUri.getQuery(),
                                            originalUri.getFragment())
                                    .normalize());
                } catch (URISyntaxException e) {
                    // this could only happen if the injected entropy string contains invalid
                    // characters
                    throw new IOException(
                            "URI format error while processing path for entropy injection", e);
                }
            }
        }
    }
@Test
    void testEntropyNotFullSegment() throws Exception {
        EntropyInjectingFileSystem efs = new TestEntropyInjectingFs("_entropy_key_", "pqr");
        Path path = new Path("s3://myhost:122/entropy-_entropy_key_-suffix/file");

        assertThat(EntropyInjector.resolveEntropy(path, efs, true))
                .isEqualTo(new Path("s3://myhost:122/entropy-pqr-suffix/file"));
        assertThat(EntropyInjector.resolveEntropy(path, efs, false))
                .isEqualTo(new Path("s3://myhost:122/entropy--suffix/file"));
    }