<commit_before>// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.debug.core.model;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

import org.chromium.sdk.ConnectionLogger;
import org.chromium.sdk.util.ByteToCharConverter;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.model.ITerminate;

/**
 * Connection logger that writes both incoming and outgoing streams into
 * logWriter with simple annotations.
 */
public class ConnectionLoggerImpl implements ConnectionLogger {
  /**
   * Additional interface logger sends its output to.
   */
  public interface LogLifecycleListener {
    /**
     * Notifies about logging start. Before this call {@link ConnectionLoggerImpl}
     * is considered to be simply garbage-collectible. After this call
     * {@link ConnectionLoggerImpl} must call {@link #logClosed()}.
     *
     * @param connectionLogger instance of host {@link ConnectionLoggerImpl}, which is nice
     *        to have because theoretically we may receive this call before constructor of
     *        {@link ConnectionLoggerImpl} returned
     */
    void logStarted(ConnectionLoggerImpl connectionLogger);

    /**
     * Notifies about log stream being closed. Technically, last messages may arrive
     * even after this. It is supposed that log representation may be closed on this call
     * because we are not 100% accurate.
     */
    void logClosed();
  }


  public ConnectionLoggerImpl(Writer logWriter, LogLifecycleListener lifecycleListener) {
    this.logWriter = logWriter;
    this.lifecycleListener = lifecycleListener;
  }

  /**
   * We mix 2 streams into a single console. This type helps to annotate them textually.
   */
  private interface StreamId {
    String getStreamName();
  }

  private static final Charset CHARSET = Charset.forName("UTF-8");

  public LoggableWriter wrapWriter(final LoggableWriter originalLoggableWriter) {
    final StreamId streamId = new StreamId() {
      public String getStreamName() {
        return Messages.ConnectionLoggerImpl_SentToChrome;
      }
    };
    final OutputStream originalOutputStream = originalLoggableWriter.getOutputStream();
    final OutputStream wrappedOutputStream = new OutputStream() {
      private final ByteToCharConverter byteToCharConverter = new ByteToCharConverter(CHARSET);

      @Override
      public void close() throws IOException {
        originalOutputStream.close();
        flushLogWriter();
      }

      @Override
      public void flush() throws IOException {
        originalOutputStream.flush();
        flushLogWriter();
      }

      @Override
      public void write(int b) throws IOException {
        originalOutputStream.write(b);
      }

      @Override
      public void write(byte[] b) throws IOException {
        write(b, 0, b.length);
      }

      @Override
      public void write(byte[] buf, int off, int len) throws IOException {
        originalOutputStream.write(buf, off, len);
        CharBuffer charBuffer = byteToCharConverter.convert(ByteBuffer.wrap(buf, off, len));
        writeToLog(charBuffer, streamId);
      }
    };
    return new LoggableWriter() {
      public OutputStream getOutputStream() {
        return wrappedOutputStream;
      }
      public void markSeparatorForLog() {
        writeToLog(MESSAGE_SEPARATOR, streamId);
        flushLogWriter();
      }
    };
  }

