<commit_before>/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * 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 com.facebook.thrift.transport;

import com.facebook.thrift.TApplicationException;
import com.facebook.thrift.TException;
import com.facebook.thrift.protocol.TBinaryProtocol;
import com.facebook.thrift.protocol.TCompactProtocol;
import com.facebook.thrift.protocol.TMessage;
import com.facebook.thrift.protocol.TMessageType;
import com.facebook.thrift.utils.StandardCharsets;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

public class THeaderTransport extends TFramedTransport {
  public static final int HEADER_MAGIC_MASK = 0xFFFF0000;
  public static final int HEADER_FLAGS_MASK = 0x0000FFFF;

  // 16th and 32nd bits must be 0 to differentiate framed vs unframed.
  public static final int HEADER_MAGIC = 0x0FFF0000;

  // HTTP has different magic
  public static final int HTTP_SERVER_MAGIC = 0x504F5354; // 'POST'

  // Note max frame size is slightly less than HTTP_SERVER_MAGIC
  public static final int MAX_FRAME_SIZE = 0x3FFFFFFF;

  private int zlibBufferSize = 512;

  // Transforms
  public enum Transforms {
    ZLIB_TRANSFORM(0x01),
    HMAC_TRANSFORM(0x02), // Deprecated and no longer supported
    SNAPPY_TRANSFORM(0x03); // Deprecated and no longer supported

    private int value;

    private Transforms(int value) {
      this.value = value;
    }

    public int getValue() {
      return value;
    }
  }

  // Infos
  public enum Infos {
    INFO_KEYVALUE(0x01),
    INFO_PKEYVALUE(0x02);

    private int value;

    private Infos(int value) {
      this.value = value;
    }

    public int getValue() {
      return value;
    }
  }

  // Client types
  public enum ClientTypes {
    HEADERS(0),
    FRAMED_DEPRECATED(1),
    HTTP(3),
    UNKNOWN(4);

    private int value;

    private ClientTypes(int value) {
      this.value = value;
    }

    public int getValue() {
      return value;
    }
  }

  public static final int T_BINARY_PROTOCOL = 0;
  public static final int T_JSON_PROTOCOL = 1;
  public static final int T_COMPACT_PROTOCOL = 2;

  private static final int numClientTypes = 4;

  private int protoId = T_COMPACT_PROTOCOL; // default
  private ClientTypes clientType = ClientTypes.HEADERS;
  private int seqId = 0;
  private int flags = 0;

  private final boolean[] supportedClients;

  private final List<Transforms> writeTransforms;
  private List<Integer> readTransforms;

  private final HashMap<String, String> readHeaders;
  private final HashMap<String, String> readPersistentHeaders;
  private final HashMap<String, String> writeHeaders;
  private final HashMap<String, String> writePersistentHeaders;

  private static final String IDENTITY_HEADER = "identity";
  private static final String ID_VERSION_HEADER = "id_version";
  private static final String ID_VERSION = "1";

  private String identity;

  public THeaderTransport(TTransport transport) {
    super(transport);
    writeTransforms = new ArrayList<Transforms>();

    // Always supported headers
    supportedClients = new boolean[numClientTypes];
    supportedClients[ClientTypes.HEADERS.getValue()] = true;
    writeHeaders = new HashMap<String, String>();
    writePersistentHeaders = new HashMap<String, String>();
    readHeaders = new HashMap<String, String>();
    readPersistentHeaders = new HashMap<String, String>();
  }

  public THeaderTransport(TTransport transport, List<ClientTypes> clientTypes) {
    this(transport);

    if (clientTypes != null) {
      for (ClientTypes t : clientTypes) {
        supportedClients[t.getValue()] = true;
      }
    }
  }

  public TTransport getUnderlyingTransport() {
    return this.transport_;
  }

  /**
   * Returns the protocol Id we are reading/writing
   *
   * @return protocol id
   */
  public int getProtocolId() {
    if (clientType == ClientTypes.HEADERS) {
      return protoId;
    } else {
      return 0; // Default to binary for all others.
    }
  }

  /** Sets protocol Id we are writing May be updated on read. */
  public void setProtocolId(int protoId) {
    this.protoId = protoId;
  }

  /**
   * Sets the internal buffer size for zlib transform This will work with any value (except 0), but
   * this is provided as an optimization knob.
   *
   * @param sz Block size for decompress
   */
  public void setZlibBufferSize(int sz) {
    zlibBufferSize = sz;
  }

