BlobRepositoryImpl implements BlobRepository { @Override public String put(InputStream input) throws IOException { if(input == null) { throw new IllegalArgumentException("input must not be null!"); } prepare(); File tempFile = File.createTempFile("Blob", ".tmp", baseDirectory); if(logger.isDebugEnabled()) logger.debug("Created temporary file '{}'.", tempFile); String hashString = copyAndHash(input, tempFile); long tempLength = tempFile.length(); if(tempLength == 0) { if(tempFile.delete()) { if(logger.isDebugEnabled()) logger.debug("Deleted empty file '{}'.", tempFile.getAbsolutePath()); } else { if(logger.isErrorEnabled()) logger.error("Failed to delete temporary file '{}'!", tempFile.getAbsolutePath()); } throw new IllegalArgumentException("input must not be empty!"); } File destinationFile=prepareFile(hashString); if(destinationFile.isFile()) { long destinationLength = destinationFile.length(); if(destinationLength == tempLength) { if(logger.isInfoEnabled()) logger.info("Blob {} did already exist.", hashString); deleteTempFile(tempFile); return hashString; } else { if(logger.isWarnEnabled()) logger.warn("A different blob with the hash {} does already exist!", hashString); deleteTempFile(tempFile); return null; } } if(tempFile.renameTo(destinationFile)) { if(logger.isDebugEnabled()) logger.debug("Created blob file '{}'", destinationFile.getAbsolutePath()); if(logger.isInfoEnabled()) logger.info("Created blob {} containing {} bytes.", hashString, tempLength); return hashString; } if(logger.isWarnEnabled()) logger.warn("Couldn't rename temp file '{}' to destination file '{}'!", tempFile.getAbsolutePath(), destinationFile.getAbsolutePath()); deleteTempFile(tempFile); return null; }  boolean isCaseSensitive(); void setCaseSensitive(boolean caseSensitive); boolean isValidating(); void setValidating(boolean validating); File getBaseDirectory(); void setBaseDirectory(File baseDirectory); @Override String put(InputStream input); @Override String put(byte[] bytes); @Override InputStream get(String id); @Override boolean delete(String id); @Override boolean contains(String id); @Override long sizeOf(String id); @Override Set<String> idSet(); @Override String toString();  }
@Test public void put() throws IOException, AmbiguousIdException { BlobRepositoryImpl instance=new BlobRepositoryImpl(); instance.setBaseDirectory(folder.newFolder(FOO)); String id=instance.put(TEST_DATA.getBytes(StandardCharsets.UTF_8)); assertEquals(TEST_DATA_ID, id); }
@Test(expected = IllegalStateException.class) public void misconfigurationPut() throws IOException, AmbiguousIdException { BlobRepositoryImpl instance=new BlobRepositoryImpl(); instance.put(TEST_DATA.getBytes(StandardCharsets.UTF_8)); }