  public LoggableReader wrapReader(final LoggableReader loggableReader) {
    final StreamId streamId = new StreamId() {
      public String getStreamName() {
        return Messages.ConnectionLoggerImpl_ReceivedFromChrome;
      }
    };

    final InputStream originalInputStream = loggableReader.getInputStream();

    final InputStream wrappedInputStream = new InputStream() {
      private final ByteToCharConverter byteToCharConverter = new ByteToCharConverter(CHARSET);
      @Override
      public int read() throws IOException {
        byte[] buffer = new byte[0];
        int res = readImpl(buffer, 0, 1);
        if (res <= 0) {
          return -1;
        } else {
          return buffer[0];
        }
      }

      @Override<commit_msg>Fix stupid bug<commit_after>// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.debug.core.model;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

import org.chromium.sdk.ConnectionLogger;
import org.chromium.sdk.util.ByteToCharConverter;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.model.ITerminate;

/**
 * Connection logger that writes both incoming and outgoing streams into
 * logWriter with simple annotations.
 */
public class ConnectionLoggerImpl implements ConnectionLogger {
  /**
   * Additional interface logger sends its output to.
   */
  public interface LogLifecycleListener {
    /**
     * Notifies about logging start. Before this call {@link ConnectionLoggerImpl}
     * is considered to be simply garbage-collectible. After this call
     * {@link ConnectionLoggerImpl} must call {@link #logClosed()}.
     *
     * @param connectionLogger instance of host {@link ConnectionLoggerImpl}, which is nice
     *        to have because theoretically we may receive this call before constructor of
     *        {@link ConnectionLoggerImpl} returned
     */
    void logStarted(ConnectionLoggerImpl connectionLogger);

    /**
     * Notifies about log stream being closed. Technically, last messages may arrive
     * even after this. It is supposed that log representation may be closed on this call
     * because we are not 100% accurate.
     */
    void logClosed();
  }


  public ConnectionLoggerImpl(Writer logWriter, LogLifecycleListener lifecycleListener) {
    this.logWriter = logWriter;
    this.lifecycleListener = lifecycleListener;
  }

  /**
   * We mix 2 streams into a single console. This type helps to annotate them textually.
   */
  private interface StreamId {
    String getStreamName();
  }

  private static final Charset CHARSET = Charset.forName("UTF-8");

  public LoggableWriter wrapWriter(final LoggableWriter originalLoggableWriter) {
    final StreamId streamId = new StreamId() {
      public String getStreamName() {
        return Messages.ConnectionLoggerImpl_SentToChrome;
      }
    };
    final OutputStream originalOutputStream = originalLoggableWriter.getOutputStream();
    final OutputStream wrappedOutputStream = new OutputStream() {
      private final ByteToCharConverter byteToCharConverter = new ByteToCharConverter(CHARSET);

      @Override
      public void close() throws IOException {
        originalOutputStream.close();
        flushLogWriter();
      }

      @Override
      public void flush() throws IOException {
        originalOutputStream.flush();
        flushLogWriter();
      }

      @Override
      public void write(int b) throws IOException {
        originalOutputStream.write(b);
      }

      @Override
      public void write(byte[] b) throws IOException {
        write(b, 0, b.length);
      }

      @Override
      public void write(byte[] buf, int off, int len) throws IOException {
        originalOutputStream.write(buf, off, len);
        CharBuffer charBuffer = byteToCharConverter.convert(ByteBuffer.wrap(buf, off, len));
        writeToLog(charBuffer, streamId);
      }
    };
    return new LoggableWriter() {
      public OutputStream getOutputStream() {
        return wrappedOutputStream;
      }
      public void markSeparatorForLog() {
        writeToLog(MESSAGE_SEPARATOR, streamId);
        flushLogWriter();
      }
    };
  }

  public LoggableReader wrapReader(final LoggableReader loggableReader) {
    final StreamId streamId = new StreamId() {
      public String getStreamName() {
        return Messages.ConnectionLoggerImpl_ReceivedFromChrome;
      }
    };

    final InputStream originalInputStream = loggableReader.getInputStream();

    final InputStream wrappedInputStream = new InputStream() {
      private final ByteToCharConverter byteToCharConverter = new ByteToCharConverter(CHARSET);
      @Override
      public int read() throws IOException {
        byte[] buffer = new byte[1];
        int res = readImpl(buffer, 0, 1);
        if (res <= 0) {
          return -1;
        } else {
          return buffer[0];
        }
      }

      @Override
{'fraction_non_alphanumeric': 0.08014530593710978, 'fraction_numerical': 0.0036326484277443526, 'mean_word_length': 3.1072261072261074, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '538704', 'n_tokens_mistral': 2299, 'n_tokens_neox': 2228, 'n_words': 713}