<commit_before>package org.smoothbuild.object;

import static org.smoothbuild.command.SmoothContants.OBJECTS_DIR;
import static org.smoothbuild.object.HashCodes.toPath;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.smoothbuild.fs.base.FileSystem;
import org.smoothbuild.fs.base.Path;
import org.smoothbuild.fs.base.PathState;
import org.smoothbuild.fs.base.SubFileSystem;
import org.smoothbuild.fs.base.exc.FileSystemError;
import org.smoothbuild.hash.Hash;
import org.smoothbuild.message.listen.ErrorMessageException;
import org.smoothbuild.object.err.NoObjectWithGivenHashError;

import com.google.common.base.Charsets;
import com.google.common.hash.HashCode;

@Singleton
public class HashedDb {
  public static final Charset STRING_CHARSET = Charsets.UTF_8;

  private final FileSystem objectsFileSystem;

  @Inject
  public HashedDb(FileSystem fileSystem) {
    this.objectsFileSystem = new SubFileSystem(fileSystem, OBJECTS_DIR);
  }

  public InputStream openInputStream(HashCode hash) {
    Path path = toPath(hash);
    if (objectsFileSystem.pathState(path) == PathState.FILE) {
      return objectsFileSystem.openInputStream(path);
    } else {
      throw new ErrorMessageException(new NoObjectWithGivenHashError(hash));
    }
  }

  public HashCode store(byte[] bytes) {
    return store(Hash.bytes(bytes), bytes);
  }

  public HashCode store(HashCode hash, byte[] bytes) {
    Path path = toPath(hash);
    if (objectsFileSystem.pathState(path) != PathState.FILE) {
      store(path, bytes);
    }
    return hash;
  }

  private void store(Path path, byte[] bytes) {
    try (OutputStream outputStream = objectsFileSystem.openOutputStream(path)) {
      outputStream.write(bytes);
    } catch (IOException e) {
      throw new ErrorMessageException(new FileSystemError(e));
    }
  }
}<commit_msg>refactored HashedDb to report ReadingHashedObjectFailedError instead of FileSystemError<commit_after>package org.smoothbuild.object;

import static org.smoothbuild.command.SmoothContants.OBJECTS_DIR;
import static org.smoothbuild.object.HashCodes.toPath;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.smoothbuild.fs.base.FileSystem;
import org.smoothbuild.fs.base.Path;
import org.smoothbuild.fs.base.PathState;
import org.smoothbuild.fs.base.SubFileSystem;
import org.smoothbuild.hash.Hash;
import org.smoothbuild.message.listen.ErrorMessageException;
import org.smoothbuild.object.err.NoObjectWithGivenHashError;
import org.smoothbuild.object.err.ReadingHashedObjectFailedError;

import com.google.common.base.Charsets;
import com.google.common.hash.HashCode;

@Singleton
public class HashedDb {
  public static final Charset STRING_CHARSET = Charsets.UTF_8;

  private final FileSystem objectsFileSystem;

  @Inject
  public HashedDb(FileSystem fileSystem) {
    this.objectsFileSystem = new SubFileSystem(fileSystem, OBJECTS_DIR);
  }

  public InputStream openInputStream(HashCode hash) {
    Path path = toPath(hash);
    if (objectsFileSystem.pathState(path) == PathState.FILE) {
      return objectsFileSystem.openInputStream(path);
    } else {
      throw new ErrorMessageException(new NoObjectWithGivenHashError(hash));
    }
  }

  public HashCode store(byte[] bytes) {
    return store(Hash.bytes(bytes), bytes);
  }

  public HashCode store(HashCode hash, byte[] bytes) {
    Path path = toPath(hash);

    if (objectsFileSystem.pathState(path) == PathState.FILE) {
      return hash;
    }

    try (OutputStream outputStream = objectsFileSystem.openOutputStream(path)) {
      outputStream.write(bytes);
    } catch (IOException e) {
      throw new ErrorMessageException(new ReadingHashedObjectFailedError(hash, e));
    }

    return hash;
  }
}
{'fraction_non_alphanumeric': 0.09788092835519677, 'fraction_numerical': 0.0005045408678102926, 'mean_word_length': 4.830882352941177, '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': '6710953', 'n_tokens_mistral': 1222, 'n_tokens_neox': 1185, 'n_words': 242}