繁体   English   中英

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

[英]store byte array into class object java

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

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

您应该使用以下内容替换setFileData定义:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM