All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dopan.fastdfs.client.core.ProtoStructDecoder Maven / Gradle / Ivy

package net.dopan.fastdfs.client.core;

import java.io.IOException;
import java.lang.reflect.Array;

/**
 * C结构体解码器
 */
public class ProtoStructDecoder {
  /**
   * Constructor
   */
  public ProtoStructDecoder() {
  }

  /**
   * decode byte buffer
   * @param bs 参数
   * @param clazz 参数
   * @param fieldsTotalSize 参数
   * @return result 返回
   * @throws Exception 异常
   */
  public T[] decode(byte[] bs, Class clazz, int fieldsTotalSize) throws Exception {
    if (bs.length % fieldsTotalSize != 0) {
      throw new IOException("byte array length: " + bs.length + " is invalid!");
    }

    int count = bs.length / fieldsTotalSize;
    int offset;
    T[] results = (T[]) Array.newInstance(clazz, count);

    offset = 0;
    for (int i = 0; i < results.length; i++) {
      results[i] = clazz.newInstance();
      results[i].setFields(bs, offset);
      offset += fieldsTotalSize;
    }

    return results;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy