优化ByteArray
This commit is contained in:
@@ -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";
|
||||
}
|
||||
|
||||
@@ -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的新位置
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user