content: /*
   * Copyright (C) 2006 The Android Open Source Project
   *
   * Licensed 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 com.aefyr.sai.utils.saf;
  
  import android.text.TextUtils;
  
  import java.nio.charset.StandardCharsets;
  
  public class FileUtils {
  
      /**
       * Mutate the given filename to make it valid for a FAT filesystem,
       * replacing any invalid characters with "_".
       */
      public static String buildValidFatFilename(String name) {
          if (TextUtils.isEmpty(name) || ".".equals(name) || "..".equals(name)) {
              return "(invalid)";
          }
          final StringBuilder res = new StringBuilder(name.length());
          for (int i = 0; i < name.length(); i++) {
              final char c = name.charAt(i);
              if (isValidFatFilenameChar(c)) {
                  res.append(c);
              } else {
                  res.append('_');
              }
          }
          // Even though vfat allows 255 UCS-2 chars, we might eventually write to
          // ext4 through a FUSE layer, so use that limit.
          trimFilename(res, 255);
          return res.toString();
      }
  
      private static boolean isValidFatFilenameChar(char c) {
          if ((0x00 <= c && c <= 0x1f)) {
              return false;
          }
          switch (c) {
              case '"':
              case '*':
              case '/':
              case ':':
              case '<':
              case '>':
              case '?':
              case '\\':
              case '|':
              case 0x7F:
                  return false;
              default:
                  return true;
          }
      }
  
      private static void trimFilename(StringBuilder res, int maxBytes) {
          byte[] raw = res.toString().getBytes(StandardCharsets.UTF_8);
          if (raw.length > maxBytes) {
              maxBytes -= 3;
              while (raw.length > maxBytes) {
                  res.deleteCharAt(res.length() / 2);
                  raw = res.toString().getBytes(StandardCharsets.UTF_8);
              }
              res.insert(res.length() / 2, "...");
          }
      }
  
  }
  