ProtoCommon.FDFS_FILE_PATH_LEN + ProtoCommon.FDFS_FILENAME_BASE64_LENGTH)); long file_size = ProtoCommon.buff2long(buff, 4 * 2); if (((remote_filename.length() > ProtoCommon.TRUNK_LOGIC_FILENAME_LENGTH) || ((remote_filename.length() > ProtoCommon.NORMAL_LOGIC_FILENAME_LENGTH) && ((file_size & ProtoCommon.TRUNK_FILE_MARK_SIZE) == 0))) || FileInfo fileInfo = new FileInfo(file_size, 0, 0, ProtoCommon.getIpAddress(buff, 0)); fileInfo.setCreateTimestamp(ProtoCommon.buff2int(buff, 4)); if ((file_size >> 63) != 0) { fileInfo.setCrc32(ProtoCommon.buff2int(buff, 4 * 4));
public void close() throws IOException { if (this.sock != null) { try { ProtoCommon.closeSocket(this.sock); } finally { this.sock = null; } } }
body_len = 3 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + appenderFilenameBytes.length + modify_size; header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_MODIFY_FILE, body_len, (byte) 0); byte[] wholePkg = new byte[(int) (header.length + body_len - modify_size)]; System.arraycopy(header, 0, wholePkg, 0, header.length); offset = header.length; hexLenBytes = ProtoCommon.long2buff(appender_filename.length()); System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length); offset += hexLenBytes.length; hexLenBytes = ProtoCommon.long2buff(file_offset); System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length); offset += hexLenBytes.length; hexLenBytes = ProtoCommon.long2buff(modify_size); System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length); offset += hexLenBytes.length; ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), ProtoCommon.STORAGE_PROTO_CMD_RESP, 0); this.errno = pkgInfo.errno;
/** * send ACTIVE_TEST command to server, test if network is ok and the server is alive * * @param sock the Socket object */ public static boolean activeTest(Socket sock) throws IOException { byte[] header; header = packHeader(FDFS_PROTO_CMD_ACTIVE_TEST, 0, (byte) 0); sock.getOutputStream().write(header); RecvHeaderInfo headerInfo = recvHeader(sock.getInputStream(), TRACKER_PROTO_CMD_RESP, 0); return headerInfo.errno == 0 ? true : false; }
int groupLen; bsOffset = ProtoCommon.long2buff(file_offset); bsDownBytes = ProtoCommon.long2buff(download_bytes); groupBytes = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN]; bs = group_name.getBytes(ClientGlobal.g_charset); header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_DOWNLOAD_FILE, bsOffset.length + bsDownBytes.length + groupBytes.length + filenameBytes.length, (byte) 0); byte[] wholePkg = new byte[header.length + bsOffset.length + bsDownBytes.length + groupBytes.length + filenameBytes.length];
header = ProtoCommon.packHeader(ProtoCommon.TRACKER_PROTO_CMD_SERVER_DELETE_STORAGE, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + ipAddrLen, (byte) 0); byte[] wholePkg = new byte[header.length + bGroupName.length + ipAddrLen]; System.arraycopy(header, 0, wholePkg, 0, header.length); out.write(wholePkg); ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(trackerSocket.getInputStream(), ProtoCommon.TRACKER_PROTO_CMD_RESP, 0); this.errno = pkgInfo.errno;
meta_buff = new byte[0]; } else { meta_buff = ProtoCommon.pack_metadata(meta_list).getBytes(ClientGlobal.g_charset); Arrays.fill(sizeBytes, (byte) 0); bs = ProtoCommon.long2buff(filenameBytes.length); System.arraycopy(bs, 0, sizeBytes, 0, bs.length); bs = ProtoCommon.long2buff(meta_buff.length); System.arraycopy(bs, 0, sizeBytes, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE, bs.length); header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_SET_METADATA, 2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + 1 + groupBytes.length + filenameBytes.length + meta_buff.length, (byte) 0); pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), ProtoCommon.STORAGE_PROTO_CMD_RESP, 0);
header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_QUERY_FILE_INFO, +groupBytes.length + filenameBytes.length, (byte) 0); OutputStream out = storageSocket.getOutputStream(); out.write(wholePkg); pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), ProtoCommon.STORAGE_PROTO_CMD_RESP, 3 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + long file_size = ProtoCommon.buff2long(pkgInfo.body, 0); int create_timestamp = (int) ProtoCommon.buff2long(pkgInfo.body, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); int crc32 = (int) ProtoCommon.buff2long(pkgInfo.body, 2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); String source_ip_addr = (new String(pkgInfo.body, 3 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE, ProtoCommon.FDFS_IPADDR_SIZE)).trim(); return new FileInfo(file_size, create_timestamp, crc32, source_ip_addr);
protected long longValue(byte[] bs, int offset, FieldInfo filedInfo) { return ProtoCommon.buff2long(bs, offset + filedInfo.offset); }
/** * receive whole pack * * @param in input stream * @param expect_cmd expect response command * @param expect_body_len expect response package body length * @return RecvPackageInfo: errno and reponse body(byte buff) */ public static RecvPackageInfo recvPackage(InputStream in, byte expect_cmd, long expect_body_len) throws IOException { RecvHeaderInfo header = recvHeader(in, expect_cmd, expect_body_len); if (header.errno != 0) { return new RecvPackageInfo(header.errno, null); } byte[] body = new byte[(int) header.body_len]; int totalBytes = 0; int remainBytes = (int) header.body_len; int bytes; while (totalBytes < header.body_len) { if ((bytes = in.read(body, totalBytes, remainBytes)) < 0) { break; } totalBytes += bytes; remainBytes -= bytes; } if (totalBytes != header.body_len) { throw new IOException("recv package size " + totalBytes + " != " + header.body_len); } return new RecvPackageInfo((byte) 0, body); }
pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), ProtoCommon.STORAGE_PROTO_CMD_RESP, -1); return ProtoCommon.split_metadata(new String(pkgInfo.body, ClientGlobal.g_charset)); } catch (IOException ex) { if (!bNewConnection) {
/** * send quit command to server and close socket * * @param sock the Socket object */ public static void closeSocket(Socket sock) throws IOException { byte[] header; header = packHeader(FDFS_PROTO_CMD_QUIT, 0, (byte) 0); sock.getOutputStream().write(header); sock.close(); }
/** * get token for file URL * * @param remote_filename the filename return by FastDFS server * @param ts unix timestamp, unit: second * @param secret_key the secret key * @return token string */ public static String getToken(String remote_filename, int ts, String secret_key) throws UnsupportedEncodingException, NoSuchAlgorithmException, MyException { byte[] bsFilename = remote_filename.getBytes(ClientGlobal.g_charset); byte[] bsKey = secret_key.getBytes(ClientGlobal.g_charset); byte[] bsTimestamp = (new Integer(ts)).toString().getBytes(ClientGlobal.g_charset); byte[] buff = new byte[bsFilename.length + bsKey.length + bsTimestamp.length]; System.arraycopy(bsFilename, 0, buff, 0, bsFilename.length); System.arraycopy(bsKey, 0, buff, bsFilename.length, bsKey.length); System.arraycopy(bsTimestamp, 0, buff, bsFilename.length + bsKey.length, bsTimestamp.length); return md5(buff); }
/** * pack header by FastDFS transfer protocol * * @param cmd which command to send * @param pkg_len package body length * @param errno status code, should be (byte)0 * @return packed byte buffer */ public static byte[] packHeader(byte cmd, long pkg_len, byte errno) throws UnsupportedEncodingException { byte[] header; byte[] hex_len; header = new byte[FDFS_PROTO_PKG_LEN_SIZE + 2]; Arrays.fill(header, (byte) 0); hex_len = ProtoCommon.long2buff(pkg_len); System.arraycopy(hex_len, 0, header, 0, hex_len.length); header[PROTO_HEADER_CMD_INDEX] = cmd; header[PROTO_HEADER_STATUS_INDEX] = errno; return header; }
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), ProtoCommon.STORAGE_PROTO_CMD_RESP, 0);
protected int int32Value(byte[] bs, int offset, FieldInfo filedInfo) { return ProtoCommon.buff2int(bs, offset + filedInfo.offset); }
meta_buff = new byte[0]; } else { meta_buff = ProtoCommon.pack_metadata(meta_list).getBytes(ClientGlobal.g_charset); Arrays.fill(sizeBytes, (byte) 0); bs = ProtoCommon.long2buff(filenameBytes.length); System.arraycopy(bs, 0, sizeBytes, 0, bs.length); bs = ProtoCommon.long2buff(meta_buff.length); System.arraycopy(bs, 0, sizeBytes, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE, bs.length); header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_SET_METADATA, 2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + 1 + groupBytes.length + filenameBytes.length + meta_buff.length, (byte) 0); pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), ProtoCommon.STORAGE_PROTO_CMD_RESP, 0);
System.arraycopy(bs, 0, bGroupName, 0, len); header = ProtoCommon.packHeader(cmd, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + bFileName.length, (byte) 0); byte[] wholePkg = new byte[header.length + bGroupName.length + bFileName.length]; System.arraycopy(header, 0, wholePkg, 0, header.length); out.write(wholePkg); ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(trackerSocket.getInputStream(), ProtoCommon.TRACKER_PROTO_CMD_RESP, -1); this.errno = pkgInfo.errno; int offset = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + ProtoCommon.FDFS_IPADDR_SIZE - 1; port = (int) ProtoCommon.buff2long(pkgInfo.body, offset); offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
/** * send ACTIVE_TEST command to server, test if network is ok and the server is alive * * @param sock the Socket object */ public static boolean activeTest(Socket sock) throws IOException { byte[] header; header = packHeader(FDFS_PROTO_CMD_ACTIVE_TEST, 0, (byte) 0); sock.getOutputStream().write(header); RecvHeaderInfo headerInfo = recvHeader(sock.getInputStream(), TRACKER_PROTO_CMD_RESP, 0); return headerInfo.errno == 0 ? true : false; }
int groupLen; bsOffset = ProtoCommon.long2buff(file_offset); bsDownBytes = ProtoCommon.long2buff(download_bytes); groupBytes = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN]; bs = group_name.getBytes(ClientGlobal.g_charset); header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_DOWNLOAD_FILE, bsOffset.length + bsDownBytes.length + groupBytes.length + filenameBytes.length, (byte) 0); byte[] wholePkg = new byte[header.length + bsOffset.length + bsDownBytes.length + groupBytes.length + filenameBytes.length];