Ollama4j
A Java library (wrapper/binding) for Ollama server.
Loading...
Searching...
No Matches
Utils.java
Go to the documentation of this file.
1package io.github.ollama4j.utils;
2
3import com.fasterxml.jackson.databind.ObjectMapper;
4import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
5
6import java.io.ByteArrayOutputStream;
7import java.io.File;
8import java.io.IOException;
9import java.io.InputStream;
10import java.net.URI;
11import java.net.URISyntaxException;
12import java.net.URL;
13import java.util.Objects;
14
15public class Utils {
16
17 private static ObjectMapper objectMapper;
18
19 public static ObjectMapper getObjectMapper() {
20 if (objectMapper == null) {
21 objectMapper = new ObjectMapper();
22 objectMapper.registerModule(new JavaTimeModule());
23 }
24 return objectMapper;
25 }
26
27 public static byte[] loadImageBytesFromUrl(String imageUrl)
28 throws IOException, URISyntaxException {
29 URL url = new URI(imageUrl).toURL();
30 try (InputStream in = url.openStream();
31 ByteArrayOutputStream out = new ByteArrayOutputStream()) {
32 byte[] buffer = new byte[1024];
33 int bytesRead;
34 while ((bytesRead = in.read(buffer)) != -1) {
35 out.write(buffer, 0, bytesRead);
36 }
37 return out.toByteArray();
38 }
39 }
40
41 public static File getFileFromClasspath(String fileName) {
42 ClassLoader classLoader = Utils.class.getClassLoader();
43 return new File(Objects.requireNonNull(classLoader.getResource(fileName)).getFile());
44 }
45}
static byte[] loadImageBytesFromUrl(String imageUrl)
Definition Utils.java:27
static ObjectMapper getObjectMapper()
Definition Utils.java:19
static File getFileFromClasspath(String fileName)
Definition Utils.java:41