package org.jmrtd.io; import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; /* loaded from: classes6.dex */ public class FragmentBuffer implements Serializable { private static final long serialVersionUID = -3510872461790499721L; private byte[] buffer; private Collection fragments; public FragmentBuffer() { this(1024); } public FragmentBuffer(int i) { this.buffer = new byte[i]; this.fragments = new HashSet(); } public void updateFrom(FragmentBuffer fragmentBuffer) { synchronized (this) { for (Fragment fragment : fragmentBuffer.fragments) { addFragment(fragment.offset, fragmentBuffer.buffer, fragment.offset, fragment.length); } } } public void addFragment(int i, byte b) { synchronized (this) { addFragment(i, new byte[]{b}); } } public void addFragment(int i, byte[] bArr) { synchronized (this) { addFragment(i, bArr, 0, bArr.length); } } public void addFragment(int i, byte[] bArr, int i2, int i3) { synchronized (this) { int i4 = i + i3; byte[] bArr2 = this.buffer; if (i4 > bArr2.length) { setLength(Math.max(i4, bArr2.length) << 1); } System.arraycopy(bArr, i2, this.buffer, i, i3); for (Fragment fragment : new ArrayList(this.fragments)) { if (fragment.getOffset() <= i && i + i3 <= fragment.getOffset() + fragment.getLength()) { return; } if (fragment.getOffset() <= i && i <= fragment.getOffset() + fragment.getLength()) { int offset = fragment.getOffset(); int offset2 = fragment.getOffset(); this.fragments.remove(fragment); i3 = (i + i3) - offset; i = offset2; } else if (i <= fragment.getOffset() && fragment.getOffset() + fragment.getLength() <= i + i3) { this.fragments.remove(fragment); } else if (i <= fragment.getOffset() && fragment.getOffset() <= i + i3) { i3 = (fragment.getOffset() + fragment.getLength()) - i; this.fragments.remove(fragment); } } this.fragments.add(Fragment.getInstance(i, i3)); } } public int getPosition() { int i; synchronized (this) { i = 0; for (int i2 = 0; i2 < this.buffer.length; i2++) { if (isCoveredByFragment(i2)) { i = i2 + 1; } } } return i; } public int getBytesBuffered() { int i; synchronized (this) { i = 0; for (int i2 = 0; i2 < this.buffer.length; i2++) { if (isCoveredByFragment(i2)) { i++; } } } return i; } public boolean isCoveredByFragment(int i) { boolean isCoveredByFragment; synchronized (this) { isCoveredByFragment = isCoveredByFragment(i, 1); } return isCoveredByFragment; } public boolean isCoveredByFragment(int i, int i2) { synchronized (this) { for (Fragment fragment : this.fragments) { int offset = fragment.getOffset(); int offset2 = fragment.getOffset(); int length = fragment.getLength(); if (offset <= i && i + i2 <= offset2 + length) { return true; } } return false; } } public int getBufferedLength(int i) { int i2; synchronized (this) { int i3 = 0; if (i >= this.buffer.length) { return 0; } for (Fragment fragment : this.fragments) { int offset = fragment.getOffset(); int offset2 = fragment.getOffset() + fragment.getLength(); if (offset <= i && i < offset2 && (i2 = offset2 - i) > i3) { i3 = i2; } } return i3; } } public int getLength() { return this.buffer.length; } public Fragment getSmallestUnbufferedFragment(int i, int i2) { Fragment fragment; synchronized (this) { Iterator it = this.fragments.iterator(); int i3 = i; while (true) { if (!it.hasNext()) { break; } Fragment next = it.next(); if (next.getOffset() <= i3 && i3 + i2 <= next.getOffset() + next.getLength()) { i2 = 0; break; } if (next.getOffset() <= i3 && i3 < next.getOffset() + next.getLength()) { int offset = next.getOffset() + next.getLength(); i2 = (i3 + i2) - offset; i3 = offset; } else if (i3 > next.getOffset() || next.getOffset() + next.getLength() > i3 + i2) { if (i <= next.getOffset() && next.getOffset() < i3 + i2) { i2 = next.getOffset() - i3; } } } fragment = Fragment.getInstance(i3, i2); } return fragment; } public String toString() { String obj; synchronized (this) { StringBuilder sb = new StringBuilder("FragmentBuffer ["); sb.append(this.buffer.length); sb.append(", "); sb.append(this.fragments); sb.append("]"); obj = sb.toString(); } return obj; } public boolean equals(Object obj) { synchronized (this) { boolean z = false; if (obj == null) { return false; } if (obj == this) { return true; } if (!obj.getClass().equals(FragmentBuffer.class)) { return false; } FragmentBuffer fragmentBuffer = (FragmentBuffer) obj; byte[] bArr = fragmentBuffer.buffer; if (bArr == null && this.buffer != null) { return false; } if (bArr != null && this.buffer == null) { return false; } Collection collection = fragmentBuffer.fragments; if (collection == null && this.fragments != null) { return false; } if (collection != null && this.fragments == null) { return false; } if (Arrays.equals(bArr, this.buffer)) { if (fragmentBuffer.fragments.equals(this.fragments)) { z = true; } } return z; } } public int hashCode() { return (Arrays.hashCode(this.buffer) * 3) + (this.fragments.hashCode() << 1) + 7; } private void setLength(int i) { synchronized (this) { byte[] bArr = this.buffer; if (i <= bArr.length) { return; } byte[] bArr2 = new byte[i]; System.arraycopy(bArr, 0, bArr2, 0, bArr.length); this.buffer = bArr2; } } /* loaded from: classes6.dex */ public static class Fragment implements Serializable { private static final long serialVersionUID = -3795931618553980328L; private int length; private int offset; private Fragment(int i, int i2) { this.offset = i; this.length = i2; } public static Fragment getInstance(int i, int i2) { return new Fragment(i, i2); } public String toString() { StringBuilder sb = new StringBuilder("["); sb.append(this.offset); sb.append(" .. "); sb.append((this.offset + this.length) - 1); sb.append(" ("); sb.append(this.length); sb.append(")]"); return sb.toString(); } public boolean equals(Object obj) { if (obj == null) { return false; } if (obj == this) { return true; } if (!obj.getClass().equals(Fragment.class)) { return false; } Fragment fragment = (Fragment) obj; return fragment.offset == this.offset && fragment.length == this.length; } public int hashCode() { return (this.offset << 1) + (this.length * 3) + 5; } public int getOffset() { return this.offset; } public int getLength() { return this.length; } } public Collection getFragments() { return this.fragments; } public byte[] getBuffer() { return this.buffer; } }