org.nervousync.beans.xml.files.SegmentationBlock Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils-jdk11 Show documentation
Show all versions of utils-jdk11 Show documentation
Java utility collections, development by Nervousync Studio (NSYC)
/*
* Licensed to the Nervousync Studio (NSYC) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.nervousync.beans.xml.files;
import jakarta.xml.bind.annotation.*;
import org.nervousync.commons.adapter.xml.CDataAdapter;
import org.nervousync.beans.converter.impl.basic.DateTimeAdapter;
import org.nervousync.beans.core.BeanObject;
import org.nervousync.utils.ConvertUtils;
import org.nervousync.utils.SecurityUtils;
import org.nervousync.utils.StringUtils;
import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.util.Date;
import java.util.Objects;
/**
* Segment Data Block Define
* 分割数据块定义
*
* @author Steven Wee [email protected]
* @version $Revision: 1.0.0 $Date: Oct 15, 2018 12:41:27 $
*/
@XmlType(name = "segment_block", namespace = "https://nervousync.org/schemas/segment")
@XmlRootElement(name = "segment_block", namespace = "https://nervousync.org/schemas/segment")
@XmlAccessorType(XmlAccessType.NONE)
public final class SegmentationBlock extends BeanObject {
/**
* Serial version UID
* 序列化UID
*/
private static final long serialVersionUID = 2993229461743423521L;
/**
* Block begin position
* 数据块起始地址
*/
@XmlElement(name = "position")
private long position;
/**
* Block data size
* 数据块大小
*/
@XmlElement(name = "block_size")
private long blockSize;
/**
* Block data identified value of SHA256
* 数据块验证值,使用SHA256
*/
@XmlElement(name = "signature_sha")
private String sha;
/**
* Block data information, base64 encoded data bytes
* 数据块信息,使用Base64编码
*/
@XmlElement(name = "data_info")
@XmlJavaTypeAdapter(CDataAdapter.class)
private String dataInfo;
/**
* Block generate time
* 数据块生成时间
*/
@XmlElement(name = "current_time")
@XmlJavaTypeAdapter(DateTimeAdapter.class)
private Date currentTime;
/**
* Default constructor for SegmentationBlock
* SegmentationBlock的默认构造函数
*/
public SegmentationBlock() {
}
/**
* Constructor for SegmentationBlock
* Using given block begin position and binary array of data content
* SegmentationBlock的构造函数
* 使用给定的数据块起始地址和数据的字节数组
*
* @param position Block begin position
* 数据块起始地址
* @param dataContent Binary array of data content
* 数据的字节数组
*/
public SegmentationBlock(long position, byte[] dataContent) {
this.sha = ConvertUtils.toHex(SecurityUtils.SHA256(dataContent));
this.position = position;
this.blockSize = dataContent.length;
this.dataInfo = StringUtils.base64Encode(dataContent);
this.currentTime = new Date();
}
/**
* Getter method for block begin position
* 数据块起始地址的Getter方法
*
* @return Block begin position
* 数据块起始地址
*/
public long getPosition() {
return position;
}
/**
* Getter method for block size
* 数据块长度的Getter方法
*
* @return Block data size
* 数据块大小
*/
public long getBlockSize() {
return blockSize;
}
/**
* Getter method for identified value
* 数据块验证值的Getter方法
*
* @return Block data identified value of SHA256
* 数据块验证值,使用SHA256
*/
public String getSha() {
return sha;
}
/**
* Getter method for block data information
* 数据块信息的Getter方法
*
* @return Block data information, base64 encoded data bytes
* 数据块信息,使用Base64编码
*/
public String getDataInfo() {
return dataInfo;
}
/**
* Getter method for block generate time
* 数据块生成时间的Getter方法
*
* @return Block generate time
* 数据块生成时间
*/
public Date getCurrentTime() {
return currentTime == null ? null : (Date)currentTime.clone();
}
/**
* Verify current data block
* 验证当前数据块
*
* @return true
for valid, false
for invalid
* true
验证通过,false
验证失败
*/
public boolean securityCheck() {
byte[] dataContent = StringUtils.base64Decode(this.dataInfo);
try {
return dataContent.length == this.blockSize
&& Objects.equals(ConvertUtils.toHex(SecurityUtils.SHA256(dataContent)), this.sha);
} catch (Exception e) {
return Boolean.FALSE;
}
}
}