下面列出了怎么用org.apache.commons.io.output.NullOutputStream的API类实例代码及写法,或者点击链接到github查看源代码。
public static void decompress(final String sourceFile, final String outputDir, final Checksum checksum)
throws IOException {
try (final FileInputStream fis = new FileInputStream(sourceFile);
final CheckedInputStream cis = new CheckedInputStream(fis, checksum);
final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(cis))) {
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
final String fileName = entry.getName();
final File entryFile = new File(Paths.get(outputDir, fileName).toString());
FileUtils.forceMkdir(entryFile.getParentFile());
try (final FileOutputStream fos = new FileOutputStream(entryFile);
final BufferedOutputStream bos = new BufferedOutputStream(fos)) {
IOUtils.copy(zis, bos);
bos.flush();
fos.getFD().sync();
}
}
// Continue to read all remaining bytes(extra metadata of ZipEntry) directly from the checked stream,
// Otherwise, the checksum value maybe unexpected.
//
// See https://coderanch.com/t/279175/java/ZipInputStream
IOUtils.copy(cis, NullOutputStream.NULL_OUTPUT_STREAM);
}
}
@Override
public void fileTransferRequest(FileTransferRequest request) {
final IncomingFileTransfer transfer = request.accept();
Thread transferThread = new Thread(new Runnable() {
public void run() {
try {
OutputStream os = new NullOutputStream();
InputStream is = transfer.recieveFile();
log.debug("Reading from stream: " + is.available());
IOUtils.copy(is, os);
log.debug("Left in stream: " + is.available());
} catch (Exception e) {
log.error("Failed incoming file transfer", e);
}
}
});
transferThread.start();
}
@Test
public void testDownloadGzip() throws Exception {
final TransferStatus status = new TransferStatus();
status.setLength(182L);
final Path container = new Path(".ACCESS_LOGS", EnumSet.of(Path.Type.directory, Path.Type.volume));
container.attributes().setRegion("DFW");
final SwiftRegionService regionService = new SwiftRegionService(session);
final InputStream in = new SwiftReadFeature(session, regionService).read(new Path(container,
"/cdn.cyberduck.ch/2015/03/01/10/3b1d6998c430d58dace0c16e58aaf925.log.gz",
EnumSet.of(Path.Type.file)), status, new DisabledConnectionCallback());
assertNotNull(in);
new StreamCopier(status, status).transfer(in, new NullOutputStream());
assertEquals(182L, status.getOffset());
assertEquals(182L, status.getLength());
in.close();
}
@Test
public void testDownloadGzip() throws Exception {
final int length = 1457;
final byte[] content = RandomUtils.nextBytes(length);
final Path container = new Path("test-us-east-1-cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
final Path file = new Path(container, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
final TransferStatus status = new TransferStatus().length(content.length);
status.setChecksum(new SHA256ChecksumCompute().compute(new ByteArrayInputStream(content), status));
final OutputStream out = new S3WriteFeature(session).write(file, status, new DisabledConnectionCallback());
new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out);
out.close();
final InputStream in = new S3ReadFeature(session).read(file, status, new DisabledConnectionCallback());
assertNotNull(in);
new StreamCopier(status, status).transfer(in, new NullOutputStream());
assertEquals(content.length, status.getOffset());
assertEquals(content.length, status.getLength());
in.close();
new S3DefaultDeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
@Test
public void testDownloadGzip() throws Exception {
final int length = 1457;
final byte[] content = RandomUtils.nextBytes(length);
final Path container = new Path("test.cyberduck.ch", EnumSet.of(Path.Type.directory, Path.Type.volume));
final Path file = new Path(container, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
final TransferStatus status = new TransferStatus().length(content.length);
status.setChecksum(new SHA256ChecksumCompute().compute(new ByteArrayInputStream(content), status));
final OutputStream out = new GoogleStorageWriteFeature(session).write(file, status, new DisabledConnectionCallback());
new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out);
out.close();
final InputStream in = new GoogleStorageReadFeature(session).read(file, status, new DisabledConnectionCallback());
assertNotNull(in);
new StreamCopier(status, status).transfer(in, new NullOutputStream());
assertEquals(content.length, status.getOffset());
assertEquals(content.length, status.getLength());
in.close();
new GoogleStorageDeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
/**
* Custom serialization method.
*
* @param oos the object output stream
*/
private void writeObject(ObjectOutputStream oos) throws IOException {
// figure out size of the written network
CountingOutputStream cos = new CountingOutputStream(new NullOutputStream());
if (isInitializationFinished) {
ModelSerializer.writeModel(model, cos, false);
}
modelSize = cos.getByteCount();
// default serialization
oos.defaultWriteObject();
// Write layer configurations
String[] layerConfigs = new String[layers.length];
for (int i = 0; i < layers.length; i++) {
layerConfigs[i] =
layers[i].getClass().getName() + "::"
+ weka.core.Utils.joinOptions(layers[i].getOptions());
}
oos.writeObject(layerConfigs);
// actually write the network
if (isInitializationFinished) {
ModelSerializer.writeModel(model, oos, false);
}
}
/**
* Custom serialization method.
*
* @param oos the object output stream
*/
private void writeObject(ObjectOutputStream oos) throws IOException {
// figure out size of the written network
CountingOutputStream cos = new CountingOutputStream(new NullOutputStream());
if (isInitializationFinished) {
ModelSerializer.writeModel(model, cos, false);
}
modelSize = cos.getByteCount();
// default serialization
oos.defaultWriteObject();
// Write layer configurations
String[] layerConfigs = new String[layers.length];
for (int i = 0; i < layers.length; i++) {
layerConfigs[i] =
layers[i].getClass().getName() + "::"
+ weka.core.Utils.joinOptions(layers[i].getOptions());
}
oos.writeObject(layerConfigs);
// actually write the network
if (isInitializationFinished) {
ModelSerializer.writeModel(model, oos, false);
}
}
@Test
public void workflowBundle() throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
// To test that seeAlso URIs are stored
serializer.workflowDoc(new NullOutputStream(), workflowBundle.getMainWorkflow(), URI.create(HELLOWORLD_RDF));
serializer.profileDoc(new NullOutputStream(), workflowBundle.getProfiles().getByName("tavernaWorkbench"), URI.create(TAVERNAWORKBENCH_RDF));
serializer.profileDoc(new NullOutputStream(), workflowBundle.getProfiles().getByName("tavernaServer"), URI.create(TAVERNASERVER_RDF));
serializer.workflowBundleDoc(outStream, URI.create("workflowBundle.rdf"));
//System.out.write(outStream.toByteArray());
Document doc = parseXml(outStream);
Element root = doc.getRootElement();
checkRoot(root);
checkWorkflowBundleDocument(root);
}
/**
* Before.
*/
@Before
public void before() {
String value = System.getProperty(KRIPTON_DEBUG_MODE);
String logEnabledvalue = System.getProperty(KRIPTON_GENERATE_LOG_MODE);
// value = "true";
if ("false".equals(value)) {
// we are in test, but we don't see log on System.out
System.setOut(new PrintStream(new NullOutputStream()));
System.setErr(new PrintStream(new NullOutputStream()));
} else {
BaseProcessor.DEBUG_MODE = true;
}
if ("false".equals(logEnabledvalue)) {
BaseProcessor.LOG_GENERATION_ENABLED_MODE = false;
} else {
BaseProcessor.LOG_GENERATION_ENABLED_MODE = true;
}
// when we run junit test, AnnotationProcessor is always in TEST_MODE
BaseProcessor.JUNIT_TEST_MODE = true;
System.setProperty("java.util.logging.SimpleFormatter.format", "%1$tH:%1$tM:%1$tS.%1$tL %4$-7s [%3$s] (%2$s) %5$s %6$s%n");
}
/**
* Setup.
*/
@Before
public void setup() {
//final String value = System.getProperty(KRIPTON_DEBUG_MODE);
final String value = System.getProperty(KRIPTON_DEBUG_MODE);
if ("false".equals(value)) {
ShadowLog.stream = new PrintStream(new NullOutputStream());
// we are in test, but we don't see log on System.out
System.setOut(new PrintStream(new NullOutputStream()));
System.setErr(new PrintStream(new NullOutputStream()));
} else {
ShadowLog.stream = System.out;
}
KriptonLibrary.init(RuntimeEnvironment.application);
}
/**
* Setup.
*/
@Before
public void setup() {
final String value = System.getProperty(KRIPTON_DEBUG_MODE);
if ("false".equals(value)) {
// we are in test, but we don't see log on System.out
System.setOut(new PrintStream(new NullOutputStream()));
System.setErr(new PrintStream(new NullOutputStream()));
}
// when we run junit test, AnnotationProcessor is always in TEST_MODE
System.setProperty("java.util.logging.SimpleFormatter.format", "%1$tH:%1$tM:%1$tS.%1$tL %4$-7s [%3$s] (%2$s) %5$s %6$s%n");
KriptonBinder.registryBinder(new KriptonYamlContext());
KriptonBinder.registryBinder(new KriptonPropertiesContext());
KriptonBinder.registryBinder(new KriptonCborContext());
KriptonBinder.registryBinder(new KriptonSmileContext());
}
@Test
public void prettyPrint() throws Exception {
// given
final String version = versionService.version().getVersionId();
final List<Action> actions = getSimpleAction("uppercase", "column_name", "lastname");
final PreparationActions newContent = new PreparationActions(actions, version);
final Step step = new Step(Step.ROOT_STEP.id(), newContent.id(), version);
final Preparation preparation = new Preparation("#15325878", "1234", step.id(), version);
repository.add(newContent);
repository.add(step);
repository.add(preparation);
// when
PreparationUtils.prettyPrint(repository, preparation, new NullOutputStream());
// Basic walk through code, no assert.
}
@Before
public void setUp() throws Exception {
releasableOutputStream = new ReleasableOutputStream(new NullOutputStream(), () -> wasCalled.set(true));
failedReleasableOutputStream = new ReleasableOutputStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
throw new IOException("Oops");
}
@Override
public void flush() throws IOException {
throw new IOException("Oops");
}
}, () -> wasCalled.set(true));
}
@RequestMapping(value = "/apply", method = POST)
@ApiOperation(value = "Run the transformation given the provided export parameters",
notes = "This operation transforms the dataset or preparation using parameters in export parameters.")
@VolumeMetered
@AsyncOperation(conditionalClass = GetPrepContentAsyncCondition.class, //
resultUrlGenerator = PreparationGetContentUrlGenerator.class, //
executionIdGeneratorClass = ExportParametersExecutionIdGenerator.class //
)
public StreamingResponseBody
execute(@ApiParam(
value = "Preparation id to apply.") @RequestBody @Valid @AsyncParameter @AsyncExecutionId final ExportParameters parameters)
throws IOException {
// Async behavior
final ConditionalTest conditionalTest = applicationContext.getBean(GetPrepContentAsyncCondition.class);
if (conditionalTest.apply(parameters)) {
// write to cache
executeSampleExportStrategy(parameters).writeTo(new NullOutputStream());
return outputStream -> {
};
} else {
// sync behavior
return executeSampleExportStrategy(parameters);
}
}
/**
* Add the following preparation in cache.
*
* @param preparation the preparation to cache.
* @param stepId the preparation step id.
*/
private void addPreparationInCache(PreparationDTO preparation, String stepId) {
final ExportParameters exportParameters = new ExportParameters();
exportParameters.setPreparationId(preparation.getId());
exportParameters.setExportType("JSON");
exportParameters.setStepId(stepId);
exportParameters.setDatasetId(preparation.getDataSetId());
final StreamingResponseBody streamingResponseBody = executeSampleExportStrategy(exportParameters);
try {
// the result is not important here as it will be cached !
streamingResponseBody.writeTo(new NullOutputStream());
} catch (IOException e) {
throw new TDPException(UNEXPECTED_EXCEPTION, e);
}
}
@Test
public void shouldUsedVersionedPreparation() throws IOException {
// Given
final ExportParameters parameters = new ExportParameters();
parameters.setExportType("JSON");
parameters.setPreparationId("prep-1234");
parameters.setStepId("step-1234");
final PreparationDTO preparation = new PreparationDTO();
preparation.setId("prep-1234");
preparation.setHeadId("step-1234");
configurePreparation(preparation, "prep-1234", "step-1234");
// When
final StreamingResponseBody body = strategy.execute(parameters);
body.writeTo(new NullOutputStream());
// Then
final ArgumentCaptor<Configuration> captor = ArgumentCaptor.forClass(Configuration.class);
verify(transformer).buildExecutable(any(), captor.capture());
assertEquals("prep-1234", captor.getValue().getPreparationId());
assertEquals("step-1234", captor.getValue().getPreparation().getHeadId());
}
@Test
public void shouldUsedHeadPreparation() throws IOException {
// Given
final ExportParameters parameters = new ExportParameters();
parameters.setExportType("JSON");
parameters.setPreparationId("prep-1234");
parameters.setStepId("head");
final PreparationDTO preparation = new PreparationDTO();
preparation.getSteps().add(Step.ROOT_STEP.id());
preparation.setId("prep-1234");
preparation.setHeadId("head");
configurePreparation(preparation, "prep-1234", "head");
// When
final StreamingResponseBody body = strategy.execute(parameters);
body.writeTo(new NullOutputStream());
// Then
final ArgumentCaptor<Configuration> captor = ArgumentCaptor.forClass(Configuration.class);
verify(transformer).buildExecutable(any(), captor.capture());
assertEquals("prep-1234", captor.getValue().getPreparationId());
assertEquals("head", captor.getValue().getPreparation().getHeadId());
}
public void loopAllEntitiesAndProperties() throws IOException {
NullOutputStream nullOutputStream = new NullOutputStream();
List<Person> listOfPersons = Service.getListOfPersons(100 * 1000);
long currentTimeMillis = System.currentTimeMillis();
ListContainer<Person> listContainer = new ListContainer<>(
listOfPersons);
Collection<?> ids = listContainer.getContainerPropertyIds();
for (int i = 0; i < listContainer.size(); i++) {
Item item = listContainer.getItem(listOfPersons.get(i));
for (Object propertyId : ids) {
Property itemProperty = item.getItemProperty(propertyId);
final Object value = itemProperty.getValue();
nullOutputStream.write(value.toString().getBytes());
LOG.log(Level.FINEST, "Property: %s", value);
}
}
LOG.
log(Level.INFO,
"Looping all properties in 100 000 Items took {0}ms",
(System.currentTimeMillis() - currentTimeMillis));
}
public void loopAllEntitiesAndPropertiesWithBeanItemContainer() throws IOException {
NullOutputStream nullOutputStream = new NullOutputStream();
List<Person> listOfPersons = Service.getListOfPersons(100 * 1000);
long currentTimeMillis = System.currentTimeMillis();
BeanItemContainer<Person> c = new BeanItemContainer<>(
Person.class, listOfPersons);
Collection<?> ids = c.getContainerPropertyIds();
for (int i = 0; i < c.size(); i++) {
Item item = c.getItem(listOfPersons.get(i));
for (Object propertyId : ids) {
Property itemProperty = item.getItemProperty(propertyId);
final Object value = itemProperty.getValue();
nullOutputStream.write(value.toString().getBytes());
LOG.log(Level.FINEST, "Property: %s", value);
}
}
// ~ 350ms in 1.34, MacBook Pro (Retina, Mid 2012) 2.3Gz i7
// ~ + 3-10ms in 1.35, when changing ListContainer to use PropertyUtils instead of WrapDynaBean
LOG.
log(Level.INFO,
"BIC from core: Looping all properties in 100 000 Items took {0}ms",
(System.currentTimeMillis() - currentTimeMillis));
}
/**
* Test that we don't have a NullpointerException when forcing the caching (ttl).
*
* @throws Exception
*/
public void testForcedTtlWith304ResponseCode() throws Exception {
properties = new PropertiesBuilder() //
.set(Parameters.REMOTE_URL_BASE, "http://localhost:8080") //
.set(Parameters.TTL, 1000) // Default value
.build();
createHttpClientRequestExecutor();
DriverRequest originalRequest = TestUtils.createDriverRequest(driver);
originalRequest.getOriginalRequest().addHeader("If-Modified-Since", "Fri, 15 Jun 2012 21:06:25 GMT");
OutgoingRequest request =
httpClientRequestExecutor.createOutgoingRequest(originalRequest, "http://localhost:8080", false);
HttpResponse response =
new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1),
HttpStatus.SC_NOT_MODIFIED, "Not Modified"));
mockConnectionManager.setResponse(response);
HttpResponse result = httpClientRequestExecutor.execute(request);
if (result.getEntity() != null) {
result.getEntity().writeTo(new NullOutputStream());
// We should have had a NullpointerException
}
}
/**
* Test that we don't have a NullpointerException when forcing the caching (ttl).
*
* @throws Exception
*/
public void testForcedTtlWith301ResponseCode() throws Exception {
properties = new PropertiesBuilder() //
.set(Parameters.REMOTE_URL_BASE, "http://localhost:8080") //
.set(Parameters.TTL, 1000) // Default value
.build();
createHttpClientRequestExecutor();
DriverRequest originalRequest = TestUtils.createDriverRequest(driver);
OutgoingRequest request =
httpClientRequestExecutor.createOutgoingRequest(originalRequest, "http://localhost:8080", true);
HttpResponse response =
new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1),
HttpStatus.SC_MOVED_PERMANENTLY, "Moved permanently"));
response.addHeader("Location", "http://www.foo.com");
mockConnectionManager.setResponse(response);
HttpResponse result = httpClientRequestExecutor.execute(request);
if (result.getEntity() != null) {
result.getEntity().writeTo(new NullOutputStream());
// We should have had a NullpointerException
}
}
/**
* Test that we don't have a NullpointerException when forcing the caching (ttl).
*
* @throws Exception
*/
public void testForcedTtlWith302ResponseCode() throws Exception {
properties = new PropertiesBuilder() //
.set(Parameters.REMOTE_URL_BASE, "http://localhost:8080") //
.set(Parameters.TTL, 1000) //
.build();
createHttpClientRequestExecutor();
DriverRequest originalRequest = TestUtils.createDriverRequest(driver);
OutgoingRequest request =
httpClientRequestExecutor.createOutgoingRequest(originalRequest, "http://localhost:8080", true);
HttpResponse response =
new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1),
HttpStatus.SC_MOVED_TEMPORARILY, "Moved temporarily"));
response.addHeader("Location", "http://www.foo.com");
mockConnectionManager.setResponse(response);
HttpResponse result = httpClientRequestExecutor.execute(request);
if (result.getEntity() != null) {
result.getEntity().writeTo(new NullOutputStream());
// We should have had a NullpointerException
}
}
public void testWriteGiantRecord() throws IOException {
PrintStream dummyStream = new PrintStream(new NullOutputStream());
ARCWriter arcWriter =
new ARCWriter(
SERIAL_NO,
dummyStream,
new File("dummy"),
new WriterPoolSettingsData(
"",
"",
-1,
false,
null,
null));
assertNotNull(arcWriter);
// Start the record with an arbitrary 14-digit date per RFC2540
long now = System.currentTimeMillis();
long recordLength = org.apache.commons.io.FileUtils.ONE_GB * 3;
arcWriter.write("dummy:uri", "application/octet-stream",
"0.1.2.3", now, recordLength, new NullInputStream(recordLength));
arcWriter.close();
}
public void runBeeLine(String sqlScriptFile) throws IOException {
List<String> argList = new ArrayList<String>();
argList.add("-u");
argList.add(connectionURL);
argList.add("-d");
argList
.add(driver);
argList.add("-n");
argList.add(userName);
argList.add("-p");
argList.add(passWord);
argList.add("-f");
argList.add(sqlScriptFile);
BeeLine beeLine = new BeeLine();
if (!verbose) {
beeLine.setOutputStream(new PrintStream(new NullOutputStream()));
// beeLine.getOpts().setSilent(true);
}
// beeLine.getOpts().setAllowMultiLineCommand(false);
// beeLine.getOpts().setIsolation("TRANSACTION_READ_COMMITTED");
int status = beeLine.begin(argList.toArray(new String[0]), null);
if (status != 0) {
throw new IOException("Schema script failed, errorcode " + status);
}
}
/**
* This method is called when the current request is a in-only operations,
* so a result is not expected.
*/
public void executeInOnly() throws XMLStreamException {
/* in case there is a result, write it to /dev/null */
XMLStreamWriter xmlWriter = DBUtils.getXMLOutputFactory().createXMLStreamWriter(
new NullOutputStream());
this.serialize(xmlWriter);
}
/**
* This is called when a boxcarring session is over,
* and the stored requests will be executed,
* the result of the last operation is returned.
*/
public synchronized OMElement execute() throws DataServiceFault {
OMElement result;
List<DataServiceRequest> reqList = this.getRequests();
int n = reqList.size();
OMElement resultElement = null;
for (int i = 0; i < n; i++) {
result = reqList.get(i).dispatch();
if (result != null) {
try {
/* if it's the last request, return the result,
* getXMLStreamReader() method will execute the actual request */
if (i == (n - 1)) {
resultElement = DBUtils.cloneAndReturnBuiltElement(result);
return DBUtils.wrapBoxCarringResponse(resultElement);
} else {
/* process the result of the request, no need to cache the data */
result.serializeAndConsume(new NullOutputStream());
}
} catch (XMLStreamException e) {
throw new DataServiceFault(e, "Error in request box result serializing");
}
} else {
if (i == (n - 1)) {
return DBUtils.wrapBoxCarringResponse(resultElement);
}
}
}
return null;
}
/**
* @return The stream to write to after the entry signal was received.
*/
public OutputStream getStream() {
if(null == stream) {
// Nothing to write
return NullOutputStream.NULL_OUTPUT_STREAM;
}
return stream;
}
/**
* @return The stream to write to after the entry signal was received.
*/
public OutputStream getStream() {
if(null == stream) {
// Nothing to write
return NullOutputStream.NULL_OUTPUT_STREAM;
}
return stream;
}
@Test(expected = IOException.class)
public void testClose() throws Exception {
try {
new HttpResponseOutputStream<Void>(new NullOutputStream()) {
@Override
public Void getStatus() throws BackgroundException {
throw new InteroperabilityException("d");
}
}.close();
}
catch(IOException e) {
assertEquals("d. Please contact your web hosting service provider for assistance.", e.getMessage());
throw e;
}
}
@Test
public void testTransferFixedLength() throws Exception {
final TransferStatus status = new TransferStatus().length(432768L);
new StreamCopier(status, status).withLimit(432768L).transfer(new NullInputStream(432768L), new NullOutputStream());
assertTrue(status.isComplete());
assertEquals(432768L, status.getOffset(), 0L);
}