forked from oras-project/oras-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.java
More file actions
130 lines (115 loc) · 3 KB
/
Config.java
File metadata and controls
130 lines (115 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package land.oras;
import java.util.Base64;
import java.util.Map;
import land.oras.utils.Const;
import land.oras.utils.JsonUtils;
import org.jspecify.annotations.NullUnmarked;
import org.jspecify.annotations.Nullable;
/**
* Class for config
*/
@NullUnmarked
public final class Config {
private final String mediaType;
private final String digest;
private final long size;
/**
* Annotations for the layer
* Can be nullable due to serialization
*/
private final @Nullable Map<String, String> annotations;
/**
* The base 64 encoded data. Never serialized because configuration
* is always referenced by digest.
*/
private final transient @Nullable String data;
/**
* Constructor
* @param mediaType The media type
* @param digest The digest
* @param size The size
*/
private Config(String mediaType, String digest, long size, @Nullable String data, Annotations annotations) {
this.mediaType = mediaType;
this.digest = digest;
this.size = size;
this.data = data;
this.annotations = Map.copyOf(annotations.configAnnotations());
}
/**
* Get the media type
* @return The media type
*/
public String getMediaType() {
return mediaType;
}
/**
* Get the digest
* @return The digest
*/
public String getDigest() {
return digest;
}
/**
* Get the size
* @return The size
*/
public long getSize() {
return size;
}
/**
* Create a new config with annotations
* @param annotations The annotations
* @return The new config
*/
public Config withAnnotations(Annotations annotations) {
return new Config(mediaType, digest, size, data, annotations);
}
/**
* Get the data as bytes
* @return The data as bytes
*/
public byte[] getDataBytes() {
if (data != null) {
return Base64.getDecoder().decode(data);
}
return "{}".getBytes();
}
/**
* Get the annotations
* @return The annotations
*/
public Map<String, String> getAnnotations() {
if (annotations == null) {
return Map.of();
}
return annotations;
}
/**
* Return the JSON representation of the manifest
* @return The JSON string
*/
public String toJson() {
return JsonUtils.toJson(this);
}
/**
* Create a manifest from a JSON string
* @param json The JSON string
* @return The manifest
*/
public static Config fromJson(String json) {
return JsonUtils.fromJson(json, Config.class);
}
/**
* An empty config
* @return The empty config
*/
public static Config empty() {
return new Config(
Const.DEFAULT_EMPTY_MEDIA_TYPE,
"sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a",
2,
"e30=",
Annotations.empty());
}
}