List of usage examples for java.io BufferedInputStream close
public void close() throws IOException
From source file:com.testmax.util.FileDownLoader.java
public String downloader(WebElement element, String attribute) throws Exception { //Assuming that getAttribute does some magic to return a fully qualified URL String downloadLocation = element.getAttribute(attribute); if (downloadLocation.trim().equals("")) { throw new Exception("The element you have specified does not link to anything!"); }// w w w.ja v a 2s . co m URL downloadURL = new URL(downloadLocation); HttpClient client = new HttpClient(); client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); client.setHostConfiguration(mimicHostConfiguration(downloadURL.getHost(), downloadURL.getPort())); client.setState(mimicCookieState(driver.manage().getCookies())); HttpMethod getRequest = new GetMethod(downloadURL.getPath()); String file_path = downloadPath + downloadURL.getFile().replaceFirst("/|\\\\", ""); FileWriter downloadedFile = new FileWriter(file_path, true); try { int status = client.executeMethod(getRequest); WmLog.getCoreLogger().info("HTTP Status {} when getting '{}'" + status + downloadURL.toExternalForm()); BufferedInputStream in = new BufferedInputStream(getRequest.getResponseBodyAsStream()); int offset = 0; int len = 4096; int bytes = 0; byte[] block = new byte[len]; while ((bytes = in.read(block, offset, len)) > -1) { downloadedFile.write(bytes); } downloadedFile.close(); in.close(); WmLog.getCoreLogger().info("File downloaded to '{}'" + file_path); } catch (Exception Ex) { WmLog.getCoreLogger().error("Download failed: {}", Ex); throw new Exception("Download failed!"); } finally { getRequest.releaseConnection(); } return file_path; }
From source file:org.n52.smartsensoreditor.dao.SOSWebServiceIT.java
@Before public void before() { //Get from propertyFile Properties properties = new Properties(); String userHome = System.getProperty("user.home"); BufferedInputStream stream; try {//from w w w. java 2 s . c om stream = new BufferedInputStream(new FileInputStream(userHome + "/build.properties")); properties.load(stream); stream.close(); if (properties.getProperty("IT_serviceURL") != null) { serviceURL = properties.getProperty("IT_serviceURL"); } if (properties.getProperty("IT_authorizationToken") != null) { authorizationToken = properties.getProperty("IT_authorizationToken"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } parameterMap = new HashMap(); parameterMap.put("procedureId", sensorId); deleteSensor(); insertSensor(); }
From source file:it.geosolutions.tools.compress.file.Extractor.java
/** * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it * /* w ww . j av a2s .co m*/ */ public static void extractBz2(File in_file, File out_file) throws CompressorException { FileOutputStream out = null; BZip2CompressorInputStream zIn = null; FileInputStream fis = null; BufferedInputStream bis = null; try { out = new FileOutputStream(out_file); fis = new FileInputStream(in_file); bis = new BufferedInputStream(fis); /* * int b = bis.read(); if (b != 'B') { throw new * CompressorException("Invalid bz2 file: "+in_file.getAbsolutePath()); } b = * bis.read(); if (b != 'Z') { throw new * CompressorException("Invalid bz2 file: "+in_file.getAbsolutePath()); } */ zIn = new BZip2CompressorInputStream(bis); byte[] buffer = new byte[Conf.getBufferSize()]; int count = 0; do { out.write(buffer, 0, count); count = zIn.read(buffer, 0, buffer.length); } while (count != -1); } catch (IOException ioe) { String msg = "Problem expanding bzip2 " + ioe.getMessage(); throw new CompressorException(msg + in_file.getAbsolutePath()); } finally { try { if (bis != null) bis.close(); } catch (IOException ioe) { throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath()); } try { if (fis != null) fis.close(); } catch (IOException ioe) { throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath()); } try { if (out != null) out.close(); } catch (IOException ioe) { throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath()); } try { if (zIn != null) zIn.close(); } catch (IOException ioe) { throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath()); } } }
From source file:IDlook.java
private void buildGUI() { Container c = getContentPane(); c.setLayout(new FlowLayout()); accountNumberList = new JList(); loadAccounts();/*from www . j a v a 2 s . c o m*/ accountNumberList.setVisibleRowCount(2); JScrollPane accountNumberListScrollPane = new JScrollPane(accountNumberList); //Do Get Account Button getAccountButton = new JButton("Get Account"); getAccountButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { rs.beforeFirst(); while (rs.next()) { if (rs.getString("acc_id").equals(accountNumberList.getSelectedValue())) break; } if (!rs.isAfterLast()) { accountIDText.setText(rs.getString("acc_id")); thumbIDText.setText(rs.getString("thumb_id")); icon = new ImageIcon(rs.getBytes("pic")); createThumbnail(); photographLabel.setIcon(iconThumbnail); } } catch (SQLException selectException) { displaySQLErrors(selectException); } } }); //Do Update Account Button updateAccountButton = new JButton("Update Account"); updateAccountButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { byte[] bytes = new byte[50000]; FileInputStream fs = new FileInputStream(nailFileText.getText()); BufferedInputStream bis = new BufferedInputStream(fs); bis.read(bytes); rs.updateBytes("thumbnail.pic", bytes); rs.updateRow(); bis.close(); accountNumberList.removeAll(); loadAccounts(); } catch (SQLException insertException) { displaySQLErrors(insertException); } catch (Exception generalE) { generalE.printStackTrace(); } } }); //Do insert Account Button insertAccountButton = new JButton("Insert Account"); insertAccountButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { byte[] bytes = new byte[50000]; FileInputStream fs = new FileInputStream(nailFileText.getText()); BufferedInputStream bis = new BufferedInputStream(fs); bis.read(bytes); rs.moveToInsertRow(); rs.updateInt("thumb_id", Integer.parseInt(thumbIDText.getText())); rs.updateInt("acc_id", Integer.parseInt(accountIDText.getText())); rs.updateBytes("pic", bytes); rs.updateObject("sysobject", null); rs.updateTimestamp("ts", new Timestamp(0)); rs.updateTimestamp("act_ts", new Timestamp(new java.util.Date().getTime())); rs.insertRow(); bis.close(); accountNumberList.removeAll(); loadAccounts(); } catch (SQLException insertException) { displaySQLErrors(insertException); } catch (Exception generalE) { generalE.printStackTrace(); } } }); photographLabel = new JLabel(); photographLabel.setHorizontalAlignment(JLabel.CENTER); photographLabel.setVerticalAlignment(JLabel.CENTER); photographLabel.setVerticalTextPosition(JLabel.CENTER); photographLabel.setHorizontalTextPosition(JLabel.CENTER); JPanel first = new JPanel(new GridLayout(4, 1)); first.add(accountNumberListScrollPane); first.add(getAccountButton); first.add(updateAccountButton); first.add(insertAccountButton); accountIDText = new JTextField(15); thumbIDText = new JTextField(15); errorText = new JTextArea(5, 15); errorText.setEditable(false); JPanel second = new JPanel(); second.setLayout(new GridLayout(2, 1)); second.add(thumbIDText); second.add(accountIDText); JPanel third = new JPanel(); third.add(new JScrollPane(errorText)); nailFileText = new JTextField(25); c.add(first); c.add(second); c.add(third); c.add(nailFileText); c.add(photographLabel); setSize(500, 500); show(); }
From source file:IDlookBlob.java
private void buildGUI() { Container c = getContentPane(); c.setLayout(new FlowLayout()); accountNumberList = new JList(); loadAccounts();/* ww w .ja va 2 s. c o m*/ accountNumberList.setVisibleRowCount(2); JScrollPane accountNumberListScrollPane = new JScrollPane(accountNumberList); //Do Get Account Button getAccountButton = new JButton("Get Account"); getAccountButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { rs.beforeFirst(); while (rs.next()) { if (rs.getString("acc_id").equals(accountNumberList.getSelectedValue())) break; } if (!rs.isAfterLast()) { accountIDText.setText(rs.getString("acc_id")); thumbIDText.setText(rs.getString("thumb_id")); Blob b = rs.getBlob("pic"); icon = new ImageIcon(b.getBytes(1L, (int) b.length())); createThumbnail(); photographLabel.setIcon(iconThumbnail); } } catch (SQLException selectException) { displaySQLErrors(selectException); } } }); //Do Update Account Button updateAccountButton = new JButton("Update Account"); updateAccountButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { byte[] bytes = new byte[50000]; FileInputStream fs = new FileInputStream(nailFileText.getText()); BufferedInputStream bis = new BufferedInputStream(fs); bis.read(bytes); rs.updateBytes("thumbnail.pic", bytes); rs.updateRow(); bis.close(); accountNumberList.removeAll(); loadAccounts(); } catch (SQLException insertException) { displaySQLErrors(insertException); } catch (Exception generalE) { generalE.printStackTrace(); } } }); //Do insert Account Button insertAccountButton = new JButton("Insert Account"); insertAccountButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { byte[] bytes = new byte[50000]; FileInputStream fs = new FileInputStream(nailFileText.getText()); BufferedInputStream bis = new BufferedInputStream(fs); bis.read(bytes); rs.moveToInsertRow(); rs.updateInt("thumb_id", Integer.parseInt(thumbIDText.getText())); rs.updateInt("acc_id", Integer.parseInt(accountIDText.getText())); rs.updateBytes("pic", bytes); rs.updateObject("sysobject", null); rs.updateTimestamp("ts", new Timestamp(0)); rs.updateTimestamp("act_ts", new Timestamp(new java.util.Date().getTime())); rs.insertRow(); bis.close(); accountNumberList.removeAll(); loadAccounts(); } catch (SQLException insertException) { displaySQLErrors(insertException); } catch (Exception generalE) { generalE.printStackTrace(); } } }); photographLabel = new JLabel(); photographLabel.setHorizontalAlignment(JLabel.CENTER); photographLabel.setVerticalAlignment(JLabel.CENTER); photographLabel.setVerticalTextPosition(JLabel.CENTER); photographLabel.setHorizontalTextPosition(JLabel.CENTER); JPanel first = new JPanel(new GridLayout(4, 1)); first.add(accountNumberListScrollPane); first.add(getAccountButton); first.add(updateAccountButton); first.add(insertAccountButton); accountIDText = new JTextField(15); thumbIDText = new JTextField(15); errorText = new JTextArea(5, 15); errorText.setEditable(false); JPanel second = new JPanel(); second.setLayout(new GridLayout(2, 1)); second.add(thumbIDText); second.add(accountIDText); JPanel third = new JPanel(); third.add(new JScrollPane(errorText)); nailFileText = new JTextField(25); c.add(first); c.add(second); c.add(third); c.add(nailFileText); c.add(photographLabel); setSize(500, 500); show(); }
From source file:br.univali.celine.lms.utils.zip.Zip.java
public void zip(ArrayList<String> fileList, File destFile, int compressionLevel) throws Exception { if (destFile.exists()) throw new Exception("File " + destFile.getName() + " already exists!!"); int fileLength; byte[] buffer = new byte[4096]; FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream bos = new BufferedOutputStream(fos); ZipOutputStream zipFile = new ZipOutputStream(bos); zipFile.setLevel(compressionLevel);/* w ww . jav a 2 s.c o m*/ for (int i = 0; i < fileList.size(); i++) { FileInputStream fis = new FileInputStream(fileList.get(i)); BufferedInputStream bis = new BufferedInputStream(fis); ZipEntry ze = new ZipEntry(FilenameUtils.getName(fileList.get(i))); zipFile.putNextEntry(ze); while ((fileLength = bis.read(buffer, 0, 4096)) > 0) zipFile.write(buffer, 0, fileLength); zipFile.closeEntry(); bis.close(); } zipFile.close(); }
From source file:net.paissad.jcamstream.utils.FTPUtils.java
/** * Upload files to the FTP server and into the specified directory. * //from w w w . j av a2 s . c o m * @param files * - The files to upload. * @throws IOException * @throws FTPException */ public void uploadFiles(List<File> files) throws IOException, FTPException { for (File aFile : files) { String filename = aFile.getName(); BufferedInputStream bis = null; try { bis = new BufferedInputStream(new FileInputStream(aFile)); this.uploadStream(filename, bis); } finally { if (bis != null) bis.close(); } } }
From source file:com.tesshu.subsonic.client.sample4_music_andmovie.StreamDownloadApplication.java
@Bean protected CommandLineRunner run(RestTemplate restTemplate) throws Exception { return args -> { SearchResult2 result2 = search2.get("e", null, null, null, null, 1, null, null); List<Child> songs = result2.getSongs(); File tmpDirectory = new File(tmpPath); tmpDirectory.mkdir();/*w w w . jav a2s . com*/ int maxBitRate = 256; for (Child song : songs) { streamController.stream(song, maxBitRate, format, null, null, null, null, (subject, inputStream, contentLength) -> { File dir = new File( tmpPath + "/" + song.getPath().replaceAll("([^/]+?)?$", StringUtils.EMPTY)); dir.mkdirs(); File file = new File(tmpPath + "/" + song.getPath().replaceAll("([^.]+?)?$", StringUtils.EMPTY) + format); try { FileOutputStream fos = new FileOutputStream(file); BufferedInputStream reader = new BufferedInputStream(inputStream); byte buf[] = new byte[256]; int len; while ((len = reader.read(buf)) != -1) { fos.write(buf, 0, len); } fos.flush(); fos.close(); reader.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } }); } }; }
From source file:com.LogicTree.app.Florida511.AudioDownloader.java
/** * @param filename/*from w ww . j a v a2 s . c om*/ * @param is */ void createExternalStoragePrivateFile(String filename, InputStream is) { // Create a path where we will place our private file on external // storage. File file = new File(cacheDir, filename); try { BufferedInputStream bi = new BufferedInputStream(is); OutputStream os = new FileOutputStream(file); byte[] data = new byte[16384]; int count; while ((count = bi.read(data)) != -1) { os.write(data, 0, count); } bi.close(); is.close(); os.close(); } catch (IOException e) { // Unable to create file, likely because external storage is // not currently mounted. Log.w("ExternalStorage", "Error writing " + file, e); } }
From source file:components.TumbleItem.java
/** * Load the image for the specified frame of animation. Since * this runs as an applet, we use getResourceAsStream for * efficiency and so it'll work in older versions of Java Plug-in. *//*from w w w. ja va2 s . c o m*/ protected ImageIcon loadImage(int imageNum) { String path = dir + "/T" + imageNum + ".gif"; int MAX_IMAGE_SIZE = 2400; //Change this to the size of //your biggest image, in bytes. int count = 0; BufferedInputStream imgStream = new BufferedInputStream(this.getClass().getResourceAsStream(path)); if (imgStream != null) { byte buf[] = new byte[MAX_IMAGE_SIZE]; try { count = imgStream.read(buf); imgStream.close(); } catch (java.io.IOException ioe) { System.err.println("Couldn't read stream from file: " + path); return null; } if (count <= 0) { System.err.println("Empty file: " + path); return null; } return new ImageIcon(Toolkit.getDefaultToolkit().createImage(buf)); } else { System.err.println("Couldn't find file: " + path); return null; } }