<commit_before>/*
 * Copyright (c) 2012-2014 Spotify AB
 *
 * 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.spotify.netty4.handler.codec.zmtp;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

public class ZMTPWriter {

  public interface Output {

    ByteBuf buffer(int size);
  }

  public static class ByteBufAllocatorOutput implements Output {

    private final ByteBufAllocator allocator;

    public ByteBufAllocatorOutput(final ByteBufAllocator allocator) {
      this.allocator = allocator;
    }

    @Override
    public ByteBuf buffer(final int size) {
      return allocator.buffer(size);
    }
  }

  private final int version;

  private ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;

  private int expectedFrames;
  private int expectedSize;
  private ByteBuf buf;

  private int frame;

  public ZMTPWriter(final int version) {
    this.version = version;
  }

  public void alloc(final ByteBufAllocator alloc) {
    this.alloc = alloc;
  }

  public void reset() {
    expectedFrames = 0;
    frame = 0;
    buf = null;
    expectedSize = 0;
  }

  public void expectFrame(final int size) {
    expectedFrames++;
    expectedSize += ZMTPUtils.frameSize(size, version);
  }

  public void expectFrames(final int frames) {
    this.expectedFrames = frames;
  }

  public void expectSize(final int size) {
    this.expectedSize = size;
  }

  public void begin() {
    buf = alloc.buffer(expectedSize);
  }

  public ByteBuf frame(final int size) {
    if (frame >= expectedFrames) {
      throw new IllegalStateException("content");
    }
    final boolean more = (frame + 1 < expectedFrames);
    return frame(size, more);
  }

  private ByteBuf frame(final int size, final boolean more) {
    ZMTPUtils.writeFrameHeader(buf, size, more, version);
    frame++;
    return buf;
  }

  public void end() {
  }

  public ByteBuf finish() {
    return buf;
  }
}<commit_msg>writer: clean up + discard state on finish()<commit_after>/*
 * Copyright (c) 2012-2014 Spotify AB
 *
 * 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.spotify.netty4.handler.codec.zmtp;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

public class ZMTPWriter {

  private final int version;

  private ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;

  private int expectedFrames;
  private int expectedSize;
  private ByteBuf buf;

  private int frame;

  public ZMTPWriter(final int version) {
    this.version = version;
  }

  public void alloc(final ByteBufAllocator alloc) {
    this.alloc = alloc;
  }

  public void reset() {
    expectedFrames = 0;
    expectedSize = 0;
    buf = null;
    frame = 0;
  }

  public void expectFrame(final int size) {
    expectedFrames++;
    expectedSize += ZMTPUtils.frameSize(size, version);
  }

  public void expectFrames(final int frames) {
    this.expectedFrames = frames;
  }

  public void expectSize(final int size) {
    this.expectedSize = size;
  }

  public void begin() {
    buf = alloc.buffer(expectedSize);
  }

  public ByteBuf frame(final int size) {
    if (frame >= expectedFrames) {
      throw new IllegalStateException("content");
    }
    final boolean more = (frame + 1 < expectedFrames);
    return frame(size, more);
  }

  private ByteBuf frame(final int size, final boolean more) {
    ZMTPUtils.writeFrameHeader(buf, size, more, version);
    frame++;
    return buf;
  }

  public void end() {
  }

  public ByteBuf finish() {
    final ByteBuf result = this.buf;
    reset();
    return result;
  }
}
{'fraction_non_alphanumeric': 0.0880806310254163, 'fraction_numerical': 0.007449605609114811, 'mean_word_length': 3.3476190476190477, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7250256', 'n_tokens_mistral': 1377, 'n_tokens_neox': 1300, 'n_words': 457}