content: /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *    http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.flink.streaming.connectors.pinot.filesystem;
  
  import org.apache.flink.annotation.Internal;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import java.io.File;
  import java.io.IOException;
  import java.nio.charset.Charset;
  import java.nio.file.Files;
  import java.util.List;
  import java.util.UUID;
  
  @Internal
  public class FileSystemUtils {
  
      private static final Logger LOG = LoggerFactory.getLogger(FileSystemUtils.class);
  
      /**
       * Writes a list of serialized elements to the temp directory of local filesystem
       * with prefix tempDirPrefix
       *
       * @param elements  List of serialized elements
       * @param targetDir Directory to create file in
       * @return File containing the written data
       * @throws IOException
       */
      public static File writeToLocalFile(List<String> elements, File targetDir) throws IOException {
          File dataFile = createFileInDir(targetDir);
  
          Files.write(dataFile.toPath(), elements, Charset.defaultCharset());
          LOG.debug("Successfully written data to file {}", dataFile.getAbsolutePath());
  
          return dataFile;
      }
  
      /**
       * Creates file with random name in targetDir.
       *
       * @param targetDir Directory to create file in
       * @return New File
       */
      public static File createFileInDir(File targetDir) {
          String fileName = String.format("%s.json", UUID.randomUUID().toString());
          return new File(targetDir.toString(), fileName);
      }
  }
  