You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by ng...@apache.org on 2008/11/28 19:56:16 UTC

svn commit: r721551 - in /mina/ftpserver/trunk/core/src: main/java/org/apache/ftpserver/impl/IODataConnection.java test/java/org/apache/ftpserver/clienttests/StoreTest.java

Author: ngn
Date: Fri Nov 28 10:56:16 2008
New Revision: 721551

URL: http://svn.apache.org/viewvc?rev=721551&view=rev
Log:
Make sure we store files sent in ASCII mode with the local line endings (FTPSERVER-184)

Modified:
    mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/impl/IODataConnection.java
    mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/clienttests/StoreTest.java

Modified: mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/impl/IODataConnection.java
URL: http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/impl/IODataConnection.java?rev=721551&r1=721550&r2=721551&view=diff
==============================================================================
--- mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/impl/IODataConnection.java (original)
+++ mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/impl/IODataConnection.java Fri Nov 28 10:56:16 2008
@@ -47,6 +47,8 @@
  */
 public class IODataConnection implements DataConnection {
 
+    private static final byte[] EOL = System.getProperty("line.separator").getBytes();
+    
     private FtpIoSession session;
 
     private Socket socket;
@@ -207,7 +209,7 @@
                 defaultFtpSession = (DefaultFtpSession) session;
             }
 
-            boolean lastWasCR = false;
+            byte lastByte = 0;
             while (true) {
 
                 // if current rate exceeds the max rate, sleep for 50ms
@@ -253,16 +255,26 @@
                 if (isAscii) {
                     for (int i = 0; i < count; ++i) {
                         byte b = buff[i];
-                        if (b == '\n' && !lastWasCR) {
-                            bos.write('\r');
-                        }
-
-                        if (b == '\r') {
-                            lastWasCR = true;
+                        if(isWrite) {
+                            if (b == '\n' && lastByte != '\r') {
+                                bos.write('\r');
+                            }
+    
+                            bos.write(b);
                         } else {
-                            lastWasCR = false;
+                            if(b == '\n') {
+                                // for reads, we should always get \r\n
+                                // so what we do here is to ignore \n bytes 
+                                // and on \r dump the system local line ending
+                            } else if(b == '\r') {
+                                bos.write(EOL);
+                            } else {
+                                // not a line ending, just output
+                                bos.write(b);
+                            }
                         }
-                        bos.write(b);
+                        // store this byte so that we can compare it for line endings
+                        lastByte = b;
                     }
                 } else {
                     bos.write(buff, 0, count);

Modified: mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/clienttests/StoreTest.java
URL: http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/clienttests/StoreTest.java?rev=721551&r1=721550&r2=721551&view=diff
==============================================================================
--- mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/clienttests/StoreTest.java (original)
+++ mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/clienttests/StoreTest.java Fri Nov 28 10:56:16 2008
@@ -32,7 +32,12 @@
 *
 */
 public class StoreTest extends ClientTestTemplate {
-    private static final String TESTDATA = "TESTDATA\r\nline2\r\n";
+    private static final String EOL = System.getProperty("line.separator");
+    private static final String CRLF = "\r\n";
+
+    
+    private static final String TESTDATA = "TESTDATA" + EOL + "line2" + EOL;
+    private static final String TESTDATA_CRLF = "TESTDATA" + CRLF + "line2" + CRLF;
 
     private static final String ENCODING = "UTF-8";
 
@@ -45,6 +50,7 @@
     private static final File TEST_DIR = new File(ROOT_DIR, "foo/bar");
 
     private static byte[] testData = null;
+    private static byte[] testDataCrLf = null;
 
     private static byte[] doubleTestData = null;
 
@@ -59,6 +65,7 @@
         super.setUp();
 
         testData = TESTDATA.getBytes(ENCODING);
+        testDataCrLf = TESTDATA_CRLF.getBytes(ENCODING);
         doubleTestData = (TESTDATA + TESTDATA).getBytes(ENCODING);
         oneAndAHalfTestData = ("TEST" + TESTDATA).getBytes(ENCODING);
 
@@ -75,6 +82,20 @@
         TestUtil.assertFileEqual(testData, testFile);
     }
 
+    /**
+     * We should always store files with the local line endings (FTPSERVER-184) 
+     * 
+     */
+    public void testStoreWithCrLf() throws Exception {
+        File testFile = new File(ROOT_DIR, TEST_FILENAME);
+
+        assertTrue(client.storeFile(TEST_FILENAME, new ByteArrayInputStream(
+                testDataCrLf)));
+
+        assertTrue(testFile.exists());
+        TestUtil.assertFileEqual(testData, testFile);
+    }
+    
     public void testStoreWithLeadingSpace() throws Exception {
         File testFile = new File(ROOT_DIR, TEST_FILENAME_WITH_LEADING_SPACE);