Browse Source

extract method refactoring

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@1557431 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 11 years ago
parent
commit
0e01584f3e
1 changed files with 178 additions and 137 deletions
  1. +178
    -137
      src/main/org/apache/tools/ant/util/ResourceUtils.java

+ 178
- 137
src/main/org/apache/tools/ant/util/ResourceUtils.java View File

@@ -396,7 +396,7 @@ public class ResourceUtils {
&& filterChains.size() > 0); && filterChains.size() > 0);
String effectiveInputEncoding = null; String effectiveInputEncoding = null;
if (source instanceof StringResource) { if (source instanceof StringResource) {
effectiveInputEncoding = ((StringResource) source).getEncoding();
effectiveInputEncoding = ((StringResource) source).getEncoding();
} else { } else {
effectiveInputEncoding = inputEncoding; effectiveInputEncoding = inputEncoding;
} }
@@ -414,151 +414,25 @@ public class ResourceUtils {
} }


if (filterSetsAvailable) { if (filterSetsAvailable) {
BufferedReader in = null;
BufferedWriter out = null;
try {
InputStreamReader isr = null;
if (effectiveInputEncoding == null) {
isr = new InputStreamReader(source.getInputStream());
} else {
isr = new InputStreamReader(source.getInputStream(),
effectiveInputEncoding);
}
in = new BufferedReader(isr);
OutputStream os = getOutputStream(dest, append, project);
OutputStreamWriter osw;
if (outputEncoding == null) {
osw = new OutputStreamWriter(os);
} else {
osw = new OutputStreamWriter(os, outputEncoding);
}
out = new BufferedWriter(osw);
if (filterChainsAvailable) {
ChainReaderHelper crh = new ChainReaderHelper();
crh.setBufferSize(FileUtils.BUF_SIZE);
crh.setPrimaryReader(in);
crh.setFilterChains(filterChains);
crh.setProject(project);
Reader rdr = crh.getAssembledReader();
in = new BufferedReader(rdr);
}
LineTokenizer lineTokenizer = new LineTokenizer();
lineTokenizer.setIncludeDelims(true);
String newline = null;
String line = lineTokenizer.getToken(in);
while (line != null) {
if (line.length() == 0) {
// this should not happen, because the lines are
// returned with the end of line delimiter
out.newLine();
} else {
newline = filters.replaceTokens(line);
out.write(newline);
}
line = lineTokenizer.getToken(in);
}
} finally {
FileUtils.close(out);
FileUtils.close(in);
}
copyWithFilterSets(source, dest, filters, filterChains,
filterChainsAvailable, append,
effectiveInputEncoding, outputEncoding,
project);
} else if (filterChainsAvailable } else if (filterChainsAvailable
|| (effectiveInputEncoding != null || (effectiveInputEncoding != null
&& !effectiveInputEncoding.equals(outputEncoding)) && !effectiveInputEncoding.equals(outputEncoding))
|| (effectiveInputEncoding == null && outputEncoding != null)) { || (effectiveInputEncoding == null && outputEncoding != null)) {
BufferedReader in = null;
BufferedWriter out = null;
try {
InputStreamReader isr = null;
if (effectiveInputEncoding == null) {
isr = new InputStreamReader(source.getInputStream());
} else {
isr = new InputStreamReader(source.getInputStream(),
effectiveInputEncoding);
}
in = new BufferedReader(isr);
OutputStream os = getOutputStream(dest, append, project);
OutputStreamWriter osw;
if (outputEncoding == null) {
osw = new OutputStreamWriter(os);
} else {
osw = new OutputStreamWriter(os, outputEncoding);
}
out = new BufferedWriter(osw);
if (filterChainsAvailable) {
ChainReaderHelper crh = new ChainReaderHelper();
crh.setBufferSize(FileUtils.BUF_SIZE);
crh.setPrimaryReader(in);
crh.setFilterChains(filterChains);
crh.setProject(project);
Reader rdr = crh.getAssembledReader();
in = new BufferedReader(rdr);
}
char[] buffer = new char[FileUtils.BUF_SIZE];
while (true) {
int nRead = in.read(buffer, 0, buffer.length);
if (nRead == -1) {
break;
}
out.write(buffer, 0, nRead);
}
} finally {
FileUtils.close(out);
FileUtils.close(in);
}
copyWithFilterChainsOrTranscoding(source, dest, filterChains,
filterChainsAvailable, append,
effectiveInputEncoding,
outputEncoding, project);
} else if (source.as(FileProvider.class) != null } else if (source.as(FileProvider.class) != null
&& destFile != null) { && destFile != null) {
File sourceFile = File sourceFile =
source.as(FileProvider.class).getFile(); source.as(FileProvider.class).getFile();

File parent = destFile.getParentFile();
if (parent != null && !parent.isDirectory()
&& !(parent.mkdirs() || parent.isDirectory())) {
throw new IOException("failed to create the parent directory"
+ " for " + destFile);
}

FileInputStream in = null;
FileOutputStream out = null;
FileChannel srcChannel = null;
FileChannel destChannel = null;

try {
in = new FileInputStream(sourceFile);
out = new FileOutputStream(destFile);
srcChannel = in.getChannel();
destChannel = out.getChannel();
long position = 0;
long count = srcChannel.size();
while (position < count) {
long chunk = Math.min(MAX_IO_CHUNK_SIZE, count - position);
position +=
destChannel.transferFrom(srcChannel, position, chunk);
}
} finally {
FileUtils.close(srcChannel);
FileUtils.close(destChannel);
FileUtils.close(out);
FileUtils.close(in);
}
copyUsingFileChannels(sourceFile, destFile);
} else { } else {
InputStream in = null;
OutputStream out = null;
try {
in = source.getInputStream();
out = getOutputStream(dest, append, project);

byte[] buffer = new byte[FileUtils.BUF_SIZE];
int count = 0;
do {
out.write(buffer, 0, count);
count = in.read(buffer, 0, buffer.length);
} while (count != -1);
} finally {
FileUtils.close(out);
FileUtils.close(in);
}
copyUsingStreams(source, dest, append, project);
} }
if (preserveLastModified) { if (preserveLastModified) {
Touchable t = dest.as(Touchable.class); Touchable t = dest.as(Touchable.class);
@@ -768,6 +642,173 @@ public class ResourceUtils {
} }
} }


