/*
 * 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.WebSocketChannel;
import io.undertow.websockets.WebSocketFrameType;
import io.undertow.websockets.protocol.WebSocketFixedPayloadMaskedFrameSourceChannel;
import io.undertow.websockets.utf8.UTF8Checker;
import io.undertow.websockets.utf8.UTF8FileChannel;
import io.undertow.websockets.utf8.UTF8StreamSinkChannel;
import org.xnio.channels.StreamSinkChannel;
import org.xnio.channels.StreamSourceChannel;

/**
 * @author <a href="mailto:nmaurer@redhat.com">Norman Maurer</a>
 */
public class WebSocket07TextFrameSourceChannel extends WebSocketFixedPayloadMaskedFrameSourceChannel {
    private final UTF8Checker checker = new UTF8Checker();

    public WebSocket07TextFrameSourceChannel(WebSocketChannel.StreamSourceChannelControl streamSourceChannelControl, StreamSourceChannel channel, WebSocket07Channel wsChannel, int rsv, boolean finalFragment, long payloadSize, final boolean masked, final int mask) {
        super(streamSourceChannelControl, channel, wsChannel, WebSocketFrameType.TEXT, rsv, finalFragment, payloadSize, masked, mask);
    }

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

    @Override
    public long transferTo0(long count, ByteBuffer throughBuffer, StreamSinkChannel target) throws IOException {
        return super.transferTo0(count, throughBuffer, new UTF8StreamSinkChannel(target, checker));
    }

}

@@ -1497,28 +1497,8 @@
 cker
- = new UTF8Checker()
 ;%0A%0A 
@@ -1762,16 +1762,423 @@
 mask) %7B%0A
+        this(streamSourceChannelControl, channel, wsChannel, rsv, finalFragment, payloadSize, masked, mask, true);%0A    %7D%0A%0A    public WebSocket07TextFrameSourceChannel(WebSocketChannel.StreamSourceChannelControl streamSourceChannelControl, StreamSourceChannel channel, WebSocket07Channel wsChannel, int rsv, boolean finalFragment, long payloadSize, final boolean masked, final int mask, boolean checkUtf8) %7B%0A
         
@@ -2304,16 +2304,137 @@
  mask);%0A
+        if (checkUtf8) %7B%0A            checker = new UTF8Checker();%0A        %7D else %7B%0A            checker = null;%0A        %7D%0A
     %7D%0A%0A 
@@ -2537,32 +2537,136 @@
 s IOException %7B%0A
+        if (checker == null) %7B%0A            return super.transferTo0(position, count, target);%0A        %7D%0A
         return s
@@ -2884,105 +2884,1126 @@
     
-return super.transferTo0(count, throughBuffer, new UTF8StreamSinkChannel(target, checker));%0A    %7D%0A
+if (checker == null) %7B%0A            return super.transferTo0(count, throughBuffer, target);%0A        %7D%0A        return super.transferTo0(count, throughBuffer, new UTF8StreamSinkChannel(target, checker));%0A    %7D%0A%0A    @Override%0A    protected int read0(ByteBuffer dst) throws IOException %7B%0A        if (checker == null) %7B%0A            return super.read0(dst);%0A        %7D%0A        int pos = dst.position();%0A        int r = super.read0(dst);%0A        checker.checkUTF8(dst, pos, r);%0A        return r;%0A    %7D%0A%0A    @Override%0A    protected long read0(ByteBuffer%5B%5D dsts) throws IOException %7B%0A        if (checker == null) %7B%0A            return super.read0(dsts);%0A        %7D%0A        return read0(dsts, 0, dsts.length);%0A    %7D%0A%0A    @Override%0A    protected long read0(ByteBuffer%5B%5D dsts, int offset, int length) throws IOException %7B%0A        if (checker == null) %7B%0A            return super.read0(dsts, offset, length);%0A        %7D%0A        long r = 0;%0A        for (int a = offset; a %3C length; a++) %7B%0A            int i = read(dsts%5Ba%5D);%0A            if (i %3C 1) %7B%0A                break;%0A            %7D%0A            r += i;%0A        %7D%0A        return r;%0A    %7D
 %0A%7D%0A

Add back utf8 checking and add a constructor which allows to disable it