package com.splunk.mint.network.io;

import com.splunk.mint.network.Counter;
import com.splunk.mint.network.MonitorRegistry;
import com.splunk.mint.network.NetLogManager;
import com.splunk.mint.network.socket.MonitoringSocketImpl;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public final class InputStreamMonitorKitKat extends InputStream
{
  private static final String IN_POSTFIX = "-bytes-in";
  private static final int MAX_POSSIBLE_HEADER_LENGTH = 50;
  private StringBuffer body;
  private List<Byte> chars;
  boolean contentLengthFound = false;
  private final Counter counter;
  public String exception = null;
  private boolean finishedReadingHeaders = false;
  HashMap<String, List<String>> headers = new HashMap(2);
  MonitoringSocketImpl monSocIm;
  private final InputStream original;
  boolean statusCodeFound = false;

  public InputStreamMonitorKitKat(String paramString, MonitorRegistry paramMonitorRegistry, InputStream paramInputStream, MonitoringSocketImpl paramMonitoringSocketImpl)
  {
    this.original = paramInputStream;
    this.counter = new Counter(paramString + "-bytes-in");
    this.chars = new ArrayList();
    this.body = new StringBuffer();
    this.monSocIm = paramMonitoringSocketImpl;
    this.finishedReadingHeaders = false;
    paramMonitorRegistry.add(this.counter);
  }

  private void updateBody()
  {
    byte[] arrayOfByte = new byte[this.chars.size()];
    for (int i = 0; i < arrayOfByte.length; i++)
      arrayOfByte[i] = ((Byte)this.chars.get(i)).byteValue();
    this.chars.clear();
    this.body.append(new String(arrayOfByte));
    if (this.body.toString().contains("\r\n\r\n"))
    {
      this.finishedReadingHeaders = true;
      tryToReadHeaders();
    }
  }

  public void close()
    throws IOException
  {
    this.original.close();
  }

  public HashMap<String, List<String>> getHeaders()
  {
    return this.headers;
  }

  public int read()
    throws IOException
  {
    try
    {
      int i = this.original.read();
      if (i > -1)
        this.counter.inc();
      if (!this.finishedReadingHeaders)
      {
        this.chars.add(Byte.valueOf((byte)i));
        updateBody();
      }
      return i;
    }
    catch (IOException localIOException)
    {
      this.exception = NetLogManager.exceptionToString(localIOException);
      throw localIOException;
    }
  }

  public int read(byte[] paramArrayOfByte)
    throws IOException
  {
    try
    {
      int i = this.original.read(paramArrayOfByte);
      if (i > -1)
        this.counter.inc(i);
      if (!this.finishedReadingHeaders)
      {
        for (int j = 0; j < paramArrayOfByte.length; j++)
          this.chars.add(Byte.valueOf(paramArrayOfByte[j]));
        updateBody();
      }
      return i;
    }
    catch (IOException localIOException)
    {
      this.exception = NetLogManager.exceptionToString(localIOException);
      throw localIOException;
    }
  }

  public int read(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
    throws IOException
  {
    try
    {
      int i = this.original.read(paramArrayOfByte, paramInt1, paramInt2);
      if (i > -1)
        this.counter.inc(i);
      if (!this.finishedReadingHeaders)
      {
        for (int j = paramInt1; j < paramInt2; j++)
          this.chars.add(Byte.valueOf(paramArrayOfByte[j]));
        updateBody();
      }
      return i;
    }
    catch (IOException localIOException)
    {
      this.exception = NetLogManager.exceptionToString(localIOException);
      throw localIOException;
    }
  }

  public void tryToReadHeaders()
  {
    BufferedReader localBufferedReader = new BufferedReader(new StringReader(this.body.toString()));
    try
    {
      str1 = localBufferedReader.readLine();
      if (str1 != null)
        if ((!this.statusCodeFound) && (str1.contains("HTTP/")))
        {
          int j = str1.length();
          if (j >= 50);
        }
    }
    catch (IOException localIOException)
    {
      try
      {
        while (true)
        {
          String str1;
          String[] arrayOfString2 = str1.split(" ");
          String[] arrayOfString3 = new String[1];
          arrayOfString3[0] = arrayOfString2[1].trim();
          this.headers.put("splk-statuscode", Arrays.asList(arrayOfString3));
          this.statusCodeFound = true;
          label105: int i;
          String str2;
          if ((!this.contentLengthFound) && (str1.contains(":")) && (str1.length() < 50))
          {
            i = str1.indexOf(":");
            if (i > -1)
            {
              str2 = str1.substring(0, i).trim();
              boolean bool2 = str2.equals("Content-Length");
              if (!bool2);
            }
          }
          try
          {
            String[] arrayOfString1 = new String[1];
            arrayOfString1[0] = str1.substring(i + 1, str1.length()).trim();
            this.headers.put(str2, Arrays.asList(arrayOfString1));
            this.contentLengthFound = true;
            label215: if (!this.statusCodeFound)
              continue;
            boolean bool1 = this.contentLengthFound;
            if (!bool1)
              continue;
            while (true)
            {
              if (this.monSocIm != null)
                this.monSocIm.readingDone();
              return;
              localIOException = localIOException;
              localIOException.printStackTrace();
            }
          }
          catch (ArrayIndexOutOfBoundsException localArrayIndexOutOfBoundsException1)
          {
            break label215;
          }
        }
      }
      catch (ArrayIndexOutOfBoundsException localArrayIndexOutOfBoundsException2)
      {
        break label105;
      }
    }
  }
}

/* Location:           C:\Documents and Settings\Cesar Cabello Cea\Mis documentos\Downloads\apk-downloader\com.ikea.kompis-6_dex2jar.jar
 * Qualified Name:     com.splunk.mint.network.io.InputStreamMonitorKitKat
 * JD-Core Version:    0.6.2
 */