package net.sf.scuba.tlv; import java.io.DataOutputStream; import java.io.IOException; import java.io.OutputStream; import o.C15108ghU; import o.C15109ghW; /* loaded from: classes6.dex */ public class TLVOutputStream extends OutputStream { private DataOutputStream c; private C15108ghU d; public TLVOutputStream(OutputStream outputStream) { this.c = outputStream instanceof DataOutputStream ? (DataOutputStream) outputStream : new DataOutputStream(outputStream); this.d = new C15108ghU(); } public final void d(int i) throws IOException { byte[] b = C15109ghW.b(i); if (this.d.e()) { this.c.write(b); } C15108ghU c15108ghU = this.d; C15108ghU.Sts sts = new C15108ghU.Sts(c15108ghU, i); if (!c15108ghU.a.isEmpty()) { C15108ghU.Sts peek = c15108ghU.a.peek(); byte[] b2 = C15109ghW.b(i); peek.e.write(b2, 0, b2.length); } c15108ghU.a.push(sts); c15108ghU.e = false; c15108ghU.d = true; c15108ghU.c = false; } public final void a(int i) throws IOException { byte[] a = C15109ghW.a(i); C15108ghU c15108ghU = this.d; if (i < 0) { StringBuilder sb = new StringBuilder("Cannot set negative length (length = "); sb.append(i); sb.append(")."); throw new IllegalArgumentException(sb.toString()); } C15108ghU.Sts pop = c15108ghU.a.pop(); if (!c15108ghU.a.isEmpty()) { C15108ghU.Sts peek = c15108ghU.a.peek(); byte[] a2 = C15109ghW.a(i); peek.e.write(a2, 0, a2.length); } pop.b = i; pop.c = true; c15108ghU.a.push(pop); c15108ghU.e = false; c15108ghU.d = false; c15108ghU.c = true; if (this.d.e()) { this.c.write(a); } } @Override // java.io.OutputStream public void write(int i) throws IOException { write(new byte[]{(byte) i}, 0, 1); } @Override // java.io.OutputStream public void write(byte[] bArr) throws IOException { write(bArr, 0, bArr.length); } @Override // java.io.OutputStream, java.io.Flushable public void flush() throws IOException { this.c.flush(); } @Override // java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { if (!this.d.e()) { throw new IllegalStateException("Cannot close stream yet, illegal TLV state."); } this.c.close(); } @Override // java.io.OutputStream public void write(byte[] bArr, int i, int i2) throws IOException { if (this.d.e) { throw new IllegalStateException("Cannot write value bytes yet. Need to write a tag first."); } if (this.d.d) { C15108ghU c15108ghU = this.d; c15108ghU.e = false; c15108ghU.d = false; c15108ghU.c = true; } this.d.c(bArr, i, i2); if (this.d.e()) { this.c.write(bArr, i, i2); } } public final void d(byte[] bArr) throws IOException { if (bArr == null) { throw new IllegalArgumentException("Cannot write null."); } if (this.d.e) { throw new IllegalStateException("Cannot write value bytes yet. Need to write a tag first."); } if (this.d.d) { a(bArr.length); write(bArr); } else { write(bArr); this.d.a(bArr.length); } } public final void c() throws IOException { if (this.d.d) { throw new IllegalStateException("Not processing value yet."); } if (this.d.e) { if (this.d.a.isEmpty() || !(!r0.a.peek().c)) { return; } } C15108ghU c15108ghU = this.d; if (c15108ghU.a.isEmpty()) { throw new IllegalStateException("Cannot get value yet."); } byte[] byteArray = c15108ghU.a.peek().e.toByteArray(); int length = byteArray.length; this.d.a(length); if (this.d.e()) { this.c.write(C15109ghW.a(length)); this.c.write(byteArray); } } }