@Test @PrepareForTest(OSSOutputStream.class) public void testCloseError() throws Exception { String errorMessage = "Invoke the createEmptyObject method error."; BufferedInputStream inputStream = PowerMockito.mock(BufferedInputStream.class); PowerMockito.whenNew(BufferedInputStream.class) .withArguments(Mockito.any(FileInputStream.class)).thenReturn(inputStream); PowerMockito .when(mOssClient.putObject(Mockito.anyString(), Mockito.anyString(), Mockito.any(InputStream.class), Mockito.any(ObjectMetadata.class))) .thenThrow(new OSSException(errorMessage)); OSSOutputStream stream = new OSSOutputStream("testBucketName", "testKey", mOssClient, sConf .getList(PropertyKey.TMP_DIRS, ",")); mThrown.expect(IOException.class); mThrown.expectMessage(errorMessage); stream.close(); }
@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();  }