Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I'd like to convert the JAVA code shared by GIT-HUB to C#. I also used "Java to C# Converter" but it was not very successful.if u know ,help me out
thanks in advance
<
Java
    public class EpfFileHandler extends FileHandler {

    public static final int HEADER_SIZE = 0xC;
    public static final int FRAME_SIZE = 0x10;

    public static int dataSize = 0;
    public static boolean allFramesLoaded = false;

    public int frameCount;
    public int width;
    public int height;
    public int bitBLT;
    public long pixelDataLength;
    public Map<Integer, Frame> frames_map;

    public EpfFileHandler(String filepath) {
        this(new File(filepath), false);
    }
    public EpfFileHandler(String filepath, boolean loadAllFrames) {
        this(new File(filepath), loadAllFrames);
    }

    public EpfFileHandler(ByteBuffer bytes) { this(bytes, false); }
    public EpfFileHandler(ByteBuffer bytes, boolean loadAllFrames) {
        super(bytes);
        init(loadAllFrames);
    }

    public EpfFileHandler(File file) {
        this(file, false);
    }
    public EpfFileHandler(File file, boolean loadAllFrames) {
        super(file);
        init(loadAllFrames);
    }

    public void init(boolean loadAllFrames) {

        frames_map = new HashMap<Integer, Frame>();

        this.frameCount = this.readShort(true, false);
        this.width = this.readShort(true, false);
        this.height = this.readShort(true, false);
        this.bitBLT = this.readShort(true, false);
        this.pixelDataLength = this.readInt(true, true);

        if (loadAllFrames) {
            this.loadAllFrames();
        }
    }

    public Frame getFrame(int index) {
        // Return Frame if Cached
        if (this.frames_map.containsKey(index)) {
            return this.frames_map.get(index);
        }

        // Seek to Frame and read
        this.seek((HEADER_SIZE + this.pixelDataLength + (index * FRAME_SIZE)), true);
        int top = this.readShort(true, false);
        int left = this.readShort(true, false);
        int bottom = this.readShort(true, false);
        int right = this.readShort(true, false);

        int width = (right - left);
        int height = (bottom - top);

        long pixelDataOffset = this.readInt(true, true);
        long stencilDataOffset = this.readInt(true, true);

        // Seek to Pixel Data and Stencil Data
        this.seek(HEADER_SIZE + pixelDataOffset, true);
        ByteBuffer rawPixelData = this.readBytes((width * height), true);
        dataSize += rawPixelData.capacity();
        Stencil stencil = new Stencil(this, stencilDataOffset, new Dimension(width, height));
        ByteBuffer rawStencilData = stencil.toByteBuffer();
        dataSize += rawStencilData.capacity();

        Frame frame = new Frame(top, left, bottom, right, width, height, pixelDataOffset, stencilDataOffset, rawPixelData, rawStencilData, stencil);
        this.frames_map.put(index, frame);

        return this.frames_map.get(index);
    }

    /*
     * Loads all Frames in the EPF to frames_map.
     */
    public void loadAllFrames() {
        for (int i = 0; i < this.frameCount; i++) {
            this.getFrame(i);
        }
        allFramesLoaded = true;
    }

    public ByteBuffer toByteBuffer() {
        if (!allFramesLoaded) {
            this.loadAllFrames();
        }

        ByteBuffer epfBytes = ByteBuffer.allocate(HEADER_SIZE + (FRAME_SIZE * this.frameCount) + dataSize);
        epfBytes.order(ByteOrder.LITTLE_ENDIAN);

        // Header
        epfBytes.putShort((short)this.frameCount);
        epfBytes.putShort((short)this.width);
        epfBytes.putShort((short)this.height);
        epfBytes.putShort((short)this.bitBLT);
        epfBytes.putInt((int)this.pixelDataLength);

        // Frames (Pixel Data)
        for (int i = 0; i < this.frames_map.size(); i++) {
            Frame frame = this.frames_map.get(i);
            epfBytes.put(frame.getRawPixelData());
            epfBytes.put(frame.getRawStencilData());
        }

        // Frames (TOC)
        for (int i = 0; i < this.frames_map.size(); i++) {
            Frame frame = this.frames_map.get(i);

            epfBytes.putShort((short)frame.getTop());
            epfBytes.putShort((short)frame.getLeft());
            epfBytes.putShort((short)frame.getBottom());
            epfBytes.putShort((short)frame.getRight());
            epfBytes.putInt((int)frame.getPixelDataOffset());
            epfBytes.putInt((int)frame.getStencilDataOffset());
        }

        return epfBytes;
    }
}


What I have tried:

Search or use "JAVA TO C# CONVERTER"
Posted
Updated 10-Mar-21 21:39pm

1 solution

This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it’ll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900