Mercurial > hg4j
comparison src/org/tmatesoft/hg/internal/NewlineFilter.java @ 352:7b34d24b8f4d
Tests for newline filter (eol extension) functionality
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Wed, 30 Nov 2011 05:11:07 +0100 |
parents | a37ce7145c3f |
children | 0f3687e79f5a |
comparison
equal
deleted
inserted
replaced
351:5abba41751e6 | 352:7b34d24b8f4d |
---|---|
19 import static org.tmatesoft.hg.internal.Filter.Direction.FromRepo; | 19 import static org.tmatesoft.hg.internal.Filter.Direction.FromRepo; |
20 import static org.tmatesoft.hg.internal.Filter.Direction.ToRepo; | 20 import static org.tmatesoft.hg.internal.Filter.Direction.ToRepo; |
21 import static org.tmatesoft.hg.internal.KeywordFilter.copySlice; | 21 import static org.tmatesoft.hg.internal.KeywordFilter.copySlice; |
22 | 22 |
23 import java.io.File; | 23 import java.io.File; |
24 import java.io.FileInputStream; | |
25 import java.io.FileOutputStream; | |
26 import java.io.IOException; | 24 import java.io.IOException; |
27 import java.nio.ByteBuffer; | 25 import java.nio.ByteBuffer; |
28 import java.util.ArrayList; | 26 import java.util.ArrayList; |
29 import java.util.Map; | 27 import java.util.Map; |
30 | 28 |
41 | 39 |
42 // if allowInconsistent is true, filter simply pass incorrect newline characters (single \r or \r\n on *nix and single \n on Windows) as is, | 40 // if allowInconsistent is true, filter simply pass incorrect newline characters (single \r or \r\n on *nix and single \n on Windows) as is, |
43 // i.e. doesn't try to convert them into appropriate newline characters. XXX revisit if Keyword extension behaves differently | 41 // i.e. doesn't try to convert them into appropriate newline characters. XXX revisit if Keyword extension behaves differently |
44 private final boolean allowInconsistent; | 42 private final boolean allowInconsistent; |
45 private final boolean winToNix; | 43 private final boolean winToNix; |
44 | |
45 // next two factory methods for testsing purposes | |
46 public static NewlineFilter createWin2Nix(boolean allowMixed) { | |
47 return new NewlineFilter(!allowMixed, 0); | |
48 } | |
49 | |
50 public static NewlineFilter createNix2Win(boolean allowMixed) { | |
51 return new NewlineFilter(!allowMixed, 1); | |
52 } | |
46 | 53 |
47 private NewlineFilter(boolean failIfInconsistent, int transform) { | 54 private NewlineFilter(boolean failIfInconsistent, int transform) { |
48 winToNix = transform == 0; | 55 winToNix = transform == 0; |
49 allowInconsistent = !failIfInconsistent; | 56 allowInconsistent = !failIfInconsistent; |
50 } | 57 } |
248 return null; | 255 return null; |
249 } | 256 } |
250 return null; | 257 return null; |
251 } | 258 } |
252 } | 259 } |
253 | |
254 public static void main(String[] args) throws Exception { | |
255 FileInputStream fis = new FileInputStream(new File("/temp/design.lf.txt")); | |
256 FileOutputStream fos = new FileOutputStream(new File("/temp/design.newline.out")); | |
257 ByteBuffer b = ByteBuffer.allocate(12); | |
258 NewlineFilter nlFilter = new NewlineFilter(true, 1); | |
259 while (fis.getChannel().read(b) != -1) { | |
260 b.flip(); // get ready to be read | |
261 ByteBuffer f = nlFilter.filter(b); | |
262 fos.getChannel().write(f); // XXX in fact, f may not be fully consumed | |
263 if (b.hasRemaining()) { | |
264 b.compact(); | |
265 } else { | |
266 b.clear(); | |
267 } | |
268 } | |
269 fis.close(); | |
270 fos.flush(); | |
271 fos.close(); | |
272 } | |
273 | |
274 } | 260 } |