Esempio n. 1
0
File: IO.java Progetto: nremond/boon
  public static CharBuf read(Reader input, CharBuf charBuf, final int bufSize) {

    if (charBuf == null) {
      charBuf = CharBuf.create(bufSize);
    } else {
      charBuf.readForRecycle();
    }

    try {

      char[] buffer = charBuf.toCharArray();
      int size = input.read(buffer);
      if (size != -1) {
        charBuf._len(size);
      }
      if (size < buffer.length) {
        return charBuf;
      }

      copy(input, charBuf);

    } catch (IOException e) {
      Exceptions.handle(e);
    } finally {
      try {
        input.close();
      } catch (IOException e) {
        Exceptions.handle(e);
      }
    }

    return charBuf;
  }
Esempio n. 2
0
  /* End universal methods. */
  private static int calculateIndex(float[] array, int originalIndex) {
    final int length = array.length;

    Exceptions.requireNonNull(array, "array cannot be null");

    int index = originalIndex;

    /* Adjust for reading from the right as in
    -1 reads the 4th element if the length is 5
     */
    if (index < 0) {
      index = length + index;
    }

    /* Bounds check
       if it is still less than 0, then they
       have an negative index that is greater than length
    */
    /* Bounds check
       if it is still less than 0, then they
       have an negative index that is greater than length
    */
    if (index < 0) {
      index = 0;
    }
    if (index >= length) {
      index = length - 1;
    }
    return index;
  }
Esempio n. 3
0
  public static float[] grow(float[] array, final int size) {
    Exceptions.requireNonNull(array);

    float[] newArray = new float[array.length + size];
    System.arraycopy(array, 0, newArray, 0, array.length);
    return newArray;
  }
Esempio n. 4
0
 @Universal
 public static float[] copy(float[] array) {
   Exceptions.requireNonNull(array);
   float[] newArray = new float[array.length];
   System.arraycopy(array, 0, newArray, 0, array.length);
   return newArray;
 }
Esempio n. 5
0
File: IO.java Progetto: nremond/boon
 public static String read(File file) {
   try (Reader reader = new FileReader(file)) {
     return read(reader);
   } catch (Exception ex) {
     return Exceptions.handle(String.class, ex);
   }
 }
Esempio n. 6
0
  /**
   * Double the size of an array.
   *
   * @param array the array you are going to double
   * @return the new array with the old values copied in
   */
  public static int[] grow(int[] array) {
    Exceptions.requireNonNull(array);

    int[] newArray = new int[array.length * 2];
    System.arraycopy(array, 0, newArray, 0, array.length);
    return newArray;
  }
Esempio n. 7
0
  @Override
  public <T> Supplier<T> getSupplier(final Class<T> type, final String name) {

    ProviderInfo nullInfo = null;

    try {
      Set<ProviderInfo> set = Sets.set(supplierNameMap.getAll(name));

      for (ProviderInfo info : set) {

        if (info.type() == null) {
          nullInfo = info;
          continue;
        }
        if (type.isAssignableFrom(info.type())) {
          return (Supplier<T>) info.supplier();
        }
      }

      return (Supplier<T>)
          (nullInfo != null
              ? nullInfo.supplier()
              : new Supplier<T>() {
                @Override
                public T get() {
                  return null;
                }
              });

    } catch (Exception e) {
      Exceptions.handle(e);
      return null;
    }
  }
Esempio n. 8
0
File: IO.java Progetto: nremond/boon
  public static String read(final String location) {
    final URI uri = createURI(location);

    return Exceptions.tryIt(
        String.class,
        new Exceptions.TrialWithReturn<String>() {

          @Override
          public String tryIt() throws Exception {

            String path = location;

            path = getWindowsPathIfNeeded(path);

            if (uri.getScheme() == null) {

              Path thePath = FileSystems.getDefault().getPath(path);
              return read(Files.newBufferedReader(thePath, DEFAULT_CHARSET));

            } else if (uri.getScheme().equals(FILE_SCHEMA)) {

              return readFromFileSchema(uri);

            } else if (uri.getScheme().equals(CLASSPATH_SCHEMA)
                || uri.getScheme().equals(JAR_SCHEMA)) {

              return readFromClasspath(uri.toString());

            } else {
              return read(location, uri);
            }
          }
        });
  }
