【问题标题】:store byte array into class object java将字节数组存储到类对象java中
【发布时间】:2014-02-21 13:05:51
【问题描述】:

请查看以下代码并建议存储字节数组的最佳方法当前我正在尝试存储字节数组,但它仅存储引用而不是实际数据(字节)。

sequence.setFileData(dataInBytes); // 这里有问题???

请提供此问题的解决方案。

 package com.asn;

/**
* Class TestSequence Represent the following ASN.1 Syntax 
* This is class support one document one signature 
* and one document counter signature.
* 
* ASN.1 Syntax : 
*  
* SignedDocument DEFINITIONS ::= 
*  BEGIN
*
* Doc ::= SEQUENCE {
*    file-name UTF8String,
*    file-type UTF8String,
*    fileData OCTET STRING,
*  
* }
*
*
*
* ASN.1 Value Syntax : 
*  
*  first-Doc Doc ::= 
* {   file-name "test", 
*      file-type "pdf",
*     fileData '7465737420'H
*    
* }
* 
* 
 * */


import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.util.Vector;

import org.bn.annotations.ASN1Element;
import org.bn.annotations.ASN1Sequence;
import org.bn.annotations.ASN1String;
import org.bn.coders.UniversalTag;


 @ASN1Sequence ( name = "TestSequence", isSet = false )
 public class TestSequence implements Serializable{

@ASN1String(name = "", stringType = UniversalTag.UTF8String, isUCS = false )
@ASN1Element(name = "fileName" , isOptional = false, hasTag = false, hasDefaultValue = false)
private String fileName = null;

@ASN1String(name = "", stringType = UniversalTag.UTF8String, isUCS = false)
@ASN1Element(name = "fileType", isOptional = false, hasTag = false, hasDefaultValue = false)
private String fileType = null;

@ASN1String(name = "", stringType = UniversalTag.OctetString, isUCS  = false)
@ASN1Element(name = "fileData", isOptional = false, hasTag = false, hasDefaultValue = false)
private byte[] fileData = null;


public String getFileName(){
    return this.fileName;
}

public void setFileName(String value){
    this.fileName = value;
}


public String getFileType() {
    return fileType;
}

public void setFileType(String fileType) {
    this.fileType = fileType;
}

public byte[] getFileData() {
    return fileData;
}

public void setFileData(byte[] fileData) {
    this.fileData = fileData;
}




 } // TestSequence ends here

 public static void main(String u[]){
String docFile = "D:/data/mytext.txt";
String newFile = "D:/data/new_mytext.txt";
    File file = new File(docFile);

    TestSequence sequence = new TestSequence();
    String name[] = file.getName().split("\\.");
    sequence.setFileName(name[0]);
    sequence.setFileType(name[1]);

    FileInputStream fis  = new FileInputStream(file);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    for (int readNum; (readNum = fis.read(buf)) != -1;) {
        bos.write(buf, 0, readNum); //no doubt here is 0
        //Writes len bytes from the specified byte array starting at offset off to this byte array output stream.

    }

    byte[] dataInBytes = bos.toByteArray();
    sequence.setFileData(dataInBytes);  // here is the problem ???

        IEncoder<TestSequence> encoder;
        encoder = CoderFactory.getInstance().newEncoder("BER");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        encoder.encode(sequence, outputStream);

        FileOutputStream fos = new FileOutputStream (new File(newFile));
        outputStream.writeTo(fos);
        fos.flush();
        fos.close();
}

【问题讨论】:

  • 克隆数组,然后将其传递给 setter。
  • 能否提供示例
  • 有什么问题?是的,字节数组是可变的,所以一些粗心/邪恶的人可能会在将它传递给 setFileData 后对其进行变异,但这不会发生在这里。代码需要有多健壮?
  • (如果数据被克隆,它应该由 setter 完成,而不是“passer”。您正在保护自己免受“passer”中粗心/恶意代码的影响。)
  • 我的问题是以字符串或任何形式将字节存储在 fileData 属性中。所以当我想从 fileData 重新生成原始文件时应该是相同的并且

标签: java bytearray asn.1


【解决方案1】:

您应该将setFileData 定义替换为:

public void setFileData(byte[] fileData) { this.fileData = java.util.Arrays.copyOf(fileData, fileData.length); }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    相关资源
    最近更新 更多