private static void copyWithFilterSets(Resource source, Resource dest,
FilterSetCollection filters,
Vector filterChains,
boolean filterChainsAvailable,
boolean append, String inputEncoding,
String outputEncoding,
Project project)
throws IOException {
BufferedReader in = null;
BufferedWriter out = null;
try {
InputStreamReader isr = null;
if (inputEncoding == null) {
isr = new InputStreamReader(source.getInputStream());
} else {
isr = new InputStreamReader(source.getInputStream(),
inputEncoding);
}
in = new BufferedReader(isr);
OutputStream os = getOutputStream(dest, append, project);
OutputStreamWriter osw;
if (outputEncoding == null) {
osw = new OutputStreamWriter(os);
} else {
osw = new OutputStreamWriter(os, outputEncoding);
}
out = new BufferedWriter(osw);
if (filterChainsAvailable) {
ChainReaderHelper crh = new ChainReaderHelper();
crh.setBufferSize(FileUtils.BUF_SIZE);
crh.setPrimaryReader(in);
crh.setFilterChains(filterChains);
crh.setProject(project);
Reader rdr = crh.getAssembledReader();
in = new BufferedReader(rdr);
}
LineTokenizer lineTokenizer = new LineTokenizer();
lineTokenizer.setIncludeDelims(true);
String newline = null;
String line = lineTokenizer.getToken(in);
while (line != null) {
if (line.length() == 0) {
// this should not happen, because the lines are
// returned with the end of line delimiter
out.newLine();
} else {
newline = filters.replaceTokens(line);
out.write(newline);
}
line = lineTokenizer.getToken(in);
}
} finally {
FileUtils.close(out);
FileUtils.close(in);
}
}

private static void copyWithFilterChainsOrTranscoding(Resource source,
Resource dest,
Vector filterChains,
boolean filterChainsAvailable,
boolean append,
String inputEncoding,
String outputEncoding,
Project project)
throws IOException {
BufferedReader in = null;
BufferedWriter out = null;
try {
InputStreamReader isr = null;
if (inputEncoding == null) {
isr = new InputStreamReader(source.getInputStream());
} else {
isr = new InputStreamReader(source.getInputStream(),
inputEncoding);
}
in = new BufferedReader(isr);
OutputStream os = getOutputStream(dest, append, project);
OutputStreamWriter osw;
if (outputEncoding == null) {
osw = new OutputStreamWriter(os);
} else {
osw = new OutputStreamWriter(os, outputEncoding);
}
out = new BufferedWriter(osw);
if (filterChainsAvailable) {
ChainReaderHelper crh = new ChainReaderHelper();
crh.setBufferSize(FileUtils.BUF_SIZE);
crh.setPrimaryReader(in);
crh.setFilterChains(filterChains);
crh.setProject(project);
Reader rdr = crh.getAssembledReader();
in = new BufferedReader(rdr);
}
char[] buffer = new char[FileUtils.BUF_SIZE];
while (true) {
int nRead = in.read(buffer, 0, buffer.length);
if (nRead == -1) {
break;
}
out.write(buffer, 0, nRead);
}
} finally {
FileUtils.close(out);
FileUtils.close(in);
}
}

private static void copyUsingFileChannels(File sourceFile,
File destFile)
throws IOException {

File parent = destFile.getParentFile();
if (parent != null && !parent.isDirectory()
&& !(parent.mkdirs() || parent.isDirectory())) {
throw new IOException("failed to create the parent directory"
+ " for " + destFile);
}

FileInputStream in = null;
FileOutputStream out = null;
FileChannel srcChannel = null;
FileChannel destChannel = null;

try {
in = new FileInputStream(sourceFile);
out = new FileOutputStream(destFile);
srcChannel = in.getChannel();
destChannel = out.getChannel();
long position = 0;
long count = srcChannel.size();
while (position < count) {
long chunk = Math.min(MAX_IO_CHUNK_SIZE, count - position);
position +=
destChannel.transferFrom(srcChannel, position, chunk);
}
} finally {
FileUtils.close(srcChannel);
FileUtils.close(destChannel);
FileUtils.close(out);
FileUtils.close(in);
}
}

private static void copyUsingStreams(Resource source, Resource dest,
boolean append, Project project)
throws IOException {
InputStream in = null;
OutputStream out = null;
try {
in = source.getInputStream();
out = getOutputStream(dest, append, project);

byte[] buffer = new byte[FileUtils.BUF_SIZE];
int count = 0;
do {
out.write(buffer, 0, count);
count = in.read(buffer, 0, buffer.length);
} while (count != -1);
} finally {
FileUtils.close(out);
FileUtils.close(in);
}
}

private static OutputStream getOutputStream(Resource resource, boolean append, Project project) private static OutputStream getOutputStream(Resource resource, boolean append, Project project)
throws IOException { throws IOException {
if (append) { if (append) {


Loading…
Cancel
Save