Esempio n. 9
0
File: IO.java Progetto: nremond/boon
 public static List<String> readLines(File file) {
   try (FileReader reader = new FileReader(file)) {
     return readLines(reader);
   } catch (Exception ex) {
     return Exceptions.handle(List.class, ex);
   }
 }
Esempio n. 10
0
File: IO.java Progetto: nremond/boon
  public static List<String> readLines(final String location) {

    final String path = getWindowsPathIfNeeded(location);

    final URI uri = createURI(path);

    return (List<String>)
        Exceptions.tryIt(
            Typ.list,
            new Exceptions.TrialWithReturn<List>() {
              @Override
              public List<String> tryIt() throws Exception {
                if (uri.getScheme() == null) {

                  Path thePath = FileSystems.getDefault().getPath(path);
                  return Files.readAllLines(thePath, DEFAULT_CHARSET);

                } else if (uri.getScheme().equals(FILE_SCHEMA)) {

                  Path thePath = FileSystems.getDefault().getPath(uri.getPath());
                  return Files.readAllLines(thePath, DEFAULT_CHARSET);

                } else {
                  return readLines(location, uri);
                }
              }
            });
  }
Esempio n. 11
0
File: IO.java Progetto: nremond/boon
 public static byte[] input(String fileName) {
   try {
     return input(Files.newInputStream(IO.path(fileName)));
   } catch (IOException e) {
     return Exceptions.handle(byte[].class, e);
   }
 }
Esempio n. 12
0
File: IO.java Progetto: nremond/boon
 public static void eachLine(File file, EachLine eachLine) {
   try (FileReader reader = new FileReader(file)) {
     eachLine(reader, eachLine);
   } catch (Exception ex) {
     Exceptions.handle(List.class, ex);
   }
 }
Esempio n. 13
0
File: IO.java Progetto: nremond/boon
 public static InputStream inputStream(String resource) {
   Path path = path(resource);
   try {
     return Files.newInputStream(path);
   } catch (IOException e) {
     return Exceptions.handle(InputStream.class, "unable to open " + resource, e);
   }
 }
Esempio n. 14
0
File: IO.java Progetto: nremond/boon
  public static void write(OutputStream out, String content, Charset charset) {

    try (OutputStream o = out) {
      o.write(content.getBytes(charset));
    } catch (Exception ex) {
      Exceptions.handle(ex);
    }
  }
Esempio n. 15
0
File: IO.java Progetto: nremond/boon
  public static void write(Path file, byte[] contents) {
    try {
      Files.write(file, contents);

    } catch (Exception ex) {
      Exceptions.handle(ex);
    }
  }
Esempio n. 16
0
File: IO.java Progetto: nremond/boon
  public static void writeNoClose(OutputStream out, String content) {

    try {
      out.write(content.getBytes(DEFAULT_CHARSET));
    } catch (Exception ex) {
      Exceptions.handle(ex);
    }
  }
Esempio n. 17
0
  public static float[] shrink(float[] array, int size) {
    Exceptions.requireNonNull(array);

    float[] newArray = new float[array.length - size];

    System.arraycopy(array, 0, newArray, 0, array.length - size);
    return newArray;
  }
Esempio n. 18
0
File: IO.java Progetto: nremond/boon
  public static CharBuf read(InputStream inputStream, CharBuf charBuf, Charset charset) {

    try (Reader reader = new InputStreamReader(inputStream, charset)) {
      return read(reader, charBuf);
    } catch (Exception ex) {
      return Exceptions.handle(CharBuf.class, ex);
    }
  }
