jimu-decompiled/sources/com/squareup/haha/perflib/io/MemoryMappedFileBuffer.java
2025-05-13 19:24:51 +02:00

172 lines
5.8 KiB
Java

package com.squareup.haha.perflib.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/* loaded from: classes.dex */
public class MemoryMappedFileBuffer implements HprofBuffer {
static final /* synthetic */ boolean $assertionsDisabled = false;
private static final int DEFAULT_PADDING = 1024;
private static final int DEFAULT_SIZE = 1073741824;
private final int mBufferSize;
private final ByteBuffer[] mByteBuffers;
private long mCurrentPosition;
private final long mLength;
private final int mPadding;
MemoryMappedFileBuffer(File file, int i, int i2) throws IOException {
this.mBufferSize = i;
this.mPadding = i2;
this.mLength = file.length();
int i3 = ((int) (this.mLength / this.mBufferSize)) + 1;
this.mByteBuffers = new ByteBuffer[i3];
FileInputStream fileInputStream = new FileInputStream(file);
long j = 0;
for (int i4 = 0; i4 < i3; i4++) {
try {
this.mByteBuffers[i4] = fileInputStream.getChannel().map(FileChannel.MapMode.READ_ONLY, j, Math.min(this.mLength - j, this.mBufferSize + this.mPadding));
this.mByteBuffers[i4].order(HprofBuffer.HPROF_BYTE_ORDER);
j += this.mBufferSize;
} finally {
fileInputStream.close();
}
}
this.mCurrentPosition = 0L;
}
private int getIndex() {
return (int) (this.mCurrentPosition / this.mBufferSize);
}
private int getOffset() {
return (int) (this.mCurrentPosition % this.mBufferSize);
}
public void dispose() {
for (int i = 0; i < this.mByteBuffers.length; i++) {
try {
this.mByteBuffers[i].cleaner().clean();
} catch (Exception unused) {
return;
}
}
}
@Override // com.squareup.haha.perflib.io.HprofBuffer
public boolean hasRemaining() {
return this.mCurrentPosition < this.mLength;
}
@Override // com.squareup.haha.perflib.io.HprofBuffer
public long position() {
return this.mCurrentPosition;
}
@Override // com.squareup.haha.perflib.io.HprofBuffer
public void read(byte[] bArr) {
int index = getIndex();
this.mByteBuffers[index].position(getOffset());
if (bArr.length <= this.mByteBuffers[index].remaining()) {
this.mByteBuffers[index].get(bArr, 0, bArr.length);
} else {
int position = this.mBufferSize - this.mByteBuffers[index].position();
this.mByteBuffers[index].get(bArr, 0, position);
int i = index + 1;
this.mByteBuffers[i].position(0);
this.mByteBuffers[i].get(bArr, position, bArr.length - position);
}
this.mCurrentPosition += bArr.length;
}
@Override // com.squareup.haha.perflib.io.HprofBuffer
public byte readByte() {
byte b = this.mByteBuffers[getIndex()].get(getOffset());
this.mCurrentPosition++;
return b;
}
@Override // com.squareup.haha.perflib.io.HprofBuffer
public char readChar() {
char c = this.mByteBuffers[getIndex()].getChar(getOffset());
this.mCurrentPosition += 2;
return c;
}
@Override // com.squareup.haha.perflib.io.HprofBuffer
public double readDouble() {
double d = this.mByteBuffers[getIndex()].getDouble(getOffset());
this.mCurrentPosition += 8;
return d;
}
@Override // com.squareup.haha.perflib.io.HprofBuffer
public float readFloat() {
float f = this.mByteBuffers[getIndex()].getFloat(getOffset());
this.mCurrentPosition += 4;
return f;
}
@Override // com.squareup.haha.perflib.io.HprofBuffer
public int readInt() {
int i = this.mByteBuffers[getIndex()].getInt(getOffset());
this.mCurrentPosition += 4;
return i;
}
@Override // com.squareup.haha.perflib.io.HprofBuffer
public long readLong() {
long j = this.mByteBuffers[getIndex()].getLong(getOffset());
this.mCurrentPosition += 8;
return j;
}
@Override // com.squareup.haha.perflib.io.HprofBuffer
public short readShort() {
short s = this.mByteBuffers[getIndex()].getShort(getOffset());
this.mCurrentPosition += 2;
return s;
}
@Override // com.squareup.haha.perflib.io.HprofBuffer
public void readSubSequence(byte[] bArr, int i, int i2) {
this.mCurrentPosition += i;
int index = getIndex();
this.mByteBuffers[index].position(getOffset());
if (bArr.length <= this.mByteBuffers[index].remaining()) {
this.mByteBuffers[index].get(bArr, 0, bArr.length);
} else {
int position = this.mBufferSize - this.mByteBuffers[index].position();
this.mByteBuffers[index].get(bArr, 0, position);
int min = Math.min(i2 - position, bArr.length - position);
int i3 = ((min + r3) - 1) / this.mBufferSize;
int i4 = position;
for (int i5 = 0; i5 < i3; i5++) {
int min2 = Math.min(min, this.mBufferSize);
int i6 = index + 1 + i5;
this.mByteBuffers[i6].position(0);
this.mByteBuffers[i6].get(bArr, i4, min2);
i4 += min2;
min -= min2;
}
}
this.mCurrentPosition += Math.min(bArr.length, i2);
}
@Override // com.squareup.haha.perflib.io.HprofBuffer
public long remaining() {
return this.mLength - this.mCurrentPosition;
}
@Override // com.squareup.haha.perflib.io.HprofBuffer
public void setPosition(long j) {
this.mCurrentPosition = j;
}
public MemoryMappedFileBuffer(File file) throws IOException {
this(file, DEFAULT_SIZE, 1024);
}
}