Java ByteBuffer Save saveStruct(ByteBuffer o, String[] struct, Object instance)

Here you can find the source of saveStruct(ByteBuffer o, String[] struct, Object instance)

Description

save Struct

License

Open Source License

Declaration

public static void saveStruct(ByteBuffer o, String[] struct, Object instance) 

Method Source Code


//package com.java2s;
import java.nio.ByteBuffer;

public class Main {
    public static void saveStruct(ByteBuffer o, String[] struct, Object instance) {
        try {// w  ww.  j  a  v  a  2s.c o  m
            Class instClass = instance.getClass();
            for (String s : struct) {
                String[] args = s.split(" ");
                if (args[0].equals("size")) {
                    // ignored
                } else if (args[0].equals("data")) {
                    o.put(Long.decode(args[1]).byteValue());
                } else if (args[0].equals("skip")) {
                    // This Long.decode is allowed since it's format spec.
                    int space = (int) (long) Long.decode(args[1]);
                    o.position(o.position() + space);
                } else if (args[0].equals("fst")) {
                    byte[] r = (byte[]) instClass.getField(args[1]).get(instance);
                    // This Long.decode is allowed since it's format spec.
                    if (r.length != Long.decode(args[2]))
                        throw new RuntimeException("invalid situation!");
                    o.put(r);
                } else {
                    boolean u32 = args[0].equals("u32");
                    boolean u16 = args[0].equals("u16");
                    boolean u8 = args[0].equals("u8");
                    if (u32 || u16 || u8) {
                        if (u32) {
                            o.putInt(instClass.getField(args[1]).getInt(instance));
                        } else if (u16) {
                            o.putShort(instClass.getField(args[1]).getShort(instance));
                        } else {
                            o.put(instClass.getField(args[1]).getByte(instance));
                        }
                    } else {
                        throw new RuntimeException("Can't understand " + args[0]);
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. saveAsFile(File targetFile, ByteBuffer bb)
  2. saveAsTempFile(ByteBuffer byteBuffer, String extension)
  3. saveAsTGA(String filename, ByteBuffer pixel_data, int width, int height)
  4. saveBytesBufferToFile(ByteBuffer buf, String filename)
  5. savePageBitMask(ByteBuffer buffer, BitSet freePagesBitSet)