1 /* 2 * Copyright 2022 Code Intelligence GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.code_intelligence.jazzer.driver; 18 19 import java.nio.charset.StandardCharsets; 20 import java.util.Arrays; 21 import java.util.Collection; 22 import java.util.List; 23 import java.util.stream.Collectors; 24 25 public class Utils { 26 /** 27 * Convert the arguments to UTF8 before passing them on to JNI as there are no JNI functions to 28 * get (unmodified) UTF-8 out of a jstring. 29 */ toNativeArgs(Collection<String> args)30 static byte[][] toNativeArgs(Collection<String> args) { 31 return args.stream().map(str -> str.getBytes(StandardCharsets.UTF_8)).toArray(byte[][] ::new); 32 } 33 fromNativeArgs(byte[][] args)34 static List<String> fromNativeArgs(byte[][] args) { 35 return Arrays.stream(args) 36 .map(bytes -> new String(bytes, StandardCharsets.UTF_8)) 37 .collect(Collectors.toList()); 38 } 39 } 40