@Test @PrepareForTest(OSSOutputStream.class) public void testCloseSuccess() throws Exception { PowerMockito.whenNew(File.class).withArguments(Mockito.anyString()).thenReturn(mFile); FileOutputStream outputStream = PowerMockito.mock(FileOutputStream.class); PowerMockito.whenNew(FileOutputStream.class).withArguments(mFile).thenReturn(outputStream); FileInputStream inputStream = PowerMockito.mock(FileInputStream.class); PowerMockito.whenNew(FileInputStream.class).withArguments(mFile).thenReturn(inputStream); OSSOutputStream stream = new OSSOutputStream("testBucketName", "testKey", mOssClient, sConf .getList(PropertyKey.TMP_DIRS, ",")); stream.close(); Mockito.verify(mFile).delete(); }
@Override public void close() throws IOException { if (mClosed.getAndSet(true)) { return; } mLocalOutputStream.close(); try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(mFile))) { ObjectMetadata objMeta = new ObjectMetadata(); objMeta.setContentLength(mFile.length()); if (mHash != null) { byte[] hashBytes = mHash.digest(); objMeta.setContentMD5(new String(Base64.encodeBase64(hashBytes))); } mOssClient.putObject(mBucketName, mKey, in, objMeta); } catch (ServiceException e) { LOG.error("Failed to upload {}.", mKey); throw new IOException(e); } finally { if (!mFile.delete()) { LOG.error("Failed to delete temporary file @ {}", mFile.getPath()); } } }
OSSOutputStream extends OutputStream { @Override public void close() throws IOException { if (mClosed.getAndSet(true)) { return; } mLocalOutputStream.close(); try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(mFile))) { ObjectMetadata objMeta = new ObjectMetadata(); objMeta.setContentLength(mFile.length()); if (mHash != null) { byte[] hashBytes = mHash.digest(); objMeta.setContentMD5(new String(Base64.encodeBase64(hashBytes))); } mOssClient.putObject(mBucketName, mKey, in, objMeta); } catch (ServiceException e) { LOG.error("Failed to upload {}.", mKey); throw new IOException(e); } finally { if (!mFile.delete()) { LOG.error("Failed to delete temporary file @ {}", mFile.getPath()); } } } }
OSSOutputStream extends OutputStream { @Override public void close() throws IOException { if (mClosed.getAndSet(true)) { return; } mLocalOutputStream.close(); try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(mFile))) { ObjectMetadata objMeta = new ObjectMetadata(); objMeta.setContentLength(mFile.length()); if (mHash != null) { byte[] hashBytes = mHash.digest(); objMeta.setContentMD5(new String(Base64.encodeBase64(hashBytes))); } mOssClient.putObject(mBucketName, mKey, in, objMeta); } catch (ServiceException e) { LOG.error("Failed to upload {}.", mKey); throw new IOException(e); } finally { if (!mFile.delete()) { LOG.error("Failed to delete temporary file @ {}", mFile.getPath()); } } } OSSOutputStream(String bucketName, String key, OSS client, List<String> tmpDirs); }
OSSOutputStream extends OutputStream { @Override public void close() throws IOException { if (mClosed.getAndSet(true)) { return; } mLocalOutputStream.close(); try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(mFile))) { ObjectMetadata objMeta = new ObjectMetadata(); objMeta.setContentLength(mFile.length()); if (mHash != null) { byte[] hashBytes = mHash.digest(); objMeta.setContentMD5(new String(Base64.encodeBase64(hashBytes))); } mOssClient.putObject(mBucketName, mKey, in, objMeta); } catch (ServiceException e) { LOG.error("Failed to upload {}.", mKey); throw new IOException(e); } finally { if (!mFile.delete()) { LOG.error("Failed to delete temporary file @ {}", mFile.getPath()); } } } OSSOutputStream(String bucketName, String key, OSS client, List<String> tmpDirs); @Override void write(int b); @Override void write(byte[] b); @Override void write(byte[] b, int off, int len); @Override void flush(); @Override void close(); }
OSSOutputStream extends OutputStream { @Override public void close() throws IOException { if (mClosed.getAndSet(true)) { return; } mLocalOutputStream.close(); try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(mFile))) { ObjectMetadata objMeta = new ObjectMetadata(); objMeta.setContentLength(mFile.length()); if (mHash != null) { byte[] hashBytes = mHash.digest(); objMeta.setContentMD5(new String(Base64.encodeBase64(hashBytes))); } mOssClient.putObject(mBucketName, mKey, in, objMeta); } catch (ServiceException e) { LOG.error("Failed to upload {}.", mKey); throw new IOException(e); } finally { if (!mFile.delete()) { LOG.error("Failed to delete temporary file @ {}", mFile.getPath()); } } } OSSOutputStream(String bucketName, String key, OSS client, List<String> tmpDirs); @Override void write(int b); @Override void write(byte[] b); @Override void write(byte[] b, int off, int len); @Override void flush(); @Override void close();  }