1 /*
2 * Copyright (c) 2022-2023. Roland T. Lichti, Kaiserpfalz EDV-Service.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18 package de.kaiserpfalzedv.commons.api.resources;
19
20 import com.fasterxml.jackson.annotation.JsonIgnore;
21 import de.kaiserpfalzedv.commons.api.BaseWrappedException;
22
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25 import java.io.OutputStream;
26
27 /**
28 * HasData -- This resource contains a data blob represented by a byte array.
29 *
30 * @author klenkes74 {@literal <rlichti@kaiserpfalz-edv.de>}
31 * @since 0.1.0 2021-04-07
32 */
33 public interface HasData {
34 byte[] getData();
35
36 @JsonIgnore
37 default OutputStream getDataStream() {
38 byte[] data = getData();
39 if (data != null) {
40 OutputStream result = new ByteArrayOutputStream();
41 try {
42 result.write(data);
43 } catch (IOException e) {
44 throw new BaseWrappedException(e);
45 }
46
47 return result;
48 } else {
49 return null;
50 }
51 }
52 }