fastdfs-client-java/src/main/java/org/csource/fastdfs/ProtoStructDecoder.java

49 lines
1.1 KiB
Java
Raw Normal View History

2014-12-07 11:24:05 +08:00
/**
2017-05-19 09:56:24 +08:00
* Copyright (C) 2008 Happy Fish / YuQing
* <p>
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
* General Public License (LGPL).
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
*/
2014-12-07 11:24:05 +08:00
package org.csource.fastdfs;
2017-05-19 09:56:24 +08:00
import java.io.IOException;
2014-12-07 11:24:05 +08:00
import java.lang.reflect.Array;
/**
2017-05-19 09:56:24 +08:00
* C struct body decoder
*
* @author Happy Fish / YuQing
* @version Version 1.17
*/
public class ProtoStructDecoder<T extends StructBase> {
/**
* Constructor
*/
public ProtoStructDecoder() {
}
/**
* decode byte buffer
*/
public T[] decode(byte[] bs, Class<T> 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;
}
2014-12-07 11:24:05 +08:00
}