@@ -21,8 +21,7 @@ public final class SentryEnvelopeItemHeader implements JsonSerializable, JsonUnk
2121 private final int length ;
2222 @ Nullable private final Callable <Integer > getLength ;
2323 private final @ Nullable String attachmentType ;
24- private final @ Nullable Integer metaLength ;
25- @ Nullable private final Callable <Integer > getMetaLength ;
24+ @ Nullable private final Callable <Integer > calculateMetaLength ;
2625
2726 private @ Nullable Map <String , Object > unknown ;
2827
@@ -54,31 +53,41 @@ public int getLength() {
5453 return platform ;
5554 }
5655
57- public @ Nullable Integer getMetaLength () {
58- if (getMetaLength != null ) {
56+ @ Nullable Integer getMetaLength () {
57+ if (calculateMetaLength != null ) {
5958 try {
60- return getMetaLength .call ();
59+ return calculateMetaLength .call ();
6160 } catch (Throwable ignored ) {
6261 return null ;
6362 }
6463 }
65- return metaLength ;
64+ return null ;
6665 }
6766
68- @ ApiStatus .Internal
69- public SentryEnvelopeItemHeader (
67+ private SentryEnvelopeItemHeader (
7068 final @ NotNull SentryItemType type ,
7169 int length ,
70+ final @ Nullable Callable <Integer > getLength ,
7271 final @ Nullable String contentType ,
7372 final @ Nullable String fileName ,
7473 final @ Nullable String attachmentType ,
7574 final @ Nullable String platform ,
76- final @ Nullable Integer itemCount ) {
77- this (type , length , contentType , fileName , attachmentType , platform , itemCount , null );
75+ final @ Nullable Integer itemCount ,
76+ final @ Nullable Callable <Integer > calculateMetaLength ) {
77+ this .type = Objects .requireNonNull (type , "type is required" );
78+ this .contentType = contentType ;
79+ this .length = length ;
80+ this .fileName = fileName ;
81+ this .getLength = getLength ;
82+ this .attachmentType = attachmentType ;
83+ this .platform = platform ;
84+ this .itemCount = itemCount ;
85+ this .calculateMetaLength = calculateMetaLength ;
7886 }
7987
88+ /** Eager constructor used internally by {@link Deserializer#deserialize}. */
8089 @ ApiStatus .Internal
81- public SentryEnvelopeItemHeader (
90+ private SentryEnvelopeItemHeader (
8291 final @ NotNull SentryItemType type ,
8392 int length ,
8493 final @ Nullable String contentType ,
@@ -87,44 +96,37 @@ public SentryEnvelopeItemHeader(
8796 final @ Nullable String platform ,
8897 final @ Nullable Integer itemCount ,
8998 final @ Nullable Integer metaLength ) {
90- this .type = Objects .requireNonNull (type , "type is required" );
91- this .contentType = contentType ;
92- this .length = length ;
93- this .fileName = fileName ;
94- this .getLength = null ;
95- this .attachmentType = attachmentType ;
96- this .platform = platform ;
97- this .itemCount = itemCount ;
98- this .metaLength = metaLength ;
99- this .getMetaLength = null ;
99+ this (
100+ type , length , null , contentType , fileName , attachmentType , platform , itemCount ,
101+ metaLength != null ? () -> metaLength : null );
100102 }
101103
102- SentryEnvelopeItemHeader (
104+ @ ApiStatus .Internal
105+ public SentryEnvelopeItemHeader (
103106 final @ NotNull SentryItemType type ,
104- final @ Nullable Callable < Integer > getLength ,
107+ int length ,
105108 final @ Nullable String contentType ,
106109 final @ Nullable String fileName ,
107- final @ Nullable String attachmentType ) {
108- this (type , getLength , contentType , fileName , attachmentType , null , null );
110+ final @ Nullable String attachmentType ,
111+ final @ Nullable String platform ,
112+ final @ Nullable Integer itemCount ) {
113+ this (type , length , contentType , fileName , attachmentType , platform , itemCount , null );
109114 }
110115
116+ /**
117+ * Lazy constructor. Both length and metaLength are computed lazily as these depend on
118+ * the Item having been evaluated.
119+ */
111120 SentryEnvelopeItemHeader (
112121 final @ NotNull SentryItemType type ,
113122 final @ Nullable Callable <Integer > getLength ,
114123 final @ Nullable String contentType ,
115124 final @ Nullable String fileName ,
116125 final @ Nullable String attachmentType ,
117126 final @ Nullable String platform ,
118- final @ Nullable Integer itemCount ) {
119- this (
120- type ,
121- getLength ,
122- contentType ,
123- fileName ,
124- attachmentType ,
125- platform ,
126- itemCount ,
127- (Integer ) null );
127+ final @ Nullable Integer itemCount ,
128+ final @ Nullable Callable <Integer > calculateMetaLength ) {
129+ this (type , -1 , getLength , contentType , fileName , attachmentType , platform , itemCount , calculateMetaLength );
128130 }
129131
130132 SentryEnvelopeItemHeader (
@@ -134,39 +136,17 @@ public SentryEnvelopeItemHeader(
134136 final @ Nullable String fileName ,
135137 final @ Nullable String attachmentType ,
136138 final @ Nullable String platform ,
137- final @ Nullable Integer itemCount ,
138- final @ Nullable Integer metaLength ) {
139- this .type = Objects .requireNonNull (type , "type is required" );
140- this .contentType = contentType ;
141- this .length = -1 ;
142- this .fileName = fileName ;
143- this .getLength = getLength ;
144- this .attachmentType = attachmentType ;
145- this .platform = platform ;
146- this .itemCount = itemCount ;
147- this .metaLength = metaLength ;
148- this .getMetaLength = null ;
139+ final @ Nullable Integer itemCount ) {
140+ this (type , getLength , contentType , fileName , attachmentType , platform , itemCount , null );
149141 }
150142
151143 SentryEnvelopeItemHeader (
152144 final @ NotNull SentryItemType type ,
153145 final @ Nullable Callable <Integer > getLength ,
154146 final @ Nullable String contentType ,
155147 final @ Nullable String fileName ,
156- final @ Nullable String attachmentType ,
157- final @ Nullable String platform ,
158- final @ Nullable Integer itemCount ,
159- final @ Nullable Callable <Integer > getMetaLength ) {
160- this .type = Objects .requireNonNull (type , "type is required" );
161- this .contentType = contentType ;
162- this .length = -1 ;
163- this .fileName = fileName ;
164- this .getLength = getLength ;
165- this .attachmentType = attachmentType ;
166- this .platform = platform ;
167- this .itemCount = itemCount ;
168- this .metaLength = null ;
169- this .getMetaLength = getMetaLength ;
148+ final @ Nullable String attachmentType ) {
149+ this (type , getLength , contentType , fileName , attachmentType , null , null );
170150 }
171151
172152 SentryEnvelopeItemHeader (
@@ -219,8 +199,9 @@ public void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger
219199 if (itemCount != null ) {
220200 writer .name (JsonKeys .ITEM_COUNT ).value (itemCount );
221201 }
222- if (metaLength != null ) {
223- writer .name (JsonKeys .META_LENGTH ).value (metaLength );
202+ final @ Nullable Integer resolvedMetaLength = getMetaLength ();
203+ if (resolvedMetaLength != null ) {
204+ writer .name (JsonKeys .META_LENGTH ).value (resolvedMetaLength );
224205 }
225206 writer .name (JsonKeys .LENGTH ).value (getLength ());
226207 if (unknown != null ) {
0 commit comments