package com.l7tech.common.mime;

import com.l7tech.util.BufferPool;
import com.l7tech.util.ConfigFactory;
import com.l7tech.util.PoolByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;

public class HybridStashManager implements StashManager {
   private static final int a = 8192;
   private static final String b;
   private static final int c;
   private final int d;
   private final int e;
   private final File f;
   private final String g;
   private final ByteArrayStashManager h;
   private FileStashManager i;
   private long j;
   private static final String[] k;

   public HybridStashManager(int param1, File param2, String param3) {
      // $VF: Couldn't be decompiled
      // Please report this to the Vineflower issue tracker, at https://github.com/Vineflower/vineflower/issues with a copy of the class file (if you have the rights to distribute it!)
      // java.lang.RuntimeException: parsing failure!
      //   at org.jetbrains.java.decompiler.modules.decompiler.decompose.DomHelper.parseGraph(DomHelper.java:211)
      //   at org.jetbrains.java.decompiler.main.rels.MethodProcessor.codeToJava(MethodProcessor.java:174)
      //
      // Bytecode:
      // 00: aload 0
      // 01: invokespecial java/lang/Object.<init> ()V
      // 04: aload 0
      // 05: new com/l7tech/common/mime/ByteArrayStashManager
      // 08: dup
      // 09: invokespecial com/l7tech/common/mime/ByteArrayStashManager.<init> ()V
      // 0c: putfield com/l7tech/common/mime/HybridStashManager.h Lcom/l7tech/common/mime/ByteArrayStashManager;
      // 0f: aload 0
      // 10: aconst_null
      // 11: putfield com/l7tech/common/mime/HybridStashManager.i Lcom/l7tech/common/mime/FileStashManager;
      // 14: aload 0
      // 15: lconst_0
      // 16: putfield com/l7tech/common/mime/HybridStashManager.j J
      // 19: iload 1
      // 1a: bipush 1
      // 1b: if_icmpge 2f
      // 1e: new java/lang/IllegalArgumentException
      // 21: dup
      // 22: getstatic com/l7tech/common/mime/HybridStashManager.k [Ljava/lang/String;
      // 25: astore 4
      // 27: aload 4
      // 29: bipush 2
      // 2a: aaload
      // 2b: invokespecial java/lang/IllegalArgumentException.<init> (Ljava/lang/String;)V
      // 2e: athrow
      // 2f: aload 2
      // 30: ifnonnull 44
      // 33: new java/lang/NullPointerException
      // 36: dup
      // 37: getstatic com/l7tech/common/mime/HybridStashManager.k [Ljava/lang/String;
      // 3a: bipush 4
      // 3b: aaload
      // 3c: invokespecial java/lang/NullPointerException.<init> (Ljava/lang/String;)V
      // 3f: athrow
      // 40: invokestatic com/l7tech/common/mime/HybridStashManager.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 43: athrow
      // 44: aload 3
      // 45: ifnull 57
      // 48: aload 3
      // 49: invokevirtual java/lang/String.length ()I
      // 4c: bipush 1
      // 4d: if_icmpge 68
      // 50: goto 57
      // 53: invokestatic com/l7tech/common/mime/HybridStashManager.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 56: athrow
      // 57: new java/lang/IllegalArgumentException
      // 5a: dup
      // 5b: getstatic com/l7tech/common/mime/HybridStashManager.k [Ljava/lang/String;
      // 5e: bipush 1
      // 5f: aaload
      // 60: invokespecial java/lang/IllegalArgumentException.<init> (Ljava/lang/String;)V
      // 63: athrow
      // 64: invokestatic com/l7tech/common/mime/HybridStashManager.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 67: athrow
      // 68: aload 0
      // 69: iload 1
      // 6a: putfield com/l7tech/common/mime/HybridStashManager.d I
      // 6d: aload 0
      // 6e: aload 2
      // 6f: putfield com/l7tech/common/mime/HybridStashManager.f Ljava/io/File;
      // 72: aload 0
      // 73: aload 3
      // 74: putfield com/l7tech/common/mime/HybridStashManager.g Ljava/lang/String;
      // 77: iload 1
      // 78: sipush 512
      // 7b: if_icmpge 8a
      // 7e: aload 0
      // 7f: iload 1
      // 80: putfield com/l7tech/common/mime/HybridStashManager.e I
      // 83: goto a5
      // 86: invokestatic com/l7tech/common/mime/HybridStashManager.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 89: athrow
      // 8a: iload 1
      // 8b: ldc 32768
      // 8d: if_icmple 9e
      // 90: aload 0
      // 91: getstatic com/l7tech/common/mime/HybridStashManager.c I
      // 94: putfield com/l7tech/common/mime/HybridStashManager.e I
      // 97: goto a5
      // 9a: invokestatic com/l7tech/common/mime/HybridStashManager.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 9d: athrow
      // 9e: aload 0
      // 9f: iload 1
      // a0: bipush 4
      // a1: idiv
      // a2: putfield com/l7tech/common/mime/HybridStashManager.e I
      // a5: return
      // try (25 -> 34): 34 java/lang/IllegalArgumentException
      // try (36 -> 42): 43 java/lang/IllegalArgumentException
      // try (38 -> 52): 52 java/lang/IllegalArgumentException
      // try (54 -> 70): 70 java/lang/IllegalArgumentException
      // try (72 -> 79): 79 java/lang/IllegalArgumentException
   }

