1 14 package com.coldcore.coloradoftp.filter.impl; 15 16 import org.apache.log4j.Logger; 17 18 import java.io.ByteArrayOutputStream ; 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.nio.ByteBuffer ; 22 import java.nio.channels.Channels ; 23 import java.nio.channels.WritableByteChannel ; 24 25 public class TypeADataFilter extends GenericDataFilter { 26 27 private static Logger log = Logger.getLogger(TypeADataFilter.class); 28 protected boolean windows; 29 protected ByteArrayOutputStream bout; 30 protected WritableByteChannel twbc; 31 protected ByteBuffer rbuffer; 32 protected byte[] tarray; 33 34 35 public TypeADataFilter() throws IOException { 36 windows = File.separator.equals("\\"); 37 38 bout = new ByteArrayOutputStream (); 39 twbc = Channels.newChannel(bout); 40 } 41 42 43 44 public TypeADataFilter(int bufferSize) throws IOException { 45 super(); 46 } 47 48 49 52 public void setWindows(boolean windows) { 53 this.windows = windows; 54 } 55 56 57 public int read(ByteBuffer dst) throws IOException { 58 if (dst.position() > 0 || dst.limit() != dst.capacity()) return 0; 60 61 int rcap = dst.capacity()/2; 62 if (rbuffer == null || rbuffer.capacity() > rcap) { 63 rbuffer = ByteBuffer.allocate(rcap); 64 rbuffer.flip(); 65 } 66 67 rbuffer.clear(); 69 int read = rbc.read(rbuffer); 70 if (read < 1) return read; 71 byte[] bytes = rbuffer.array(); 72 73 byte[] barr = new byte[read*2]; 75 byte b13 = (byte) 13; 76 int put = 0; 77 for (int z = 0; z < read; z++) { 78 byte b = bytes[z]; 79 if (b == 13) continue; 80 if (b == 10) barr[put++] = b13; 81 barr[put++] = b; 82 } 83 84 dst.put(barr, 0, put); 86 87 return barr.length; 89 } 90 91 92 93 public int write(ByteBuffer src) throws IOException { 94 95 int rcap = src.capacity()*2; 96 if (rbuffer == null || rbuffer.capacity() > rcap) { 97 rbuffer = ByteBuffer.allocate(rcap); 98 rbuffer.flip(); 99 } 100 101 rbuffer.clear(); 103 int read = src.remaining(); 104 rbuffer.put(src); 105 byte[] bytes = rbuffer.array(); 106 107 byte[] barr = new byte[read*2]; 109 byte b13 = (byte) 13; 110 int put = 0; 111 for (int z = 0; z < read; z++) { 112 byte b = bytes[z]; 113 if (b == 13) continue; 114 if (b == 10 && windows) barr[put++] = b13; 115 barr[put++] = b; 116 } 117 118 rbuffer.clear(); 120 rbuffer.put(barr, 0, put); 121 rbuffer.flip(); 122 123 int i = wbc.write(rbuffer); 125 if (i != put) { 126 throw new RuntimeException ("BUG: Data did not fit into channel"); 128 } 129 130 return read; 132 } 133 134 135 public boolean mayModifyDataLength() { 136 return true; 137 } 138 } 139 | Popular Tags |