规范maven项目结构并升级版本号(1.27-SNAPSHOT)
This commit is contained in:
55
src/main/java/org/csource/fastdfs/ProtoStructDecoder.java
Normal file
55
src/main/java/org/csource/fastdfs/ProtoStructDecoder.java
Normal file
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Copyright (C) 2008 Happy Fish / YuQing
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.csource.fastdfs;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.net.*;
|
||||
import java.lang.reflect.Array;
|
||||
import org.csource.common.*;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user