示例#1
0
 /** @throws IOException */
 @Test
 public void testNewlines() throws IOException {
   final File temp = File.createTempFile("io-helper", ".txt");
   try {
     IOHelper.write(temp, "\n\n");
     assertThat(IOHelper.readToString(temp), is("\n\n"));
   } finally {
     temp.delete();
   }
 }
示例#2
0
  public static void copyFile(File from, File to) throws IOException {
    FileChannel in = new FileInputStream(from).getChannel();
    FileChannel out = new FileOutputStream(to).getChannel();
    try {
      if (LOG.isTraceEnabled()) {
        LOG.trace("Using FileChannel to copy from: " + in + " to: " + out);
      }

      long size = in.size();
      long position = 0;
      while (position < size) {
        position += in.transferTo(position, BUFFER_SIZE, out);
      }
    } finally {
      IOHelper.close(in, from.getName(), LOG);
      IOHelper.close(out, to.getName(), LOG);
    }
  }
示例#3
0
  public static String removeXmlns(File file) throws IOException {
    InputStream forEncodingInput = new FileInputStream(file);
    String encoding = XMLHelper.getXMLEncoding(forEncodingInput);
    forEncodingInput.close();

    InputStream input = new FileInputStream(file);
    String xml = IOHelper.toString(encoding, input);
    xml = XMLHelper.removeXmlns(xml);
    input.close();
    return xml;
  }
示例#4
0
 /**
  * Attempts to grab the lock for the given file, returning a FileLock if the lock has been
  * created; otherwise it returns null
  */
 public static FileLocker getLock(File lockFile) {
   lockFile.getParentFile().mkdirs();
   if (!lockFile.exists()) {
     try {
       IOHelper.write(lockFile, "I have the lock!");
       lockFile.deleteOnExit();
       return new FileLocker(lockFile);
     } catch (IOException e) {
       // Ignore
     }
   }
   return null;
 }
 /** {@inheritDoc} */
 public void writeTo(
     DataSource t,
     Class<?> type,
     Type genericType,
     Annotation[] annotations,
     MediaType mediaType,
     MultivaluedMap<String, Object> httpHeaders,
     OutputStream entityStream)
     throws IOException {
   InputStream in = t.getInputStream();
   try {
     IOHelper.write(in, entityStream);
   } finally {
     in.close();
   }
 }
示例#6
0
 private static Properties loadLog4jProperties() {
   try {
     File file = FileHelper.getFileByClassLoader("log4j.properties");
     Properties props = new Properties();
     FileInputStream in = new FileInputStream(file);
     try {
       props.load(in);
     } finally {
       IOHelper.close(in, null);
     }
     return props;
   } catch (FileNotFoundException e) {
     GLogger.warn("not found log4j.properties, cause:" + e);
     return new Properties();
   } catch (IOException e) {
     GLogger.warn("load log4j.properties occer error, cause:" + e);
     return new Properties();
   }
 }
示例#7
0
 public static String getXMLEncoding(InputStream inputStream)
     throws UnsupportedEncodingException, IOException {
   return getXMLEncoding(IOHelper.toString("UTF-8", inputStream));
 }