KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > coldcore > coloradoftp > filter > impl > TypeADataFilter


1 /**
2  * @see com.coldcore.coloradoftp.filter.DataFilter
3  *
4  * TYPE A filter.
5  *
6  * Replaces on upload line feeds to platform default line feeds.
7  * On download this class does nothing.
8  *
9  * This class is capable of replacing Windows (#13#10) and Unix (#10) line feeds.
10  *
11  * WARNING! Buffer size property must match the buffer size used by a data connection,
12  * otherwise the performance will suffer.
13  */

14 package com.coldcore.coloradoftp.filter.impl;
15
16 import org.apache.log4j.Logger;
17
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.nio.ByteBuffer JavaDoc;
22 import java.nio.channels.Channels JavaDoc;
23 import java.nio.channels.WritableByteChannel JavaDoc;
24
25 public class TypeADataFilter extends GenericDataFilter {
26
27   private static Logger log = Logger.getLogger(TypeADataFilter.class);
28   protected boolean windows;
29   protected ByteArrayOutputStream JavaDoc bout;
30   protected WritableByteChannel JavaDoc twbc;
31   protected ByteBuffer JavaDoc rbuffer;
32   protected byte[] tarray;
33
34
35   public TypeADataFilter() throws IOException JavaDoc {
36     windows = File.separator.equals("\\");
37
38     bout = new ByteArrayOutputStream JavaDoc();
39     twbc = Channels.newChannel(bout);
40   }
41
42
43   /** @deprecated Supports clients with v1.22 configuration files */
44   public TypeADataFilter(int bufferSize) throws IOException JavaDoc {
45     super();
46   }
47
48
49   /** Set type of OS and type of line feeds use (Windows or Unix)
50    * @param windows TRUE for Windows, FALSE for Unix
51    */

52   public void setWindows(boolean windows) {
53     this.windows = windows;
54   }
55
56
57   public int read(ByteBuffer JavaDoc dst) throws IOException JavaDoc {
58     //Do not do anything until the destination buffer is clear, save extra efforts
59
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     //Read data from the channel into the buffer and get it as a byte array
68
rbuffer.clear();
69     int read = rbc.read(rbuffer);
70     if (read < 1) return read;
71     byte[] bytes = rbuffer.array();
72
73     //Replace \n to \r\n
74
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     //Write the array with replaced line feeds into the destination buffer
85
dst.put(barr, 0, put);
86
87     //Return how many bytes were put into the destination buffer
88
return barr.length;
89   }
90
91
92
93   public int write(ByteBuffer JavaDoc src) throws IOException JavaDoc {
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     //Read data from the source into the buffer and get it as a byte array
102
rbuffer.clear();
103     int read = src.remaining();
104     rbuffer.put(src);
105     byte[] bytes = rbuffer.array();
106
107     //Replace \n to \r\n or \r\n to \n
108
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     //Write the array with replaced line feeds into the buffer
119
rbuffer.clear();
120     rbuffer.put(barr, 0, put);
121     rbuffer.flip();
122
123     //Forward the buffer to the underlying channel
124
int i = wbc.write(rbuffer);
125     if (i != put) {
126       //File will be damaged, cannot allow that
127
throw new RuntimeException JavaDoc("BUG: Data did not fit into channel");
128     }
129
130     //Return how many bytes were read from the source buffer
131
return read;
132   }
133
134
135   public boolean mayModifyDataLength() {
136     return true;
137   }
138 }
139
Popular Tags