Esempio n. 19
0
File: IO.java Progetto: nremond/boon
  public static char[] readCharBuffer(InputStream inputStream) {

    try (Reader reader = new InputStreamReader(inputStream)) {
      return readCharBuffer(reader);
    } catch (Exception ex) {
      return Exceptions.handle(char[].class, ex);
    }
  }
Esempio n. 20
0
File: IO.java Progetto: nremond/boon
  public static String read(InputStream inputStream) {

    try (Reader reader = new InputStreamReader(inputStream, DEFAULT_CHARSET)) {
      return read(reader);
    } catch (Exception ex) {
      return Exceptions.handle(String.class, ex);
    }
  }
Esempio n. 21
0
File: IO.java Progetto: nremond/boon
  public static String readCharBuffer(InputStream inputStream, Charset charset) {

    try (Reader reader = new InputStreamReader(inputStream, charset)) {
      return read(reader);
    } catch (Exception ex) {
      return Exceptions.handle(String.class, ex);
    }
  }
Esempio n. 22
0
File: IO.java Progetto: nremond/boon
  public static String read(Path path) {
    try {

      return read(Files.newBufferedReader(path, DEFAULT_CHARSET));

    } catch (IOException ex) {
      return Exceptions.handle(String.class, ex);
    }
  }
Esempio n. 23
0
File: IO.java Progetto: nremond/boon
  private static String readFromFileSchema(URI uri) {
    Path thePath = uriToPath(uri);

    try {
      return read(Files.newBufferedReader(thePath, DEFAULT_CHARSET));
    } catch (IOException e) {

      return Exceptions.handle(Typ.string, e); //
    }
  }
Esempio n. 24
0
File: IO.java Progetto: nremond/boon
  public static char[] readCharBuffer(Path path) {
    try {

      long bufSize = Files.size(path);
      return readCharBuffer(Files.newBufferedReader(path, DEFAULT_CHARSET), (int) bufSize);

    } catch (IOException ex) {
      return Exceptions.handle(char[].class, ex);
    }
  }
Esempio n. 25
0
File: IO.java Progetto: nremond/boon
  public static String readChild(Path parentDir, String childFileName) {
    try {

      final Path newFilePath = path(parentDir.toString(), childFileName);

      return read(newFilePath);
    } catch (Exception ex) {
      return Exceptions.handle(String.class, ex);
    }
  }
Esempio n. 26
0
File: IO.java Progetto: nremond/boon
  public static List<String> readLines(InputStream is) {

    try (Reader reader = new InputStreamReader(is, DEFAULT_CHARSET)) {

      return readLines(reader);

    } catch (Exception ex) {

      return Exceptions.handle(List.class, ex);
    }
  }
Esempio n. 27
0
File: IO.java Progetto: nremond/boon
  public static List<String> readLines(Reader reader) {

    try (BufferedReader bufferedReader = new BufferedReader(reader)) {

      return readLines(bufferedReader);

    } catch (Exception ex) {

      return Exceptions.handle(List.class, ex);
    }
  }
Esempio n. 28
0
File: IO.java Progetto: nremond/boon
  public static void writeChild(Path parentDir, String childFileName, String childContents) {

    try {

      final Path newFilePath = path(parentDir.toString(), childFileName);

      write(newFilePath, childContents);
    } catch (Exception ex) {
      Exceptions.handle(ex);
    }
  }
Esempio n. 29
0
File: IO.java Progetto: nremond/boon
  public static void eachLine(Reader reader, EachLine eachLine) {

    try (BufferedReader bufferedReader = new BufferedReader(reader)) {

      eachLine(bufferedReader, eachLine);

    } catch (Exception ex) {

      Exceptions.handle(List.class, ex);
    }
  }
Esempio n. 30
0
File: IO.java Progetto: nremond/boon
  public static void eachLine(InputStream is, EachLine eachLine) {

    try (Reader reader = new InputStreamReader(is, DEFAULT_CHARSET)) {

      eachLine(reader, eachLine);

    } catch (Exception ex) {

      Exceptions.handle(ex);
    }
  }