优化ByteArray

This commit is contained in:
redkale
2023-04-09 21:28:55 +08:00
parent 41d0cbcb46
commit 3178221c44
2 changed files with 32 additions and 1 deletions

View File

@@ -1300,7 +1300,9 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
}
}
this.contentType = MimeType.getByFilename(filename == null || filename.isEmpty() ? file.getName() : filename);
if (this.contentType == null) {
this.contentType = MimeType.getByFilename(filename == null || filename.isEmpty() ? file.getName() : filename);
}
if (this.contentType == null) {
this.contentType = "application/octet-stream";
}

View File

@@ -7,8 +7,10 @@ package org.redkale.util;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.*;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 简单的byte[]操作类。
@@ -60,6 +62,33 @@ public final class ByteArray implements ByteTuple {
return true;
}
public ReadableByteChannel toChannel() {
final byte[] bytes = getBytes();
final AtomicInteger offset = new AtomicInteger();
return new ReadableByteChannel() {
@Override
public int read(ByteBuffer dst) throws IOException {
if (offset.get() >= bytes.length) {
return -1;
}
int len = Math.min(bytes.length - offset.get(), dst.remaining());
dst.put(bytes, offset.get(), len);
offset.addAndGet(len);
return len;
}
@Override
public boolean isOpen() {
return true;
}
@Override
public void close() throws IOException {
}
};
}
/**
* 设置count的新位置
*