<commit_before>package org.ihtsdo.otf.dao.resources;

import com.amazonaws.services.s3.model.AmazonS3Exception;
import org.springframework.cloud.aws.core.io.s3.PathMatchingSimpleStorageResourcePatternResolver;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.WritableResource;
import org.springframework.util.StreamUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Service to read and write file resources from S3 or local disk.
 * Use spring-cloud-aws-autoconfigure dependency autowire an S3 capable ResourceLoader into your configuration.
 */
public class ResourceManager {

	private final ResourceConfiguration resourceConfiguration;
	private final ResourceLoader resourceLoader;

	public ResourceManager(ResourceConfiguration resourceConfiguration, ResourceLoader cloudResourceLoader) {
		this.resourceConfiguration = resourceConfiguration;
		if (resourceConfiguration.isUseCloud()) {
			// Check we can perform a get request to our S3 bucket and path
			String s3Path = getFullPath("does-not-exist.txt");
			cloudResourceLoader.getResource(s3Path).exists();
			resourceLoader = cloudResourceLoader;
		} else {
			resourceLoader = new FileSystemResourceLoader();
		}
	}

	public InputStream readResourceStream(String resourcePath) throws IOException {
		try {
			String fullPath = getFullPath(resourcePath);
			Resource resource = resourceLoader.getResource(fullPath);
			return resource.getInputStream();
		} catch (AmazonS3Exception e) {
			throw new IOException("Failed to load resource '" + resourcePath + "'.", e);
		}
	}

	public void writeResource(String resourcePath, InputStream resourceInputStream) throws IOException {
		try {
			try (OutputStream outputStream = writeResourceStream(resourcePath);
				 InputStream inputStream = resourceInputStream) {
				StreamUtils.copy(inputStream, outputStream);
			}
		} catch (AmazonS3Exception e) {
			throw new IOException("Failed to write resource '" + resourcePath + "'.", e);
		}
	}

	public OutputStream writeResourceStream(String resourcePath) throws IOException {
		writeCheck();<commit_msg>CLASS-96 ResourceManager method for cautious object read.<commit_after>package org.ihtsdo.otf.dao.resources;

import com.amazonaws.services.s3.model.AmazonS3Exception;
import org.springframework.cloud.aws.core.io.s3.PathMatchingSimpleStorageResourcePatternResolver;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.WritableResource;
import org.springframework.util.StreamUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Service to read and write file resources from S3 or local disk.
 * Use spring-cloud-aws-autoconfigure dependency autowire an S3 capable ResourceLoader into your configuration.
 */
public class ResourceManager {

	private final ResourceConfiguration resourceConfiguration;
	private final ResourceLoader resourceLoader;

	public ResourceManager(ResourceConfiguration resourceConfiguration, ResourceLoader cloudResourceLoader) {
		this.resourceConfiguration = resourceConfiguration;
		if (resourceConfiguration.isUseCloud()) {
			// Check we can perform a get request to our S3 bucket and path
			String s3Path = getFullPath("does-not-exist.txt");
			cloudResourceLoader.getResource(s3Path).exists();
			resourceLoader = cloudResourceLoader;
		} else {
			resourceLoader = new FileSystemResourceLoader();
		}
	}

	public InputStream readResourceStream(String resourcePath) throws IOException {
		try {
			String fullPath = getFullPath(resourcePath);
			Resource resource = resourceLoader.getResource(fullPath);
			return resource.getInputStream();
		} catch (AmazonS3Exception e) {
			throw new IOException("Failed to load resource '" + resourcePath + "'.", e);
		}
	}

	public InputStream readResourceStreamOrNullIfNotExists(String resourcePath) throws IOException {
		try {
			String fullPath = getFullPath(resourcePath);
			Resource resource = resourceLoader.getResource(fullPath);
			return resource.exists() ? resource.getInputStream() : null;
		} catch (AmazonS3Exception e) {
			throw new IOException("Failed to load resource '" + resourcePath + "'.", e);
		}
	}

	public void writeResource(String resourcePath, InputStream resourceInputStream) throws IOException {
		try {
			try (OutputStream outputStream = writeResourceStream(resourcePath);
				 InputStream inputStream = resourceInputStream) {
				StreamUtils.copy(inputStream, outputStream);
			}
		} catch (AmazonS3Exception e) {
			throw new IOException("Failed to write resource '" + resourcePath + "'.", e);
		}
	}

	public OutputStream writeResourceStream(String resourcePath) throws IOException {
		writeCheck();
{'fraction_non_alphanumeric': 0.08601497672535924, 'fraction_numerical': 0.004654928152195912, 'mean_word_length': 5.519788918205805, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '936873', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1248, 'n_words': 333}