001    package railo.transformer.bytecode.extern;
002    
003    import java.io.IOException;
004    
005    import railo.aprint;
006    import railo.commons.io.IOUtil;
007    import railo.commons.io.res.Resource;
008    import railo.commons.io.res.ResourcesImpl;
009    
010    public class StringExternalizerWriter {
011            
012            private StringBuilder sb=new StringBuilder();
013            private int offset=0;
014            private Resource res;
015            
016            public StringExternalizerWriter(Resource res) throws IOException{
017                    this.res=res;
018                    if(res.exists())res.delete();
019            }
020            
021            public Range write(String str){
022                    sb.append(str);
023                    return new Range(offset,(offset+=str.length())-1);
024            } 
025            
026            public void writeOut() throws IOException{
027                    if(sb.length()>0)IOUtil.write(res, sb.toString(),"UTF-8",false);
028            }
029            
030            public static class Range {
031    
032                    public final int from;
033                    public final int to;
034    
035                    public Range(int from, int to) {
036                            this.from=from;
037                            this.to=to;
038                    }
039                    public String toString(){
040                            return "from:"+from+";to:"+to+";";
041                    }
042                    
043            }
044            
045            public static void main(String[] args) throws IOException {
046                    Resource res = ResourcesImpl.getFileResourceProvider().getResource("/Users/mic/temp/externalize.txt");
047                    
048                    StringExternalizerWriter ext=new StringExternalizerWriter(res);
049                    Range r1 = ext.write("hallo");
050                    Range r2 = ext.write("peter");
051                    Range r3 = ext.write("m�ller");
052                    
053                    ext.writeOut();
054                    
055                    StringExternalizerReader reader=new StringExternalizerReader(res);
056                    aprint.o(r1);
057                    aprint.o(r2);
058                    aprint.o(r3);
059                    aprint.o(reader.read(r1.from, r1.to));
060                    aprint.o(reader.read(r2.from, r2.to));
061                    aprint.o(reader.read(r3.from, r3.to));
062                    
063                    
064                    
065            }
066    }