X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.core%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fcore%2Fexecution%2FSedFilteredResource.java;fp=runtime%2Forg.argeo.slc.core%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fcore%2Fexecution%2FSedFilteredResource.java;h=9b4ceff47c418bbc24261f9a8932e860afa1924d;hb=7a25f69ee62187261e9cdd19def72f1d3e9dadfb;hp=676c025804f5dc3ed193da0810ac3ddabe38fe5e;hpb=42ccec04f34da0ff3b3040f693b1afc65ac5ca69;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/SedFilteredResource.java b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/SedFilteredResource.java index 676c02580..9b4ceff47 100644 --- a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/SedFilteredResource.java +++ b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/SedFilteredResource.java @@ -8,7 +8,6 @@ import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.channels.Channels; import java.nio.channels.FileChannel; -import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; @@ -21,12 +20,14 @@ import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.commons.io.IOUtils; import org.argeo.slc.SlcException; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.Resource; +/** Experimental and suboptimal */ public class SedFilteredResource implements FactoryBean, InitializingBean { private Resource source; @@ -42,7 +43,7 @@ public class SedFilteredResource implements FactoryBean, InitializingBean { if (filters.size() == 0) return source; - int capacity = 100 * 1024;// 100 KB + //int capacity = 100 * 1024;// 100 KB ByteBuffer bb; if (source instanceof ByteArrayResource) { bb = ByteBuffer.wrap(((ByteArrayResource) source).getByteArray()); @@ -56,22 +57,29 @@ public class SedFilteredResource implements FactoryBean, InitializingBean { int sz = (int) fc.size(); bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz); } catch (IOException e) { - ReadableByteChannel channel = Channels.newChannel(source - .getInputStream()); - bb = ByteBuffer.allocateDirect(capacity); - channel.read(bb); + // ReadableByteChannel channel = Channels.newChannel(source + // .getInputStream()); + // bb = ByteBuffer.allocateDirect(capacity); + // int read = 0; + // do { + // read = channel.read(bb); + // } while (read > 0); + // FIXME : use nio to parse the stream as it goes + bb = ByteBuffer.wrap(IOUtils.toByteArray(source + .getInputStream())); } } CharBuffer cb = decoder.decode(bb); for (Pattern pattern : patterns.keySet()) { Matcher matcher = pattern.matcher(cb); - matcher.replaceAll(patterns.get(pattern)); + String output = matcher.replaceAll(patterns.get(pattern)); + cb = CharBuffer.wrap(output); } - ByteBuffer bbout = encoder.encode(cb); - ByteArrayOutputStream out = new ByteArrayOutputStream(capacity); - WritableByteChannel wchannel = Channels.newChannel(out); - wchannel.write(bbout); - ByteArrayResource res = new ByteArrayResource(out.toByteArray()); +// ByteBuffer bbout = encoder.encode(cb); +// ByteArrayOutputStream out = new ByteArrayOutputStream(capacity); +// WritableByteChannel wchannel = Channels.newChannel(out); +// wchannel.write(bbout); + ByteArrayResource res = new ByteArrayResource(cb.toString().getBytes()); return res; }