Exemplo n.º 1
0
  public static final int writeStreamToFile(InputStream in, File file, long maxSize)
      throws IOException {
    int countByte = 0;

    if (file.getParentFile() != null && !file.getParentFile().exists()) {
      file.getParentFile().mkdirs();
    }

    if (!file.exists()) {
      file.createNewFile();
    }
    OutputStream out = null;
    try {
      out = new FileOutputStream(file);
      countByte = writeStreamToStream(in, out, maxSize);
      if (countByte < 0) {
        ResourceHelper.closeResource(out);
        file.delete();
        return -1;
      }
    } finally {
      ResourceHelper.closeResource(out);
    }

    return countByte;
  }
Exemplo n.º 2
0
  public static final File writeFileItemToFolder(
      FileItem fileItem, File folder, boolean overwrite, boolean rename) throws IOException {
    if (!folder.isDirectory()) {
      return null;
    }
    File file =
        new File(
            URLHelper.mergePath(
                folder.getAbsolutePath(),
                StringHelper.createFileName(StringHelper.getFileNameFromPath(fileItem.getName()))));

    if (!file.exists()) {
      file.createNewFile();
    } else {
      if (!overwrite && !rename) {
        throw new FileExistsException("File already exists.");
      }
      if (rename) {
        file = ResourceHelper.getFreeFileName(file);
      }
    }
    InputStream in = null;
    try {
      in = fileItem.getInputStream();
      writeStreamToFile(in, file);
      return file;
    } catch (IOException e) {
      ResourceHelper.closeResource(in);
      file.delete();
      throw e;
    } finally {
      ResourceHelper.closeResource(in);
    }
  }
Exemplo n.º 3
0
 public static final void appendStringToFile(File file, String line) throws IOException {
   Writer in = new FileWriter(file, true);
   BufferedWriter writer = new BufferedWriter(in);
   writer.write(line);
   writer.newLine();
   closeResource(writer);
 }
Exemplo n.º 4
0
 public static Serializable loadBeanFromXML(File file) throws FileNotFoundException {
   InputStream in = new FileInputStream(file);
   try {
     return loadBeanFromXML(in);
   } finally {
     closeResource(in);
   }
 }
Exemplo n.º 5
0
 public static final int writeFileToStream(File fileIn, OutputStream out) throws IOException {
   InputStream in = null;
   try {
     in = new FileInputStream(fileIn);
     return writeStreamToStream(in, out);
   } finally {
     ResourceHelper.closeResource(in);
   }
 }
Exemplo n.º 6
0
 public static Properties loadProperties(File file) throws IOException {
   Properties properties = new Properties();
   InputStream in = new FileInputStream(file);
   try {
     properties.load(in);
   } finally {
     ResourceHelper.closeResource(in);
   }
   return properties;
 }
Exemplo n.º 7
0
  public static String downloadResourceAsString(URL workURL) throws IOException {
    InputStream in = workURL.openStream();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
      byte[] buffer = new byte[8192];
      int len, b = in.read();
      while (b >= 0) {
        out.write(b);
        len = in.read(buffer);
        out.write(buffer, 0, len);

        b = in.read();
      }
      return new String(out.toByteArray());
    } finally {
      ResourceHelper.closeResource(in);
      ResourceHelper.closeResource(out);
    }
  }
Exemplo n.º 8
0
 public static final void storeCollectionToFile(File file, List<String> lines) throws IOException {
   if (!file.exists()) {
     file.getParentFile().mkdirs();
     file.createNewFile();
   }
   Writer in = new FileWriter(file);
   BufferedWriter writer = new BufferedWriter(in);
   for (String line : lines) {
     writer.write(line);
     writer.newLine();
   }
   closeResource(writer);
 }
Exemplo n.º 9
0
 public static String storeBean(Serializable bean, File file) throws FileNotFoundException {
   OutputStream out = new FileOutputStream(file);
   XMLEncoder encoder = null;
   try {
     encoder = new XMLEncoder(out, ContentContext.CHARACTER_ENCODING, true, 0);
     encoder.writeObject(bean);
     encoder.flush();
   } finally {
     closeResource(out);
     if (encoder != null) {
       encoder.close();
     }
   }
   return null;
 }
Exemplo n.º 10
0
  public static final List<String> loadCollectionFromFile(File file) throws IOException {
    if (!file.exists()) {
      return Collections.EMPTY_LIST;
    }
    List<String> outLines = new LinkedList<String>();
    Reader in = new FileReader(file);

    BufferedReader reader = new BufferedReader(in);
    String line = reader.readLine();
    while (line != null) {
      outLines.add(line);
      line = reader.readLine();
    }
    closeResource(reader);

    return outLines;
  }
Exemplo n.º 11
0
  public static final String loadStringFromStream(InputStream in, Charset encoding)
      throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(outStream);

    BufferedReader reader = new BufferedReader(new InputStreamReader(in, encoding));
    try {
      String line = reader.readLine();
      while (line != null) {
        out.println(line);
        line = reader.readLine();
      }
    } finally {
      ResourceHelper.closeResource(reader);
    }

    out.close();
    return new String(outStream.toByteArray());
  }
Exemplo n.º 12
0
 public static final String loadStringFromFile(File file) throws IOException {
   InputStream in = new FileInputStream(file);
   String content = loadStringFromStream(in, ContentContext.CHARSET_DEFAULT);
   closeResource(in);
   return content;
 }
Exemplo n.º 13
0
  public static String excutePost(
      String targetURL,
      String urlParameters,
      String contentType,
      String lang,
      String user,
      String pwd) {
    URL url;
    HttpURLConnection connection = null;
    BufferedReader rd = null;
    PrintWriter writer = null;

    if (urlParameters == null) {
      urlParameters = "";
    }
    Map<String, String> params = URLHelper.getParams(urlParameters);
    StringBuffer encodedParam = new StringBuffer();
    String sep = "";
    for (Map.Entry<String, String> param : params.entrySet()) {
      encodedParam.append(sep);
      encodedParam.append(param.getKey());
      encodedParam.append("=");
      encodedParam.append(URLEncoder.encode(param.getValue()));
      sep = "&";
    }

    if (urlParameters == null) {
      urlParameters = "";
    }

    try {
      // Create connection
      url = new URL(targetURL);
      connection = (HttpURLConnection) url.openConnection();
      connection.setRequestProperty("Content-Type", contentType);
      connection.setRequestProperty("Content-Language", lang);
      connection.setDoOutput(true);

      // user authentification
      if (user != null && pwd != null) {
        connection.setRequestProperty(
            "Authorization", "Basic " + Base64.encodeBase64((user + ':' + pwd).getBytes()));
      }

      // Send request
      writer = new PrintWriter(connection.getOutputStream());
      writer.write(encodedParam.toString());
      writer.flush();

      // Get Response
      InputStream is = connection.getInputStream();
      String line;
      rd = new BufferedReader(new InputStreamReader(is));
      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
      PrintStream out = new PrintStream(outStream);
      while ((line = rd.readLine()) != null) {
        out.println(line);
      }
      out.close();
      return new String(outStream.toByteArray());
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    } finally {
      closeResource(rd, writer);
      if (connection != null) {
        connection.disconnect();
      }
    }
  }