Here you can find the source of saveStruct(ByteBuffer o, String[] struct, Object instance)
public static void saveStruct(ByteBuffer o, String[] struct, Object instance)
//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); } } }