<commit_before>/*
 *
 * Copyright 2011 <NAME> (molino.luca--AT--gmail.com *
 * 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.orientechnologies.orient.server.network.protocol.http.multipart;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * @author luca.molino
 * 
 */
public class OHttpMultipartContentInputStream extends InputStream {

  protected String                        boundary;

  protected OHttpMultipartBaseInputStream wrappedInputStream;

  protected int                           nextByte;

  protected boolean                       internalAvailable = true;

  public OHttpMultipartContentInputStream(OHttpMultipartBaseInputStream in, String iBoundary) throws IOException {
    wrappedInputStream = in;
    boundary = '\n' + "--" + iBoundary;
  }

  public InputStream getWrappedInputStream() {
    return wrappedInputStream;
  }

  @Override
  public synchronized int available() throws IOException {
    if (internalAvailable) {
      return wrappedInputStream.available();
    } else {
      return -1;
    }
  }

  @Override
  public synchronized int read() throws IOException {
    if (!internalAvailable)
      return -1;

    int value = nextByte;
    nextByte = wrappedInputStream.read();
    if (((char) nextByte) == '\r') {
      bufferData();
    }
    return value;
  }

  @Override
  public void close() throws IOException {
  }

  @Override
  public synchronized void mark(int readlimit) {
    wrappedInputStream.mark(readlimit);
  }

  @Override
  public boolean markSupported() {
    return wrappedInputStream.markSupported();
  }

  @Override
  public int read(byte[] b, int off, int len) throws IOException {
    return wrappedInputStream.read(b, off, len);
  }

  @Override
  public int read(byte[] b) throws IOException {
    return wrappedInputStream.read(b);
  }

  @Override
  public synchronized void reset() throws IOException {
    nextByte = wrappedInputStream.read();
    internalAvailable = true;
    if (((char) nextByte) == '\r') {
      bufferData();
    }
  }

  @Override
  public long skip(long n) throws IOException {
    return wrappedInputStream.skip(n);
  }

  @Override
  public String toString() {
    return wrappedInputStream.toString();
  }

  protected void bufferData() throws IOException {
    boolean checkingEnd = true;
    int boundaryCursor = 0;
    final ArrayList<Integer> buffer = new ArrayList<Integer>();
    int b;
    while (checkingEnd && (b = wrappedInputStream.read()) > 0) {
      buffer.add(b);
      if (((char) b) == boundary.charAt(boundaryCursor)) {
        internalAvailable = false;
        boundaryCursor++;
        if (boundaryCursor == boundary.length()) {
          checkingEnd = false;<commit_msg>HTTP upload: fixed bug on management of byte 0<commit_after>/*
 *
 * Copyright 2011 <NAME> (molino.luca--AT--gmail.com *
 * 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.orientechnologies.orient.server.network.protocol.http.multipart;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * @author luca.molino
 * 
 */
public class OHttpMultipartContentInputStream extends InputStream {

  protected String                        boundary;

  protected OHttpMultipartBaseInputStream wrappedInputStream;

  protected int                           nextByte;

  protected boolean                       internalAvailable = true;

  public OHttpMultipartContentInputStream(OHttpMultipartBaseInputStream in, String iBoundary) throws IOException {
    wrappedInputStream = in;
    boundary = '\n' + "--" + iBoundary;
  }

  public InputStream getWrappedInputStream() {
    return wrappedInputStream;
  }

  @Override
  public synchronized int available() throws IOException {
    if (internalAvailable) {
      return wrappedInputStream.available();
    } else {
      return -1;
    }
  }

  @Override
  public synchronized int read() throws IOException {
    if (!internalAvailable)
      return -1;

    int value = nextByte;
    nextByte = wrappedInputStream.read();
    if (((char) nextByte) == '\r') {
      bufferData();
    }
    return value;
  }

  @Override
  public void close() throws IOException {
  }

  @Override
  public synchronized void mark(int readlimit) {
    wrappedInputStream.mark(readlimit);
  }

  @Override
  public boolean markSupported() {
    return wrappedInputStream.markSupported();
  }

  @Override
  public int read(byte[] b, int off, int len) throws IOException {
    return wrappedInputStream.read(b, off, len);
  }

  @Override
  public int read(byte[] b) throws IOException {
    return wrappedInputStream.read(b);
  }

  @Override
  public synchronized void reset() throws IOException {
    nextByte = wrappedInputStream.read();
    internalAvailable = true;
    if (((char) nextByte) == '\r') {
      bufferData();
    }
  }

  @Override
  public long skip(long n) throws IOException {
    return wrappedInputStream.skip(n);
  }

  @Override
  public String toString() {
    return wrappedInputStream.toString();
  }

  protected void bufferData() throws IOException {
    boolean checkingEnd = true;
    int boundaryCursor = 0;
    final ArrayList<Integer> buffer = new ArrayList<Integer>();
    int b;
    while (checkingEnd && (b = wrappedInputStream.read()) > -1) {
      buffer.add(b);
      if (((char) b) == boundary.charAt(boundaryCursor)) {
        internalAvailable = false;
        boundaryCursor++;
        if (boundaryCursor == boundary.length()) {
          checkingEnd = false;
{'fraction_non_alphanumeric': 0.09635974304068523, 'fraction_numerical': 0.0038237993270113183, 'mean_word_length': 3.1491116751269037, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '3409572', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1687, 'n_words': 557}