   FileStashManager a() throws IOException {
      try {
         if (this.i == null) {
            this.i = new FileStashManager(this.f, this.g);
         }
      } catch (IOException var1) {
         throw a(var1);
      }

      return this.i;
   }

   @Override
   public void stash(int var1, InputStream var2) throws IOException {
      this.unstash(var1);
      if (this.j > this.d) {
         this.a().stash(var1, var2);
         long var15 = this.a().getSize(var1);

         try {
            if (var15 < 0L) {
               throw new IOException(k[3]);
            }
         } catch (IOException var13) {
            throw a(var13);
         }

         this.j += var15;
      } else {
         PoolByteArrayOutputStream var3 = new PoolByteArrayOutputStream(this.e);
         byte[] var4 = null;

         try {
            var4 = BufferPool.getBuffer(16384);
            long var6 = this.j;

            do {
               int var5;
               if ((var5 = var2.read(var4)) <= 0) {
                  this.j = var6;
                  int var8 = var3.size();
                  byte[] var9 = var3.detachPooledByteArray();
                  this.h.stash(var1, var9, 0, var8);
                  return;
               }

               var3.write(var4, 0, var5);
               var6 += var5;
            } while (var6 <= this.d);

            this.a().stash(var1, new SequenceInputStream(new ByteArrayInputStream(var3.getPooledByteArray(), 0, var3.size()), var2));
            this.j = this.j + this.a().getSize(var1);
         } finally {
            var3.close();
            BufferPool.returnBuffer(var4);
         }
      }
   }

   @Override
   public void stash(int var1, byte[] var2) throws IOException {
      this.stash(var1, var2, 0, var2.length);
   }

   @Override
   public void stash(int var1, byte[] var2, int var3, int var4) throws IOException {
      try {
         this.unstash(var1);
         this.j += var4;
         if (this.j > this.d) {
            this.a().stash(var1, var2, var3, var4);
            return;
         }
      } catch (IOException var5) {
         throw a(var5);
      }

      this.h.stash(var1, var2, var3, var4);
   }

