<commit_before>/* Copyright (c) 2008, <NAME>

   Permission to use, copy, modify, and/or distribute this software
   for any purpose with or without fee is hereby granted, provided
   that the above copyright notice and this permission notice appear
   in all copies.

   There is NO WARRANTY for this software.  See license.txt for
   details. */

package java.nio.channels;

import java.io.IOException;
import java.net.SocketException;
import java.net.SocketAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;

public class SocketChannel extends SelectableChannel
  implements ReadableByteChannel, WritableByteChannel
{
  public static final int InvalidSocket = -1;

  int socket = InvalidSocket;
  boolean connected = false;

  public static SocketChannel open() {
    return new SocketChannel();
  }

  public SelectableChannel configureBlocking(boolean v) {
    if (v) throw new IllegalArgumentException();
    return this;
  }

  public Socket socket() {
    return new Handle();
  }

  public boolean connect(SocketAddress address) throws Exception {
    InetSocketAddress a;
    try {
      a = (InetSocketAddress) address;
    } catch (ClassCastException e) {
      throw new UnsupportedAddressTypeException();
    }
    socket = doConnect(a.getHostName(), a.getPort());
    return connected;
  }

  public void close() throws IOException {
    if (isOpen()) {
      super.close();
      closeSocket();
    }
  }

  private int doConnect(String host, int port) throws Exception {
    boolean b[] = new boolean[1];
    int s = natDoConnect(host, port, b);
    connected = b[0];
    return s;
  }

  public int read(ByteBuffer b) throws IOException {
    if (! isOpen()) return -1;
    if (b.remaining() == 0) return 0;
    int r = natRead(socket, b.array(), b.arrayOffset() + b.position(), b.remaining());
    if (r > 0) {
      b.position(b.position() + r);
    }
    return r;
  }

  public int write(ByteBuffer b) throws IOException {
    if (! connected) {
      natThrowWriteError(socket);
    }
    if (b.remaining() == 0) return 0;
    int w = natWrite(socket, b.array(), b.arrayOffset() + b.position(), b.remaining());
    if (w > 0) {
      b.position(b.position() + w);
    }
    return w;
  }

  private void closeSocket() {
    natCloseSocket(socket);
  }

  int socketFD() {
    return socket;<commit_msg>avoid passing null arguments to native methods in SocketChannel<commit_after>/* Copyright (c) 2008, Avian Contributors

   Permission to use, copy, modify, and/or distribute this software
   for any purpose with or without fee is hereby granted, provided
   that the above copyright notice and this permission notice appear
   in all copies.

   There is NO WARRANTY for this software.  See license.txt for
   details. */

package java.nio.channels;

import java.io.IOException;
import java.net.SocketException;
import java.net.SocketAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;

public class SocketChannel extends SelectableChannel
  implements ReadableByteChannel, WritableByteChannel
{
  public static final int InvalidSocket = -1;

  int socket = InvalidSocket;
  boolean connected = false;

  public static SocketChannel open() {
    return new SocketChannel();
  }

  public SelectableChannel configureBlocking(boolean v) {
    if (v) throw new IllegalArgumentException();
    return this;
  }

  public Socket socket() {
    return new Handle();
  }

  public boolean connect(SocketAddress address) throws Exception {
    InetSocketAddress a;
    try {
      a = (InetSocketAddress) address;
    } catch (ClassCastException e) {
      throw new UnsupportedAddressTypeException();
    }
    socket = doConnect(a.getHostName(), a.getPort());
    return connected;
  }

  public void close() throws IOException {
    if (isOpen()) {
      super.close();
      closeSocket();
    }
  }

  private int doConnect(String host, int port) throws Exception {
    if (host == null) throw new NullPointerException();

    boolean b[] = new boolean[1];
    int s = natDoConnect(host, port, b);
    connected = b[0];
    return s;
  }

  public int read(ByteBuffer b) throws IOException {
    if (! isOpen()) return -1;
    if (b.remaining() == 0) return 0;

    byte[] array = b.array();
    if (array == null) throw new NullPointerException();

    int r = natRead(socket, array, b.arrayOffset() + b.position(), b.remaining());
    if (r > 0) {
      b.position(b.position() + r);
    }
    return r;
  }

  public int write(ByteBuffer b) throws IOException {
    if (! connected) {
      natThrowWriteError(socket);
    }
    if (b.remaining() == 0) return 0;

    byte[] array = b.array();
    if (array == null) throw new NullPointerException();

    int w = natWrite(socket, array, b.arrayOffset() + b.position(), b.remaining());
    if (w > 0) {
      b.position(b.position() + w);
    }
    return w;
  }

  private void closeSocket() {
    natCloseSocket(socket);
  }

  int socketFD() {
    return socket;
{'fraction_non_alphanumeric': 0.10956452575064625, 'fraction_numerical': 0.005567707297673493, 'mean_word_length': 3.219798657718121, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 8, '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': '3945922', 'n_tokens_mistral': 1514, 'n_tokens_neox': 1458, 'n_words': 482}