CharacterStreamSourceAdapter now takes a Reader, and CharacterStreamTargetAdapter takes a Writer (INT-148).
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,38 +18,42 @@ package org.springframework.integration.adapter.stream;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.integration.adapter.PollableSource;
|
||||
import org.springframework.integration.message.MessageDeliveryException;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A pollable source for text-based {@link InputStream InputStreams}.
|
||||
* A pollable source for {@link Reader Readers}.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class CharacterStreamSource implements PollableSource<String> {
|
||||
|
||||
private BufferedReader reader;
|
||||
private final BufferedReader reader;
|
||||
|
||||
private Object streamMonitor;
|
||||
private final Object monitor;
|
||||
|
||||
|
||||
public CharacterStreamSource(InputStream stream) {
|
||||
this(stream, -1);
|
||||
public CharacterStreamSource(Reader reader) {
|
||||
this(reader, -1);
|
||||
}
|
||||
|
||||
public CharacterStreamSource(InputStream stream, int bufferSize) {
|
||||
this.streamMonitor = stream;
|
||||
if (bufferSize > 0) {
|
||||
this.reader = new BufferedReader(new InputStreamReader(stream), bufferSize);
|
||||
public CharacterStreamSource(Reader reader, int bufferSize) {
|
||||
Assert.notNull(reader, "reader must not be null");
|
||||
this.monitor = reader;
|
||||
if (reader instanceof BufferedReader) {
|
||||
this.reader = (BufferedReader) reader;
|
||||
}
|
||||
else if (bufferSize > 0) {
|
||||
this.reader = new BufferedReader(reader, bufferSize);
|
||||
}
|
||||
else {
|
||||
this.reader = new BufferedReader(new InputStreamReader(stream));
|
||||
this.reader = new BufferedReader(reader);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,12 +63,12 @@ public class CharacterStreamSource implements PollableSource<String> {
|
||||
while (results.size() < limit) {
|
||||
try {
|
||||
String line = null;
|
||||
synchronized (this.streamMonitor) {
|
||||
boolean isReady = reader.ready();
|
||||
synchronized (this.monitor) {
|
||||
boolean isReady = this.reader.ready();
|
||||
if (!isReady) {
|
||||
return results;
|
||||
}
|
||||
line = reader.readLine();
|
||||
line = this.reader.readLine();
|
||||
}
|
||||
if (line == null) {
|
||||
return results;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,7 +16,8 @@
|
||||
|
||||
package org.springframework.integration.adapter.stream;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
|
||||
import org.springframework.integration.adapter.PollingSourceAdapter;
|
||||
import org.springframework.integration.channel.MessageChannel;
|
||||
@@ -28,8 +29,8 @@ import org.springframework.integration.channel.MessageChannel;
|
||||
*/
|
||||
public class CharacterStreamSourceAdapter extends PollingSourceAdapter<String> {
|
||||
|
||||
public CharacterStreamSourceAdapter(InputStream stream) {
|
||||
super(new CharacterStreamSource(stream));
|
||||
public CharacterStreamSourceAdapter(Reader reader) {
|
||||
super(new CharacterStreamSource(reader));
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +38,8 @@ public class CharacterStreamSourceAdapter extends PollingSourceAdapter<String> {
|
||||
* Factory method that creates an adapter for stdin (System.in).
|
||||
*/
|
||||
public static CharacterStreamSourceAdapter stdinAdapter(MessageChannel channel) {
|
||||
CharacterStreamSourceAdapter adapter = new CharacterStreamSourceAdapter(System.in);
|
||||
CharacterStreamSourceAdapter adapter =
|
||||
new CharacterStreamSourceAdapter(new InputStreamReader(System.in));
|
||||
adapter.setChannel(channel);
|
||||
return adapter;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,14 +18,15 @@ package org.springframework.integration.adapter.stream;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
|
||||
import org.springframework.integration.adapter.AbstractTargetAdapter;
|
||||
import org.springframework.integration.message.MessageHandlingException;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A target adapter that writes to an {@link OutputStream}. String-based
|
||||
* A target adapter that writes to a {@link Writer}. String-based
|
||||
* objects will be written directly, but if the object is not itself a
|
||||
* {@link String}, the adapter will write the result of the object's
|
||||
* {@link #toString()} method. To append a new-line after each write, set the
|
||||
@@ -36,21 +37,25 @@ import org.springframework.integration.message.MessageHandlingException;
|
||||
*/
|
||||
public class CharacterStreamTargetAdapter extends AbstractTargetAdapter {
|
||||
|
||||
private BufferedWriter writer;
|
||||
private final BufferedWriter writer;
|
||||
|
||||
private boolean shouldAppendNewLine = false;
|
||||
private volatile boolean shouldAppendNewLine = false;
|
||||
|
||||
|
||||
public CharacterStreamTargetAdapter(OutputStream stream) {
|
||||
this(stream, -1);
|
||||
public CharacterStreamTargetAdapter(Writer writer) {
|
||||
this(writer, -1);
|
||||
}
|
||||
|
||||
public CharacterStreamTargetAdapter(OutputStream stream, int bufferSize) {
|
||||
if (bufferSize > 0) {
|
||||
this.writer = new BufferedWriter(new OutputStreamWriter(stream), bufferSize);
|
||||
public CharacterStreamTargetAdapter(Writer writer, int bufferSize) {
|
||||
Assert.notNull(writer, "writer must not be null");
|
||||
if (writer instanceof BufferedWriter) {
|
||||
this.writer = (BufferedWriter) writer;
|
||||
}
|
||||
else if (bufferSize > 0) {
|
||||
this.writer = new BufferedWriter(writer, bufferSize);
|
||||
}
|
||||
else {
|
||||
this.writer = new BufferedWriter(new OutputStreamWriter(stream));
|
||||
this.writer = new BufferedWriter(writer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,14 +64,14 @@ public class CharacterStreamTargetAdapter extends AbstractTargetAdapter {
|
||||
* Factory method that creates an adapter for stdout (System.out).
|
||||
*/
|
||||
public static CharacterStreamTargetAdapter stdoutAdapter() {
|
||||
return new CharacterStreamTargetAdapter(System.out);
|
||||
return new CharacterStreamTargetAdapter(new OutputStreamWriter(System.out));
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method that creates an adapter for stderr (System.err).
|
||||
*/
|
||||
public static CharacterStreamTargetAdapter stderrAdapter() {
|
||||
return new CharacterStreamTargetAdapter(System.err);
|
||||
return new CharacterStreamTargetAdapter(new OutputStreamWriter(System.err));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user