   @Override
   public void unstash(int var1) {
      if (this.h.peek(var1)) {
         long var2 = this.h.getSize(var1);

         try {
            if (var2 > 0L) {
               this.j -= var2;
            }
         } catch (IllegalArgumentException var6) {
            throw a(var6);
         }
      } else {
         label47: {
            try {
               if (this.i == null || !this.i.peek(var1)) {
                  break label47;
               }
            } catch (IllegalArgumentException var7) {
               throw a(var7);
            }

            long var8 = this.i.getSize(var1);

            try {
               if (var8 > 0L) {
                  this.j -= var8;
               }
            } catch (IllegalArgumentException var5) {
               throw a(var5);
            }
         }
      }

      try {
         this.h.unstash(var1);
         if (this.i != null) {
            this.i.unstash(var1);
         }
      } catch (IllegalArgumentException var4) {
         throw a(var4);
      }
   }

   @Override
   public long getSize(int var1) {
      long var2 = this.h.getSize(var1);

      try {
         if (var2 != -1L) {
            return var2;
         }
      } catch (IllegalArgumentException var5) {
         throw a(var5);
      }

      try {
         return this.i != null ? this.i.getSize(var1) : -1L;
      } catch (IllegalArgumentException var4) {
         throw a(var4);
      }
   }

   long b() {
      return this.j;
   }

   ByteArrayStashManager c() {
      return this.h;
   }

   @Override
   public InputStream recall(int var1) throws IOException, NoSuchPartException {
      InputStream var2 = null;

      try {
         var2 = this.h.recall(var1);
      } catch (NoSuchPartException var4) {
      }

      try {
         if (var2 != null || this.i == null) {
            return var2;
         }
      } catch (NoSuchPartException var5) {
         throw a(var5);
      }

      return this.i.recall(var1);
   }

   @Override
   public boolean isByteArrayAvailable(int var1) {
      return this.h.isByteArrayAvailable(var1);
   }

   @Override
   public byte[] recallBytes(int var1) throws NoSuchPartException {
      return this.h.recallBytes(var1);
   }

   @Override
   public boolean peek(int param1) {
      // $VF: Couldn't be decompiled
      // Please report this to the Vineflower issue tracker, at https://github.com/Vineflower/vineflower/issues with a copy of the class file (if you have the rights to distribute it!)
      // java.lang.RuntimeException: parsing failure!
      //   at org.jetbrains.java.decompiler.modules.decompiler.decompose.DomHelper.parseGraph(DomHelper.java:211)
      //   at org.jetbrains.java.decompiler.main.rels.MethodProcessor.codeToJava(MethodProcessor.java:174)
      //
      // Bytecode:
      // 00: aload 0
      // 01: getfield com/l7tech/common/mime/HybridStashManager.h Lcom/l7tech/common/mime/ByteArrayStashManager;
      // 04: iload 1
      // 05: invokevirtual com/l7tech/common/mime/ByteArrayStashManager.peek (I)Z
      // 08: ifne 2b
      // 0b: aload 0
      // 0c: getfield com/l7tech/common/mime/HybridStashManager.i Lcom/l7tech/common/mime/FileStashManager;
      // 0f: ifnull 33
      // 12: goto 19
      // 15: invokestatic com/l7tech/common/mime/HybridStashManager.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 18: athrow
      // 19: aload 0
      // 1a: getfield com/l7tech/common/mime/HybridStashManager.i Lcom/l7tech/common/mime/FileStashManager;
      // 1d: iload 1
      // 1e: invokevirtual com/l7tech/common/mime/FileStashManager.peek (I)Z
      // 21: ifeq 33
      // 24: goto 2b
      // 27: invokestatic com/l7tech/common/mime/HybridStashManager.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 2a: athrow
      // 2b: bipush 1
      // 2c: goto 34
      // 2f: invokestatic com/l7tech/common/mime/HybridStashManager.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 32: athrow
      // 33: bipush 0
      // 34: ireturn
      // try (0 -> 8): 9 java/lang/IllegalArgumentException
      // try (5 -> 16): 17 java/lang/IllegalArgumentException
      // try (11 -> 21): 21 java/lang/IllegalArgumentException
   }

