/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you under the Apache License, Version 2.0 (the            *
 * "License"); you may not use this file except in compliance   *
 * with the License.  You may obtain a copy of the License at   *
 *                                                              *
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 *                                                              *
 * Unless required by applicable law or agreed to in writing,   *
 * software distributed under the License is distributed on an  *
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 * KIND, either express or implied.  See the License for the    *
 * specific language governing permissions and limitations      *
 * under the License.                                           *
 ****************************************************************/
package org.apache.james.socket.netty;

import org.apache.james.protocols.api.LineHandler;
import org.apache.james.protocols.api.ProtocolSession;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipelineCoverage;
import org.jboss.netty.channel.ChannelUpstreamHandler;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;

/**
 * {@link ChannelUpstreamHandler} implementation which will call a given {@link LineHandler} implementation
 *
 * @param <Session>
 */
@ChannelPipelineCoverage("one")
public class LineHandlerUpstreamHandler<Session extends ProtocolSession> extends SimpleChannelUpstreamHandler implements ChannelAttributeSupport{

    private LineHandler<Session> handler;
    public LineHandlerUpstreamHandler(LineHandler<Session> handler) {
        this.handler = handler;
    }
    @SuppressWarnings("unchecked")
    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        Session pSession = (Session) attributes.get(ctx.getChannel());
        
        // Copy buffer and add CRLF to it
        ChannelBuffer buf = (ChannelBuffer) e.getMessage();      
        ChannelBuffer lineBuf = ChannelBuffers.buffer(buf.capacity() + 2);
        lineBuf.writeBytes(buf);
        lineBuf.writeInt('\r');
        lineBuf.writeInt('\n');
        byte[] lineBytes = new byte[lineBuf.capacity()];
        lineBuf.getBytes(0, lineBytes);
        handler.onLine(pSession, lineBytes);

        
    }

}

@@ -1400,55 +1400,8 @@
 r;%0D%0A
-import org.jboss.netty.buffer.ChannelBuffers;%0D%0A
 impo
@@ -2386,51 +2386,8 @@
   %0D%0A
-        // Copy buffer and add CRLF to it%0D%0A
     
@@ -2461,54 +2461,49 @@
     
-ChannelBuffer lineBuf = ChannelBuffers.buffer(
+        %0D%0A        byte%5B%5D line = new byte%5B
 buf.
@@ -2516,13 +2516,9 @@
 ty()
- + 2)
+%5D
 ;%0D%0A 
@@ -2528,42 +2528,42 @@
     
-lineBuf.writeBytes(buf
+buf.getBytes(0, line
 );%0D%0A
+%0D%0A
         
 line
@@ -2562,64 +2562,149 @@
     
-lineBuf.writeInt('%5Cr');%0D%0A        lineBuf.writeInt('%5Cn');
+// TODO: improve me!%0D%0A        // thats not the most performant thing but it at least works for now%0D%0A        // this should get improved later
 %0D%0A  
@@ -2716,25 +2716,23 @@
  byte%5B%5D 
-lineBytes
+newLine
  = new b
@@ -2743,65 +2743,205 @@
 line
-Buf.capacity()%5D;%0D%0A        lineBuf.getBytes(0, lineBytes);
+.length +2%5D;%0D%0A        for (int i = 0; i %3C line.length; i++) %7B%0D%0A            newLine%5Bi%5D = line%5Bi%5D;%0D%0A        %7D%0D%0A        newLine%5BnewLine.length -2%5D = '%5Cr';%0D%0A        newLine%5BnewLine.length -1%5D = '%5Cn';%0D%0A
 %0D%0A  
@@ -2974,18 +2974,15 @@
 ion,
- lineBytes
+newLine
 );%0D%0A

Revert changes.. not workin as it should..