Package java.io

Examples of java.io.BufferedWriter


        return;
      }
    }
   
    File dialogFile = new File(dialogSubDir.toString() + File.separator + dialogName);
    BufferedWriter br = new BufferedWriter(new FileWriter(dialogFile));
    br.write(response + "\n");
    br.flush();
    br.close();
  }
View Full Code Here


    // the connection to the HTTP server
    // HttpConnection httpConn = null;

    InputStream is = null;
    BufferedWriter bwrite = null;

    // get host
    host = u.getHost();
    if (host == null) {
      throw new HttpException("no host part in URL found");
    }

    // get address, if not using a proxy
    // if the client runs behind a proxy it is possible, that name
    // resolution for the internet is not possible
    if(! useProxy()) {
      try {
  addr = InetAddress.getByName(host);
      } catch (UnknownHostException e) {
  addr = null;
      }
      if (addr == null) {
  throw new HttpException("host part (" + host + ") does not resolve");
      }
    }

    // get path   
    path = u.getFile();
    if (path.equals("")) {
      path = "/";
    }
    // replace spaces
    path=path.replaceAll(" ","%20");

    // get protocol and port
    port = u.getPort();
    protocol = u.getProtocol().toLowerCase();
    if (protocol.equals("http")) {
      if (port == -1) {
  port = DEFAULT_HTTPPORT;
      }
    } else if (protocol.equals("https")) {
      if (port == -1) {
  port = DEFAULT_HTTPSPORT;
      }
      secureConnection=true;
    } else {
      throw new HttpException("protocol " + protocol + " not supported");
    }

    // if using the proxy, request path is the whole URL, otherwise only
    // the path part of the URL
    if (useProxy() && (! secureConnection)) {
      requestPath="http://"+host+path;
    } else {
      requestPath=path;
    }

    // get user info
    userinfo = u.getUserInfo();
    if (userinfo != null) {
     if (userinfo.equals("")) {
      userinfo=null;
     } else {
      // Store user info for this host
      userInfos.setProperty(host,userinfo);
     }
    } else {
     // do we hae a stored user info?
     userinfo=userInfos.getProperty(host);
    }


    if (callback != null) {
      callback.setHttpToolDocUrl(u.toString());
      callback.setHttpToolStatus(STATUS_CONNECTING);
    }

    // okay, we got all needed information, try to connect to the host
    try {
    if (httpConn == null) {
        // connect and initialize streams
        // timeout is stored in seconds in HttpTool, but
        // HttpConnection uses milliseconds
        if (secureConnection) {
      HttpsHelper helper = new HttpsHelper(proxyAddr,proxyPort,useProxy());
      httpConn = helper.createHttpsConnection(host,port);
        } else {
      if (useProxy()) {
        httpConn = HttpConnection.createConnection(proxyAddr,
                     proxyPort,
                     socketTimeout*1000);
      } else {
        httpConn = HttpConnection.createConnection(addr,
                     port,
                     socketTimeout*1000);
      }
        }
    }
 
      is = new LimitedBandwidthStream(
    new BufferedInputStream(httpConn.getInputStream(), 256),
          bandwidth);
      bwrite = new BufferedWriter(
           new OutputStreamWriter(httpConn.getOutputStream()));

      if (callback != null) {
  callback.setHttpToolStatus(STATUS_CONNECTED);
      }


      // write HTTP request
      // get or post ?
      if (method == HttpConstants.GET) {
  bwrite.write("GET ");
  bwrite.write(requestPath);
  if ((parameters != null)
      && (! parameters.equals(""))) {
    bwrite.write("?");
    bwrite.write(parameters);
  }

      } else if (method == HttpConstants.POST) {
  bwrite.write("POST " + requestPath);
      } else {
  throw new HttpException("HTTP method " + method + " not supported");
      }

      // last part of request line
      bwrite.write(" ");
      bwrite.write(HTTP_VERSION);
      bwrite.write("\r\n");

      // Referer header only if defined
      if (referer != null) {
  bwrite.write("Referer: " + referer + "\r\n");
      }

      // if cookies are enabled, write a Cookie: header
      if (cookiesEnabled) {
  String cookieString = cookieManager.cookiesForURL(u);
  if (cookieString != null) {
    bwrite.write("Cookie: ");
    bwrite.write(cookieString);
    bwrite.write("\r\n");
    log.debug("Cookie request header: "+cookieString);
  }
      }

      // Write other headers
      bwrite.write("Host: " + host + "\r\n");
      bwrite.write("User-Agent: " + agentName + "\r\n");
      bwrite.write("Accept: */*\r\n");
      if (ntlmAuthorizationInfo == null) {
        bwrite.write("Connection: close\r\n");
      } else {
    bwrite.write("Connection: keep-alive\r\n");
      }

      // Write "From:" header only if a fromAddress is defined
      if (fromAddress != null) {
  bwrite.write("From: "+fromAddress+"\r\n");
      }

      // if we have username and password, lets write an Authorization
      // header
      if (userinfo != null) {
  // special hack to support usernames with "@"
  // TO DO: find a better solution for this problem
  userinfo = userinfo.replace('%','@');
  bwrite.write("Authorization: Basic ");
  bwrite.write(Base64.encode(userinfo));
  bwrite.write("\r\n");
        log.debug(userinfo);
       
      }
     
      if (ntlmAuthorizationInfo != null) {
    bwrite.write("Authorization: NTLM ");
    bwrite.write(ntlmAuthorizationInfo);
    bwrite.write("\r\n");
      }
     

      // if there is a "If-Modified-Since" date, also write this header
      if (modifyDate != null) { 
  String dateStr = df.format(modifyDate);

  bwrite.write("If-Modified-Since: ");
  bwrite.write(dateStr);
  bwrite.write("\r\n");
  log.debug("If-Modified-Since header: "+dateStr)
      }

      // for a POST request we also need a content-length header
      if (method == HttpConstants.POST) {
  bwrite.write("Content-Type: application/x-www-form-urlencoded\r\n");
  bwrite.write("Content-Length: "+parameters.length()+"\r\n");
      }

      // finished headers
      bwrite.write("\r\n");
      // if this is a POST request, we have to add the POST parameters
      if (method == HttpConstants.POST) {
  bwrite.write(parameters);
      }
      bwrite.flush();
     
      if (callback != null) {
  callback.setHttpToolStatus(STATUS_RETRIEVING);
      }

View Full Code Here

     * This method writes the solution to a file.
     */
    public void writeToFile(Collection<? extends Event> optTIs, String fileName)
            throws IOException {

        BufferedWriter writer = new BufferedWriter(new FileWriter(fileName + ".sln"));
        logger.debug("Write to file: " + fileName + ".sln");

        try {
            for (Event ti : optTIs) {
                Location r = ti.getLocation();
                assert ti.getStart() == -1 ? r == null : true;

                // Hint: If a Event was not assigned to the timetable the
                // starttime is null and the room is null, so this is fine:
                // writer.write("-1 -1");
                writer.write(ti.getStart() + " " + indexOfLocation(r) + "\n");
            }
        } finally {
            writer.close();
        }
    }
View Full Code Here

      public void actionPerformed(ActionEvent evt){
        JFileChooser filechooser = new JFileChooser();
        int result = filechooser.showSaveDialog(dialog);
        if (result == JFileChooser.APPROVE_OPTION) {
          try {
            BufferedWriter writer = new BufferedWriter(
                                      new FileWriter(
                                        filechooser.getSelectedFile()));
            writer.write(textData.getText());
            writer.flush();
            writer.close();
            JOptionPane.showMessageDialog(
              dialog,
              "Output successfully saved to file '"
              + filechooser.getSelectedFile() + "'!",
              "Information",
View Full Code Here

   * @param append  whether to append the message or not
   * @return    true if writing was successful
   */
  public static boolean writeToFile(String filename, String message, boolean append) {
    boolean    result;
    BufferedWriter  writer;
   
    try {
      writer = new BufferedWriter(new FileWriter(filename, append));
      writer.write(message);
      writer.newLine();
      writer.flush();
      writer.close();
      result = true;
    }
    catch (Exception e) {
      result = false;
    }
View Full Code Here

   * Appends the given string to the log file (without new line!).
   *
   * @param s    the string to append
   */
  protected void append(String s) {
    BufferedWriter  writer;
  
    if (m_LogFile == null)
      return;
   
    // append output to file
    try {
      writer = new BufferedWriter(new FileWriter(m_LogFile, true));
      writer.write(s);
      writer.flush();
      writer.close();
    }
    catch (Exception e) {
      // ignored
    }
  }
View Full Code Here

   * @param output the output stream.
   * @throws IOException throws an IOException if destination cannot be set
   */
  public void setDestination(OutputStream output) throws IOException {

    m_writer = new BufferedWriter(new OutputStreamWriter(output));
  }
View Full Code Here

    /**
     * Get <code>BufferedWriter</code>.
     */
    public final static BufferedWriter getBufferedWriter(Writer wr) {
        BufferedWriter bw = null;
        if (wr instanceof java.io.BufferedWriter) {
            bw = (BufferedWriter) wr;
        } else {
            bw = new BufferedWriter(wr);
        }
        return bw;
    }
View Full Code Here

     * whatever the encoding
     * @throws IOException on error
     */
    public static void exportToXML(HashMap names, OutputStream out, String encoding, boolean onlyASCII) throws IOException {
        String jenc = IanaEncodings.getJavaEncoding(encoding);
        Writer wrt = new BufferedWriter(new OutputStreamWriter(out, jenc));
        exportToXML(names, wrt, encoding, onlyASCII);
    }
View Full Code Here

    return folder;
  }
 
  public static void copyWithVariableSubstitution(File source, File target, Map<String,String> variables, String varPrefix, String varSuffix) throws IOException {
    BufferedReader reader = null;
    BufferedWriter writer = null;
    try {
      reader = new BufferedReader(new InputStreamReader(new FileInputStream(source)));
      writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(target)));
      String line = reader.readLine();
      while (line != null) {
        line = substitute(line, variables, varPrefix, varSuffix);
        writer.write(line + "\n");
        line = reader.readLine();
      }
    } catch (IOException e) {
      throw e;
    } finally {
      if (writer != null) {
        try {
          writer.close();   
        } catch (IOException e) {         
        }
      }
      if (reader != null) {
        try {
View Full Code Here

TOP

Related Classes of java.io.BufferedWriter

Copyright © 2015 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.