   @Override
   public int getMaxOrdinal() {
      try {
         if (this.i == null) {
            return this.h.getMaxOrdinal();
         }
      } catch (IllegalArgumentException var1) {
         throw a(var1);
      }

      return Math.max(this.h.getMaxOrdinal(), this.i.getMaxOrdinal());
   }

   @Override
   public void close() {
      try {
         this.h.close();
         if (this.i != null) {
            this.i.close();
            this.i = null;
         }
      } catch (IllegalArgumentException var1) {
         throw a(var1);
      }

      this.j = 0L;
   }

   static {
      String[] var5 = new String[5];
      int var3 = 0;
      String var2 = "2\u001dv;(A4h\u0019v/#Z;z\u0015e'i\u001e~2\u0014J}z\u0019{&\u000fN0yPg1\u0004I4dPz*\u0012\\4r\u00177,\u0013\u000f8q\u0000c:\u0016p\u0019z*\u0015\u000f0i\u0003cc\u0003J}l\u001fd*\u0015F+y";
      int var4 = "2\u001dv;(A4h\u0019v/#Z;z\u0015e'i\u001e~2\u0014J}z\u0019{&\u000fN0yPg1\u0004I4dPz*\u0012\\4r\u00177,\u0013\u000f8q\u0000c:\u0016p\u0019z*\u0015\u000f0i\u0003cc\u0003J}l\u001fd*\u0015F+y"
         .length();
      char var1 = 17;
      int var7 = -1;

      label24:
      while (true) {
         byte var10000 = 101;
         String var10001 = var2.substring(++var7, var7 + var1);
         byte var10002 = -1;

         while (true) {
            String var8 = a(var10000, a(var10001));
            switch (var10002) {
               case 0:
                  var5[var3++] = var8;
                  if ((var7 += var1) >= var4) {
                     k = var5;
                     b = HybridStashManager.class.getName() + k[0];
                     c = ConfigFactory.getIntProperty(b, 8192);
                     return;
                  }

                  var1 = var2.charAt(var7);
                  break;
               default:
                  var5[var3++] = var8;
                  if ((var7 += var1) < var4) {
                     var1 = var2.charAt(var7);
                     continue label24;
                  }

                  var2 = "\b_7`L\u000b<)^vfE\u001ay/\\?lENo4K3\"O\b<;X:g\u0000\u001at<EvuA\u001d<7D%v\u0000\u0019n4E\"gN\u001a9X$gC\u001as/HvoU\u001dh}S3\"S\u001bl-]?gD";
                  var4 = "\b_7`L\u000b<)^vfE\u001ay/\\?lENo4K3\"O\b<;X:g\u0000\u001at<EvuA\u001d<7D%v\u0000\u0019n4E\"gN\u001a9X$gC\u001as/HvoU\u001dh}S3\"S\u001bl-]?gD"
                     .length();
                  var1 = '6';
                  var7 = -1;
            }

            var10000 = 36;
            var10001 = var2.substring(++var7, var7 + var1);
            var10002 = 0;
         }
      }
   }

   private static Exception a(Exception var0) {
      return var0;
   }

   private static char[] a(String var0) {
      char[] var10000 = var0.toCharArray();
      if (var10000.length < 2) {
         var10000[0] = (char)(var10000[0] ^ 56);
      }

      return var10000;
   }

   private static String a(int var0, char[] var1) {
      int var10003 = var1.length;
      int var10002 = var0;
      char[] var10001 = var1;
      int var10000 = var10003;

      for (int var2 = 0; var10000 > var2; var2++) {
         var10001[var2] = (char)(var10001[var2] ^ var10002 ^ switch (var2 % 7) {
            case 0 -> 121;
            case 1 -> 21;
            case 2 -> 114;
            case 3 -> 38;
            case 4 -> 4;
            case 5 -> 74;
            default -> 56;
         });
      }

      return new String(var10001).intern();
   }
}
