将字节数组存储到类对象java中

时间:2014-02-21 13:05:52

标签: java bytearray asn.1

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

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();
}

1 个答案:

答案 0 :(得分:0)

您应该将setFileData定义替换为:

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