Skip to content

Commit

Permalink
NGResourceLoader now obtains an InputStream provider instead of a stream
Browse files Browse the repository at this point in the history
This will allow us to read data multiple times from the retrieved object
  • Loading branch information
hugithordarson committed Jun 20, 2024
1 parent af41b2f commit 0bcddae
Showing 1 changed file with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
Expand Down Expand Up @@ -105,7 +106,26 @@ public default Optional<byte[]> bytesForResourceWithPath( String resourcePath )
}
}

public Optional<InputStream> inputStreamForResourceWithPath( String resourcePath );
public default Optional<InputStream> inputStreamForResourceWithPath( String resourcePath ) {
final Optional<Callable<InputStream>> provider = inputStreamProviderForResourceWithPath( resourcePath );

if( provider.isEmpty() ) {
return Optional.empty();
}

try {
final InputStream inputStream = provider.get().call();
return Optional.of( inputStream );
}
catch( IOException e ) {
throw new UncheckedIOException( e );
}
catch( Exception e ) {
throw new RuntimeException( e );
}
}

public Optional<Callable<InputStream>> inputStreamProviderForResourceWithPath( String resourcePath );
}

/**
Expand All @@ -126,7 +146,7 @@ public JavaClasspathResourceSource( final String basePath ) {
}

@Override
public Optional<InputStream> inputStreamForResourceWithPath( String resourcePath ) {
public Optional<Callable<InputStream>> inputStreamProviderForResourceWithPath( String resourcePath ) {
Objects.requireNonNull( resourcePath );

logger.debug( "Reading resource {} ", resourcePath );
Expand Down Expand Up @@ -164,12 +184,7 @@ public Optional<InputStream> inputStreamForResourceWithPath( String resourcePath
return Optional.empty();
}

try {
return Optional.of( resourceURL.openStream() );
}
catch( final IOException ioException ) {
throw new UncheckedIOException( ioException );
}
return Optional.of( resourceURL::openStream );
}

/**
Expand Down Expand Up @@ -212,6 +227,12 @@ public Optional<InputStream> inputStreamForResourceWithPath( String resourcePath
throw new UncheckedIOException( ioException );
}
}

@Override
public Optional<Callable<InputStream>> inputStreamProviderForResourceWithPath( String resourcePath ) {
// TODO Auto-generated method stub
return Optional.empty();
}
}

public Set<String> namespaces() {
Expand Down

0 comments on commit 0bcddae

Please sign in to comment.