package epson.print.Util;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

public class BmpFileInfo {
    static final /* synthetic */ boolean $assertionsDisabled = false;
    private int mBitPerPixel;
    private int mDataOffset;
    private String mFileName;
    private int mFileSize;
    private int mHeaderSize;
    private int mHeight;
    private int mImageDataBytes;
    private RandomAccessFile mOfs;
    private int mWidth;
    private FileChannel mWriteChannel;

    public boolean readParams(String r11) {

        throw new UnsupportedOperationException("Method not decompiled: epson.print.Util.BmpFileInfo.readParams(java.lang.String):boolean");
    }

    public int getFileSize() {
        return mFileSize;
    }

    public int getWidth() {
        return mWidth;
    }

    public int getHeight() {
        return mHeight;
    }

    public int getBitPerPixel() {
        return mBitPerPixel;
    }

    public int transferImageData(WritableByteChannel r11) {
        throw new UnsupportedOperationException("Method not decompiled: epson.print.Util.BmpFileInfo.transferImageData(java.nio.channels.WritableByteChannel):int");
    }

    public boolean transferImageData(BmpFileInfo bmpFileInfo) {
        int transferImageData = transferImageData(bmpFileInfo.getWritableChannelChannel());
        if (transferImageData <= 0) {
            return false;
        }
        bmpFileInfo.addSize(transferImageData, mHeight);
        return true;
    }

    private void writeHeader(FileChannel fileChannel) throws IOException {
        ByteBuffer allocate = ByteBuffer.allocate(54);
        allocate.clear();
        allocate.order(ByteOrder.LITTLE_ENDIAN);
        allocate.put((byte) 66);
        allocate.put((byte) 77);
        allocate.putInt(mFileSize);
        allocate.putShort((short) 0);
        allocate.putShort((short) 0);
        allocate.putInt(mDataOffset);
        allocate.putInt(mHeaderSize);
        allocate.putInt(mWidth);
        allocate.putInt(0);
        allocate.putShort((short) 1);
        allocate.putShort((short) mBitPerPixel);
        allocate.putInt(0);
        allocate.putInt(mImageDataBytes);
        allocate.putInt(0);
        allocate.putInt(0);
        allocate.putInt(0);
        allocate.putInt(0);
        fileChannel.position(0);
        allocate.rewind();
        fileChannel.write(allocate);
    }

    public boolean prepareAppendData() {
        mWriteChannel = null;
        try {
            mOfs = new RandomAccessFile(mFileName, "rw");
            mWriteChannel = mOfs.getChannel();
            writeHeader(mWriteChannel);
            mWriteChannel.position((long) mDataOffset);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            RandomAccessFile randomAccessFile = mOfs;
            if (randomAccessFile != null) {
                try {
                    randomAccessFile.close();
                } catch (IOException unused) {
                }
            }
            FileChannel fileChannel = mWriteChannel;
            if (fileChannel == null) {
                return false;
            }
            try {
                fileChannel.close();
            } catch (IOException unused2) {
            }
            mWriteChannel = null;
            return false;
        }
    }

    public void closeWriteableChannel() {
        FileChannel fileChannel = mWriteChannel;
        if (fileChannel != null) {
            try {
                fileChannel.close();
            } catch (IOException unused) {
            }
            mWriteChannel = null;
        }
        RandomAccessFile randomAccessFile = mOfs;
        if (randomAccessFile != null) {
            try {
                randomAccessFile.close();
            } catch (IOException unused2) {
            }
            mOfs = null;
        }
    }
    protected WritableByteChannel getWritableChannelChannel() {
        return mWriteChannel;
    }

    public boolean updateHeader() {
        ByteBuffer allocate = ByteBuffer.allocate(38);
        allocate.clear();
        allocate.order(ByteOrder.LITTLE_ENDIAN);
        try {
            mWriteChannel.position(0);
            if (((long) mWriteChannel.read(allocate)) < 38) {
                FileChannel fileChannel = mWriteChannel;
                if (fileChannel != null) {
                    try {
                        fileChannel.close();
                    } catch (IOException unused) {
                    }
                    mWriteChannel = null;
                }
                RandomAccessFile randomAccessFile = mOfs;
                if (randomAccessFile != null) {
                    try {
                        randomAccessFile.close();
                    } catch (IOException unused2) {
                    }
                    mOfs = null;
                }
                return false;
            }
            allocate.position(2);
            allocate.putInt(mFileSize);
            allocate.position(22);
            allocate.putInt(mHeight);
            allocate.position(34);
            allocate.putInt(mImageDataBytes);
            allocate.rewind();
            mWriteChannel.position(0);
            mWriteChannel.write(allocate);
            FileChannel fileChannel2 = mWriteChannel;
            if (fileChannel2 != null) {
                try {
                    fileChannel2.close();
                } catch (IOException unused3) {
                }
                mWriteChannel = null;
            }
            RandomAccessFile randomAccessFile2 = mOfs;
            if (randomAccessFile2 == null) {
                return true;
            }
            try {
                randomAccessFile2.close();
            } catch (IOException unused4) {
            }
            mOfs = null;
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            FileChannel fileChannel3 = mWriteChannel;
            if (fileChannel3 != null) {
                try {
                    fileChannel3.close();
                } catch (IOException unused5) {
                }
                mWriteChannel = null;
            }
            RandomAccessFile randomAccessFile3 = mOfs;
            if (randomAccessFile3 != null) {
                try {
                    randomAccessFile3.close();
                } catch (IOException unused6) {
                }
                mOfs = null;
            }
            return false;
        } catch (Throwable th) {
            FileChannel fileChannel4 = mWriteChannel;
            if (fileChannel4 != null) {
                try {
                    fileChannel4.close();
                } catch (IOException unused7) {
                }
                mWriteChannel = null;
            }
            RandomAccessFile randomAccessFile4 = mOfs;
            if (randomAccessFile4 != null) {
                try {
                    randomAccessFile4.close();
                } catch (IOException unused8) {
                }
                mOfs = null;
            }
            throw th;
        }
    }
    protected void addSize(int i, int i2) {
        mFileSize += i;
        mImageDataBytes += i;
        mHeight += i2;
    }

    public static BmpFileInfo createH0Instance(int i, int i2, String str) {
        BmpFileInfo bmpFileInfo = new BmpFileInfo();
        bmpFileInfo.mFileName = str;
        bmpFileInfo.mHeaderSize = 40;
        bmpFileInfo.mDataOffset = 54;
        bmpFileInfo.mWidth = i;
        bmpFileInfo.mHeight = 0;
        bmpFileInfo.mBitPerPixel = i2;
        bmpFileInfo.mImageDataBytes = 0;
        bmpFileInfo.mFileSize = bmpFileInfo.mDataOffset;
        return bmpFileInfo;
    }
}
