java get请求 字节,java.io.IOException:请求写入“字节”的请求超出了条目“”字节的标头中的大小...

I am creating a tar.Gzip file using GZIPOutputStream and I have added an another logic in that if any Exception caught while compressing file then, my code will retry three times.

When I am throwing an IOException to test my retry logic it throwing a below Exception:

java.io.IOException: request to write '4096' bytes exceeds size in header of '2644' bytes for entry 'Alldbtypes'

I am getting Exception at line: org.apache.commons.io.IOUtils.copyLarge(inputStream, tarStream);

private class CompressionStream extends GZIPOutputStream {

// Use compression levels from the deflator class

public CompressionStream(OutputStream out, int compressionLevel) throws IOException {

super(out);

def.setLevel(compressionLevel);

}

}

public void createTAR(){

boolean isSuccessful=false;

int count = 0;

int maxTries = 3;

while(!isSuccessful) {

InputStream inputStream =null;

FileOutputStream outputStream =null;

CompressionStream compressionStream=null;

OutputStream md5OutputStream = null;

TarArchiveOutputStream tarStream = null;

try{

inputStream = new BufferedInputStream(new FileInputStream(rawfile));

File stagingPath = new File("C:\\Workarea\\6d22b6a3-564f-42b4-be83-9e1573a718cd\\b88beb62-aa65-4ad5-b46c-4f2e9c892259.tar.gz");

boolean isDeleted = false;

if(stagingPath.exists()){

isDeleted = stagingPath.delete();

if(stagingPath.exists()){

try {

FileUtils.forceDelete(stagingPath);

}catch (IOException ex){

//ignore

}

}

}

outputStream = new FileOutputStream(stagingPath);

if (isCompressionEnabled) {

compressionStream = new

CompressionStream(outputStream, getCompressionLevel(om));

}

final MessageDigest outputDigest = MessageDigest.getInstance("MD5");

md5OutputStream = new DigestOutputStream(isCompressionEnabled ? compressionStream : outputStream, outputDigest);

tarStream = new TarArchiveOutputStream(new BufferedOutputStream(md5OutputStream));

tarStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

tarStream.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);

TarArchiveEntry entry = new TarArchiveEntry("Alldbtypes");

entry.setSize(getOriginalSize());

entry.setModTime(getLastModified().getMillis());

tarStream.putArchiveEntry(entry);

org.apache.commons.io.IOUtils.copyLarge(inputStream, tarStream);

inputStream.close();

tarStream.closeArchiveEntry();

tarStream.finish();

tarStream.close();

String digest = Hex.encodeHexString(outputDigest.digest());

setChecksum(digest);

setIngested(DateTime.now());

setOriginalSize(FileUtils.sizeOf(stagingPath));

isSuccessful =true;

} catch (IOException e) {

if (++count == maxTries) {

throw new RuntimeException("Exception: " + e.getMessage(), e);

}

} catch (NoSuchAlgorithmException e) {

throw new RuntimeException(Exception("MD5 hash algo not installed.");

} catch (Exception e) {

throw new RuntimeException("Exception: " + e.getMessage(), e);

} finally {

org.apache.commons.io.IOUtils.closeQuietly(inputStream);

try {

tarStream.flush();

tarStream.finish();

} catch (IOException e) {

e.printStackTrace();

}

org.apache.commons.io.IOUtils.closeQuietly(tarStream);

org.apache.commons.io.IOUtils.closeQuietly(compressionStream);

org.apache.commons.io.IOUtils.closeQuietly(md5OutputStream);

org.apache.commons.io.IOUtils.closeQuietly(outputStream);

}

}

}

解决方案

Case solved. This Exception java.io.IOException: request to write '4096' bytes exceeds size in header of '2644' bytes for entry 'Alldbtypes' thrown when the size of the file that going to be zipped is incorrect.

TarArchiveEntry entry = new TarArchiveEntry("Alldbtypes");

entry.setSize(getOriginalSize());

In my code getOriginalSize() is getting updated again at the end so in retry the original size became change and original size is now zipped file size so it was throwing this Exception.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值