  /** Add a transform to the write transforms list */
  public void addTransform(Transforms transform) {
    writeTransforms.add(transform);<commit_msg>Java: add setClientType() in THeaderTransport (#385)<commit_after>/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * 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 com.facebook.thrift.transport;

import com.facebook.thrift.TApplicationException;
import com.facebook.thrift.TException;
import com.facebook.thrift.protocol.TBinaryProtocol;
import com.facebook.thrift.protocol.TCompactProtocol;
import com.facebook.thrift.protocol.TMessage;
import com.facebook.thrift.protocol.TMessageType;
import com.facebook.thrift.utils.StandardCharsets;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

public class THeaderTransport extends TFramedTransport {
  public static final int HEADER_MAGIC_MASK = 0xFFFF0000;
  public static final int HEADER_FLAGS_MASK = 0x0000FFFF;

  // 16th and 32nd bits must be 0 to differentiate framed vs unframed.
  public static final int HEADER_MAGIC = 0x0FFF0000;

  // HTTP has different magic
  public static final int HTTP_SERVER_MAGIC = 0x504F5354; // 'POST'

  // Note max frame size is slightly less than HTTP_SERVER_MAGIC
  public static final int MAX_FRAME_SIZE = 0x3FFFFFFF;

  private int zlibBufferSize = 512;

  // Transforms
  public enum Transforms {
    ZLIB_TRANSFORM(0x01),
    HMAC_TRANSFORM(0x02), // Deprecated and no longer supported
    SNAPPY_TRANSFORM(0x03); // Deprecated and no longer supported

    private int value;

    private Transforms(int value) {
      this.value = value;
    }

    public int getValue() {
      return value;
    }
  }

  // Infos
  public enum Infos {
    INFO_KEYVALUE(0x01),
    INFO_PKEYVALUE(0x02);

    private int value;

    private Infos(int value) {
      this.value = value;
    }

    public int getValue() {
      return value;
    }
  }

  // Client types
  public enum ClientTypes {
    HEADERS(0),
    FRAMED_DEPRECATED(1),
    HTTP(3),
    UNKNOWN(4);

    private int value;

    private ClientTypes(int value) {
      this.value = value;
    }

    public int getValue() {
      return value;
    }
  }

  public static final int T_BINARY_PROTOCOL = 0;
  public static final int T_JSON_PROTOCOL = 1;
  public static final int T_COMPACT_PROTOCOL = 2;

  private static final int numClientTypes = 4;

  private int protoId = T_COMPACT_PROTOCOL; // default
  private ClientTypes clientType = ClientTypes.HEADERS;
  private int seqId = 0;
  private int flags = 0;

  private final boolean[] supportedClients;

  private final List<Transforms> writeTransforms;
  private List<Integer> readTransforms;

  private final HashMap<String, String> readHeaders;
  private final HashMap<String, String> readPersistentHeaders;
  private final HashMap<String, String> writeHeaders;
  private final HashMap<String, String> writePersistentHeaders;

  private static final String IDENTITY_HEADER = "identity";
  private static final String ID_VERSION_HEADER = "id_version";
  private static final String ID_VERSION = "1";

  private String identity;

  public THeaderTransport(TTransport transport) {
    super(transport);
    writeTransforms = new ArrayList<Transforms>();

    // Always supported headers
    supportedClients = new boolean[numClientTypes];
    supportedClients[ClientTypes.HEADERS.getValue()] = true;
    writeHeaders = new HashMap<String, String>();
    writePersistentHeaders = new HashMap<String, String>();
    readHeaders = new HashMap<String, String>();
    readPersistentHeaders = new HashMap<String, String>();
  }

  public THeaderTransport(TTransport transport, List<ClientTypes> clientTypes) {
    this(transport);

    if (clientTypes != null) {
      for (ClientTypes t : clientTypes) {
        supportedClients[t.getValue()] = true;
      }
    }
  }

  public TTransport getUnderlyingTransport() {
    return this.transport_;
  }

  /**
   * Returns the protocol Id we are reading/writing
   *
   * @return protocol id
   */
  public int getProtocolId() {
    if (clientType == ClientTypes.HEADERS) {
      return protoId;
    } else {
      return 0; // Default to binary for all others.
    }
  }

  /** Sets protocol Id we are writing May be updated on read. */
  public void setProtocolId(int protoId) {
    this.protoId = protoId;
  }

  /**
   * Returns the current client type we are reading/writing
   *
   * @return client type
   */
  public ClientTypes getClientType() {
    return this.clientType;
  }

  /** Sets client type we are writing */
  public void setClientType(ClientTypes clientType) {
    this.clientType = clientType;
  }

  /**
   * Sets the internal buffer size for zlib transform This will work with any value (except 0), but
   * this is provided as an optimization knob.
   *
   * @param sz Block size for decompress
   */
  public void setZlibBufferSize(int sz) {
    zlibBufferSize = sz;
  }

  /** Add a transform to the write transforms list */
  public void addTransform(Transforms transform) {
    writeTransforms.add(transform);
{'fraction_non_alphanumeric': 0.08403361344537816, 'fraction_numerical': 0.012466525071567088, 'mean_word_length': 3.5561632309633993, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4827259', 'n_tokens_mistral': 3230, 'n_tokens_neox': 3097, 'n_words': 1014}