Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
01035216
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
01035216
编写于
6月 04, 2018
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Eliminate the need for Encoder#getContentLength
Issue: SPR-16892
上级
124d4c83
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
55 addition
and
72 deletion
+55
-72
spring-core/src/main/java/org/springframework/core/codec/ByteArrayEncoder.java
...java/org/springframework/core/codec/ByteArrayEncoder.java
+0
-4
spring-core/src/main/java/org/springframework/core/codec/ByteBufferEncoder.java
...ava/org/springframework/core/codec/ByteBufferEncoder.java
+0
-4
spring-core/src/main/java/org/springframework/core/codec/CharSequenceEncoder.java
...a/org/springframework/core/codec/CharSequenceEncoder.java
+0
-5
spring-core/src/main/java/org/springframework/core/codec/DataBufferEncoder.java
...ava/org/springframework/core/codec/DataBufferEncoder.java
+0
-5
spring-core/src/main/java/org/springframework/core/codec/Encoder.java
...src/main/java/org/springframework/core/codec/Encoder.java
+0
-11
spring-core/src/main/java/org/springframework/core/codec/ResourceEncoder.java
.../java/org/springframework/core/codec/ResourceEncoder.java
+0
-15
spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java
.../springframework/http/codec/EncoderHttpMessageWriter.java
+9
-13
spring-web/src/main/java/org/springframework/http/codec/ResourceHttpMessageWriter.java
...springframework/http/codec/ResourceHttpMessageWriter.java
+18
-5
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingMessageConversionIntegrationTests.java
...tion/RequestMappingMessageConversionIntegrationTests.java
+28
-10
未找到文件。
spring-core/src/main/java/org/springframework/core/codec/ByteArrayEncoder.java
浏览文件 @
01035216
...
...
@@ -55,8 +55,4 @@ public class ByteArrayEncoder extends AbstractEncoder<byte[]> {
return
Flux
.
from
(
inputStream
).
map
(
bufferFactory:
:
wrap
);
}
@Override
public
Long
getContentLength
(
byte
[]
bytes
,
@Nullable
MimeType
mimeType
)
{
return
(
long
)
bytes
.
length
;
}
}
spring-core/src/main/java/org/springframework/core/codec/ByteBufferEncoder.java
浏览文件 @
01035216
...
...
@@ -56,8 +56,4 @@ public class ByteBufferEncoder extends AbstractEncoder<ByteBuffer> {
return
Flux
.
from
(
inputStream
).
map
(
bufferFactory:
:
wrap
);
}
@Override
public
Long
getContentLength
(
ByteBuffer
byteBuffer
,
@Nullable
MimeType
mimeType
)
{
return
(
long
)
byteBuffer
.
array
().
length
;
}
}
spring-core/src/main/java/org/springframework/core/codec/CharSequenceEncoder.java
浏览文件 @
01035216
...
...
@@ -82,11 +82,6 @@ public class CharSequenceEncoder extends AbstractEncoder<CharSequence> {
return
charset
;
}
@Override
public
Long
getContentLength
(
CharSequence
data
,
@Nullable
MimeType
mimeType
)
{
return
(
long
)
data
.
toString
().
getBytes
(
getCharset
(
mimeType
)).
length
;
}
/**
* Create a {@code CharSequenceEncoder} that supports only "text/plain".
*/
...
...
spring-core/src/main/java/org/springframework/core/codec/DataBufferEncoder.java
浏览文件 @
01035216
...
...
@@ -55,9 +55,4 @@ public class DataBufferEncoder extends AbstractEncoder<DataBuffer> {
return
Flux
.
from
(
inputStream
);
}
@Override
public
Long
getContentLength
(
DataBuffer
dataBuffer
,
@Nullable
MimeType
mimeType
)
{
return
(
long
)
dataBuffer
.
readableByteCount
();
}
}
spring-core/src/main/java/org/springframework/core/codec/Encoder.java
浏览文件 @
01035216
...
...
@@ -67,17 +67,6 @@ public interface Encoder<T> {
Flux
<
DataBuffer
>
encode
(
Publisher
<?
extends
T
>
inputStream
,
DataBufferFactory
bufferFactory
,
ResolvableType
elementType
,
@Nullable
MimeType
mimeType
,
@Nullable
Map
<
String
,
Object
>
hints
);
/**
* Return the length for the given item, if known.
* @param t the item to check
* @return the length in bytes, or {@code null} if not known.
* @since 5.0.5
*/
@Nullable
default
Long
getContentLength
(
T
t
,
@Nullable
MimeType
mimeType
)
{
return
null
;
}
/**
* Return the list of mime types this encoder supports.
*/
...
...
spring-core/src/main/java/org/springframework/core/codec/ResourceEncoder.java
浏览文件 @
01035216
...
...
@@ -16,13 +16,11 @@
package
org.springframework.core.codec
;
import
java.io.IOException
;
import
java.util.Map
;
import
reactor.core.publisher.Flux
;
import
org.springframework.core.ResolvableType
;
import
org.springframework.core.io.InputStreamResource
;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.buffer.DataBuffer
;
import
org.springframework.core.io.buffer.DataBufferFactory
;
...
...
@@ -70,17 +68,4 @@ public class ResourceEncoder extends AbstractSingleValueEncoder<Resource> {
return
DataBufferUtils
.
read
(
resource
,
dataBufferFactory
,
this
.
bufferSize
);
}
@Override
public
Long
getContentLength
(
Resource
resource
,
@Nullable
MimeType
mimeType
)
{
// Don't consume InputStream...
if
(
InputStreamResource
.
class
!=
resource
.
getClass
())
{
try
{
return
resource
.
contentLength
();
}
catch
(
IOException
ignored
)
{
}
}
return
null
;
}
}
spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java
浏览文件 @
01035216
...
...
@@ -28,6 +28,7 @@ import reactor.core.publisher.Mono;
import
org.springframework.core.ResolvableType
;
import
org.springframework.core.codec.Encoder
;
import
org.springframework.core.io.buffer.DataBuffer
;
import
org.springframework.core.io.buffer.DataBufferFactory
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ReactiveHttpOutputMessage
;
...
...
@@ -98,23 +99,18 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
@Nullable
MediaType
mediaType
,
ReactiveHttpOutputMessage
message
,
Map
<
String
,
Object
>
hints
)
{
MediaType
contentType
=
updateContentType
(
message
,
mediaType
);
HttpHeaders
headers
=
message
.
getHeaders
();
if
(
headers
.
getContentLength
()
<
0
&&
!
headers
.
containsKey
(
HttpHeaders
.
TRANSFER_ENCODING
))
{
if
(
inputStream
instanceof
Mono
)
{
// This works because we don't actually commit until after the first signal...
inputStream
=
((
Mono
<
T
>)
inputStream
).
doOnNext
(
data
->
{
Long
contentLength
=
this
.
encoder
.
getContentLength
(
data
,
contentType
);
if
(
contentLength
!=
null
)
{
headers
.
setContentLength
(
contentLength
);
}
});
}
}
Flux
<
DataBuffer
>
body
=
this
.
encoder
.
encode
(
inputStream
,
message
.
bufferFactory
(),
elementType
,
contentType
,
hints
);
// Response is not committed until the first signal...
if
(
inputStream
instanceof
Mono
)
{
HttpHeaders
headers
=
message
.
getHeaders
();
if
(
headers
.
getContentLength
()
<
0
&&
!
headers
.
containsKey
(
HttpHeaders
.
TRANSFER_ENCODING
))
{
body
=
body
.
doOnNext
(
data
->
headers
.
setContentLength
(
data
.
readableByteCount
()));
}
}
return
(
isStreamingMediaType
(
contentType
)
?
message
.
writeAndFlushWith
(
body
.
map
(
Flux:
:
just
))
:
message
.
writeWith
(
body
));
}
...
...
spring-web/src/main/java/org/springframework/http/codec/ResourceHttpMessageWriter.java
浏览文件 @
01035216
...
...
@@ -31,6 +31,7 @@ import org.springframework.core.ResolvableType;
import
org.springframework.core.codec.ResourceDecoder
;
import
org.springframework.core.codec.ResourceEncoder
;
import
org.springframework.core.codec.ResourceRegionEncoder
;
import
org.springframework.core.io.InputStreamResource
;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.buffer.DataBuffer
;
import
org.springframework.core.io.buffer.DataBufferFactory
;
...
...
@@ -119,9 +120,9 @@ public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> {
headers
.
setContentType
(
resourceMediaType
);
if
(
headers
.
getContentLength
()
<
0
)
{
Long
contentLength
=
this
.
encoder
.
getContentLength
(
resource
,
mediaTyp
e
);
if
(
contentLength
!=
null
)
{
headers
.
setContentLength
(
contentL
ength
);
long
length
=
lengthOf
(
resourc
e
);
if
(
length
!=
-
1
)
{
headers
.
setContentLength
(
l
ength
);
}
}
...
...
@@ -141,6 +142,18 @@ public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> {
return
MediaTypeFactory
.
getMediaType
(
resource
).
orElse
(
MediaType
.
APPLICATION_OCTET_STREAM
);
}
private
static
long
lengthOf
(
Resource
resource
)
{
// Don't consume InputStream...
if
(
InputStreamResource
.
class
!=
resource
.
getClass
())
{
try
{
return
resource
.
contentLength
();
}
catch
(
IOException
ignored
)
{
}
}
return
-
1
;
}
private
static
Optional
<
Mono
<
Void
>>
zeroCopy
(
Resource
resource
,
@Nullable
ResourceRegion
region
,
ReactiveHttpOutputMessage
message
)
{
...
...
@@ -192,8 +205,8 @@ public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> {
if
(
regions
.
size
()
==
1
){
ResourceRegion
region
=
regions
.
get
(
0
);
headers
.
setContentType
(
resourceMediaType
);
Long
contentLength
=
this
.
encoder
.
getContentLength
(
resource
,
mediaTyp
e
);
if
(
contentLength
!=
null
)
{
long
contentLength
=
lengthOf
(
resourc
e
);
if
(
contentLength
!=
-
1
)
{
long
start
=
region
.
getPosition
();
long
end
=
start
+
region
.
getCount
()
-
1
;
end
=
Math
.
min
(
end
,
contentLength
-
1
);
...
...
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingMessageConversionIntegrationTests.java
浏览文件 @
01035216
...
...
@@ -128,45 +128,59 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
@Test
public
void
personResponseBody
()
throws
Exception
{
Person
expected
=
new
Person
(
"Robert"
);
assertEquals
(
expected
,
performGet
(
"/person-response/person"
,
JSON
,
Person
.
class
).
getBody
());
ResponseEntity
<
Person
>
responseEntity
=
performGet
(
"/person-response/person"
,
JSON
,
Person
.
class
);
assertEquals
(
17
,
responseEntity
.
getHeaders
().
getContentLength
());
assertEquals
(
expected
,
responseEntity
.
getBody
());
}
@Test
public
void
personResponseBodyWithCompletableFuture
()
throws
Exception
{
Person
expected
=
new
Person
(
"Robert"
);
assertEquals
(
expected
,
performGet
(
"/person-response/completable-future"
,
JSON
,
Person
.
class
).
getBody
());
ResponseEntity
<
Person
>
responseEntity
=
performGet
(
"/person-response/completable-future"
,
JSON
,
Person
.
class
);
assertEquals
(
17
,
responseEntity
.
getHeaders
().
getContentLength
());
assertEquals
(
expected
,
responseEntity
.
getBody
());
}
@Test
public
void
personResponseBodyWithMono
()
throws
Exception
{
Person
expected
=
new
Person
(
"Robert"
);
assertEquals
(
expected
,
performGet
(
"/person-response/mono"
,
JSON
,
Person
.
class
).
getBody
());
ResponseEntity
<
Person
>
responseEntity
=
performGet
(
"/person-response/mono"
,
JSON
,
Person
.
class
);
assertEquals
(
17
,
responseEntity
.
getHeaders
().
getContentLength
());
assertEquals
(
expected
,
responseEntity
.
getBody
());
}
@Test
public
void
personResponseBodyWithMonoDeclaredAsObject
()
throws
Exception
{
Person
expected
=
new
Person
(
"Robert"
);
assertEquals
(
expected
,
performGet
(
"/person-response/mono-declared-as-object"
,
JSON
,
Person
.
class
).
getBody
());
ResponseEntity
<
Person
>
entity
=
performGet
(
"/person-response/mono-declared-as-object"
,
JSON
,
Person
.
class
);
assertEquals
(
17
,
entity
.
getHeaders
().
getContentLength
());
assertEquals
(
expected
,
entity
.
getBody
());
}
@Test
public
void
personResponseBodyWithSingle
()
throws
Exception
{
Person
expected
=
new
Person
(
"Robert"
);
assertEquals
(
expected
,
performGet
(
"/person-response/single"
,
JSON
,
Person
.
class
).
getBody
());
ResponseEntity
<
Person
>
entity
=
performGet
(
"/person-response/single"
,
JSON
,
Person
.
class
);
assertEquals
(
17
,
entity
.
getHeaders
().
getContentLength
());
assertEquals
(
expected
,
entity
.
getBody
());
}
@Test
public
void
personResponseBodyWithMonoResponseEntity
()
throws
Exception
{
Person
expected
=
new
Person
(
"Robert"
);
assertEquals
(
expected
,
performGet
(
"/person-response/mono-response-entity"
,
JSON
,
Person
.
class
).
getBody
());
ResponseEntity
<
Person
>
entity
=
performGet
(
"/person-response/mono-response-entity"
,
JSON
,
Person
.
class
);
assertEquals
(
17
,
entity
.
getHeaders
().
getContentLength
());
assertEquals
(
expected
,
entity
.
getBody
());
}
@Test
// SPR-16172
public
void
personResponseBodyWithMonoResponseEntityXml
()
throws
Exception
{
String
actual
=
performGet
(
"/person-response/mono-response-entity-xml"
,
new
HttpHeaders
(),
String
.
class
).
getBody
();
String
url
=
"/person-response/mono-response-entity-xml"
;
ResponseEntity
<
String
>
entity
=
performGet
(
url
,
new
HttpHeaders
(),
String
.
class
);
String
actual
=
entity
.
getBody
();
assertEquals
(
91
,
entity
.
getHeaders
().
getContentLength
());
assertEquals
(
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
+
"<person><name>Robert</name></person>"
,
actual
);
}
...
...
@@ -174,13 +188,17 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
@Test
public
void
personResponseBodyWithList
()
throws
Exception
{
List
<?>
expected
=
asList
(
new
Person
(
"Robert"
),
new
Person
(
"Marie"
));
assertEquals
(
expected
,
performGet
(
"/person-response/list"
,
JSON
,
PERSON_LIST
).
getBody
());
ResponseEntity
<
List
<
Person
>>
entity
=
performGet
(
"/person-response/list"
,
JSON
,
PERSON_LIST
);
assertEquals
(
36
,
entity
.
getHeaders
().
getContentLength
());
assertEquals
(
expected
,
entity
.
getBody
());
}
@Test
public
void
personResponseBodyWithPublisher
()
throws
Exception
{
List
<?>
expected
=
asList
(
new
Person
(
"Robert"
),
new
Person
(
"Marie"
));
assertEquals
(
expected
,
performGet
(
"/person-response/publisher"
,
JSON
,
PERSON_LIST
).
getBody
());
ResponseEntity
<
List
<
Person
>>
entity
=
performGet
(
"/person-response/publisher"
,
JSON
,
PERSON_LIST
);
assertEquals
(-
1
,
entity
.
getHeaders
().
getContentLength
());
assertEquals
(
expected
,
entity
.
getBody
());
}
@Test
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录