/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2012 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * Licensed 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 io.undertow.websockets.protocol.version07;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import io.undertow.websockets.WebSocketFrameType;
import io.undertow.websockets.protocol.version08.WebSocket08Channel;
import io.undertow.websockets.utf8.UTF8Checker;
import io.undertow.websockets.utf8.UTF8FileChannel;
import io.undertow.websockets.utf8.UTF8StreamSourceChannel;
import org.xnio.channels.StreamSinkChannel;
import org.xnio.channels.StreamSourceChannel;

/**
 * WebSocket08FrameSinkChannel that is used to write WebSocketFrameType#TEXT frames.
 *
 * It will check if the written payload contain any non-UTF8 data and if so throw
 * an {@link java.io.UnsupportedEncodingException}.
 *
 * @author <a href="mailto:nmaurer@redhat.com">Norman Maurer</a>
 */
public class WebSocket07TextFrameSinkChannel extends WebSocket07FrameSinkChannel {
    private final UTF8Checker checker = new UTF8Checker();

    public WebSocket07TextFrameSinkChannel(StreamSinkChannel channel, WebSocket07Channel wsChannel, long payloadSize) {
        super(channel, wsChannel, WebSocketFrameType.TEXT, payloadSize);
    }

    @Override
    public boolean isFragmentationSupported() {
        return true;
    }

    @Override
    public boolean areExtensionsSupported() {
        return true;
    }

    @Override
    protected int write0(ByteBuffer src) throws IOException {
        checker.checkUTF8(src, src.position(), src.limit());
        return super.write0(src);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    protected long write0(ByteBuffer[] srcs, int offset, int length) throws IOException {
        for (int i = offset; i < length; i++) {
            ByteBuffer src = srcs[i];
            checker.checkUTF8(src, src.position(), src.limit());
        }
        return super.write0(srcs, offset, length);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    protected long write0(ByteBuffer[] srcs) throws IOException {
        for (ByteBuffer src: srcs) {
            checker.checkUTF8(src, src.position(), src.limit());
        }
        return super.write0(srcs);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    protected long transferFrom0(FileChannel src, long position, long count) throws IOException {
        return super.transferFrom0(new UTF8FileChannel(src, checker), position, count);
    }

    @Override
    protected long transferFrom0(StreamSourceChannel source, long count, ByteBuffer throughBuffer) throws IOException {
        return super.transferFrom0(new UTF8StreamSourceChannel(source, checker), count, throughBuffer);
    }
}

@@ -901,77 +901,8 @@
 pe;%0A
-import io.undertow.websockets.protocol.version08.WebSocket08Channel;%0A
 impo
@@ -1244,142 +1244,8 @@
 %0A *%0A
- * It will check if the written payload contain any non-UTF8 data and if so throw%0A * an %7B@link java.io.UnsupportedEncodingException%7D.%0A
  *%0A 
@@ -1436,28 +1436,8 @@
 cker
- = new UTF8Checker()
 ;%0A%0A 
@@ -1567,72 +1567,392 @@
     
-super(channel, wsChannel, WebSocketFrameType.TEXT, payloadSize);
+this(channel, wsChannel, payloadSize, true);%0A    %7D%0A%0A    public WebSocket07TextFrameSinkChannel(StreamSinkChannel channel, WebSocket07Channel wsChannel, long payloadSize, boolean checkUtf8) %7B%0A        super(channel, wsChannel, WebSocketFrameType.TEXT, payloadSize);%0A        if (checkUtf8) %7B%0A            checker = new UTF8Checker();%0A        %7D else %7B%0A            checker = null;%0A        %7D
 %0A   
@@ -2201,32 +2201,67 @@
 s IOException %7B%0A
+        if (checker != null) %7B%0A    
         checker.
@@ -2297,32 +2297,42 @@
 , src.limit());%0A
+        %7D%0A
         return s
@@ -2352,88 +2352,8 @@
 rc);
-    //To change body of overridden methods use File %7C Settings %7C File Templates.
 %0A   
@@ -2452,32 +2452,67 @@
 s IOException %7B%0A
+        if (checker != null) %7B%0A    
         for (int
@@ -2555,16 +2555,20 @@
         
+    
 ByteBuff
@@ -2589,32 +2589,36 @@
 i%5D;%0A            
+   
+ 
 checker.checkUTF
@@ -2646,32 +2646,46 @@
 , src.limit());%0A
+            %7D%0A
         %7D%0A      
@@ -2888,32 +2888,67 @@
 s IOException %7B%0A
+        if (checker != null) %7B%0A    
         for (Byt
@@ -2964,24 +2964,28 @@
 rc: srcs) %7B%0A
+    
             
@@ -3029,32 +3029,46 @@
 , src.limit());%0A
+            %7D%0A
         %7D%0A      
@@ -3287,32 +3287,135 @@
 s IOException %7B%0A
+        if (checker == null) %7B%0A            return super.transferFrom0(src, position, count);%0A        %7D%0A
         return s
@@ -3619,32 +3619,143 @@
 s IOException %7B%0A
+        if (checker == null) %7B%0A            return super.transferFrom0(source, count, throughBuffer);%0A        %7D%0A
         return s

Allow to disable utf8 checking when using the sink