List of usage examples for java.io DataOutputStream close
@Override public void close() throws IOException
From source file:com.hdfs.concat.crush.Crush.java
void writeDirs() throws IOException { print(Verbosity.INFO, "\n\nUsing temporary directory " + tmpDir.toUri().getPath()); FileStatus status = fs.getFileStatus(srcDir); Path tmpIn = new Path(tmpDir, "in"); bucketFiles = new Path(tmpIn, "dirs"); partitionMap = new Path(tmpIn, "partition-map"); counters = new Path(tmpIn, "counters"); skippedFiles = new HashSet<String>(); /*/* w ww . j a v a 2 s .c o m*/ * Prefer the path returned by the status because it is always fully qualified. */ List<Path> dirs = asList(status.getPath()); Text key = new Text(); Text value = new Text(); Writer writer = SequenceFile.createWriter(fs, job, bucketFiles, Text.class, Text.class, CompressionType.BLOCK); int numPartitions = Integer.parseInt(job.get("mapred.reduce.tasks")); Bucketer partitionBucketer = new Bucketer(numPartitions, 0, false); partitionBucketer.reset("partition-map"); jobCounters = new Counters(); try { while (!dirs.isEmpty()) { List<Path> nextLevel = new LinkedList<Path>(); for (Path dir : dirs) { jobCounters.incrCounter(MapperCounter.DIRS_FOUND, 1); print(Verbosity.INFO, "\n\n" + dir.toUri().getPath()); FileStatus[] contents = fs.listStatus(dir, new PathFilter() { @Override public boolean accept(Path testPath) { if (ignoredFiles == null) return true; ignoredFiles.reset(testPath.toUri().getPath()); return !ignoredFiles.matches(); } }); if (contents == null || contents.length == 0) { print(Verbosity.INFO, " is empty"); jobCounters.incrCounter(MapperCounter.DIRS_SKIPPED, 1); } else { List<FileStatus> crushables = new ArrayList<FileStatus>(contents.length); Set<String> uncrushedFiles = new HashSet<String>(contents.length); long crushableBytes = 0; /* * Queue sub directories for subsequent inspection and examine the files in this directory. */ for (FileStatus content : contents) { Path path = content.getPath(); if (content.isDir()) { nextLevel.add(path); } else { boolean changed = uncrushedFiles.add(path.toUri().getPath()); assert changed : path.toUri().getPath(); long fileLength = content.getLen(); if (fileLength <= maxEligibleSize) { crushables.add(content); crushableBytes += fileLength; } } } /* * We found a directory with data in it. Make sure we know how to name the crush output file and then increment the * number of files we found. */ if (!uncrushedFiles.isEmpty()) { if (-1 == findMatcher(dir)) { throw new IllegalArgumentException( "Could not find matching regex for directory: " + dir); } jobCounters.incrCounter(MapperCounter.FILES_FOUND, uncrushedFiles.size()); } if (0 == crushableBytes) { print(Verbosity.INFO, " has no crushable files"); jobCounters.incrCounter(MapperCounter.DIRS_SKIPPED, 1); } else { /* * We found files to consider for crushing. */ long nBlocks = crushableBytes / dfsBlockSize; if (nBlocks * dfsBlockSize != crushableBytes) { nBlocks++; } /* * maxFileBlocks will be huge in v1 mode, which will lead to one bucket per directory. */ long dirBuckets = nBlocks / maxFileBlocks; if (dirBuckets * maxFileBlocks != nBlocks) { dirBuckets++; } if (dirBuckets > Integer.MAX_VALUE) { throw new AssertionError("Too many buckets: " + dirBuckets); } Bucketer directoryBucketer = new Bucketer((int) dirBuckets, excludeSingleFileDirs); directoryBucketer.reset(getPathPart(dir)); for (FileStatus file : crushables) { directoryBucketer.add(new FileStatusHasSize(file)); } List<Bucket> crushFiles = directoryBucketer.createBuckets(); if (crushFiles.isEmpty()) { jobCounters.incrCounter(MapperCounter.DIRS_SKIPPED, 1); } else { nBuckets += crushFiles.size(); jobCounters.incrCounter(MapperCounter.DIRS_ELIGIBLE, 1); print(Verbosity.INFO, " => " + crushFiles.size() + " output files"); /* * Write out the mapping between a bucket and a file. */ for (Bucket crushFile : crushFiles) { String bucketId = crushFile.name(); List<String> bucketFiles = crushFile.contents(); print(Verbosity.INFO, format("\n Output %s will include %,d input bytes from %,d files", bucketId, crushFile.size(), bucketFiles.size())); key.set(bucketId); for (String f : bucketFiles) { boolean changed = uncrushedFiles.remove(f); assert changed : f; pathMatcher.reset(f); pathMatcher.matches(); value.set(pathMatcher.group(5)); writer.append(key, value); /* * Print the input file with four leading spaces. */ print(Verbosity.VERBOSE, "\n " + f); } jobCounters.incrCounter(MapperCounter.FILES_ELIGIBLE, bucketFiles.size()); partitionBucketer.add(crushFile); } } } if (!uncrushedFiles.isEmpty()) { print(Verbosity.INFO, "\n\n Skipped " + uncrushedFiles.size() + " files"); for (String uncrushed : uncrushedFiles) { print(Verbosity.VERBOSE, "\n " + uncrushed); } jobCounters.incrCounter(MapperCounter.FILES_SKIPPED, uncrushedFiles.size()); } skippedFiles.addAll(uncrushedFiles); } } dirs = nextLevel; } } finally { try { writer.close(); } catch (Exception e) { LOG.error("Trapped exception during close: " + bucketFiles, e); } } /* * Now that we have processed all the directories, write the partition map. */ List<Bucket> partitions = partitionBucketer.createBuckets(); assert partitions.size() <= numPartitions; writer = SequenceFile.createWriter(fs, job, partitionMap, Text.class, IntWritable.class); IntWritable partNum = new IntWritable(); try { for (Bucket partition : partitions) { String partitionName = partition.name(); partNum.set(Integer.parseInt(partitionName.substring(partitionName.lastIndexOf('-') + 1))); for (String bucketId : partition.contents()) { key.set(bucketId); writer.append(key, partNum); } } } finally { try { writer.close(); } catch (Exception e) { LOG.error("Trapped exception during close: " + partitionMap, e); } } DataOutputStream countersStream = fs.create(this.counters); try { jobCounters.write(countersStream); } finally { try { countersStream.close(); } catch (Exception e) { LOG.error("Trapped exception during close: " + partitionMap, e); } } }
From source file:eu.eexcess.partnerwizard.webservice.WizardRESTService.java
public String cmdExecute(ArrayList<String> commands) { Process shell = null;//from w ww. jav a2s. c om DataOutputStream out = null; BufferedReader in = null; StringBuilder processOutput = new StringBuilder(); processOutput.append(new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime())) .append("\n"); try { shell = Runtime.getRuntime().exec("cmd");//su if needed out = new DataOutputStream(shell.getOutputStream()); in = new BufferedReader(new InputStreamReader(shell.getInputStream())); // Executing commands for (String command : commands) { LOGGER.info("executing:\n" + command); out.writeBytes(command + "\n"); out.flush(); } out.writeBytes("exit\n"); out.flush(); String line; while ((line = in.readLine()) != null) { processOutput.append(line).append("\n"); } //LOGGER.info("result:\n" + processOutput); processOutput.append(new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime())) .append("\n"); String output = processOutput.toString(); shell.waitFor(); LOGGER.info(processOutput.toString()); LOGGER.info("finished!"); return output; } catch (Exception e) { } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } // shell.destroy(); } catch (Exception e) { // hopeless } } return ""; }
From source file:com.wso2telco.dep.mediator.RequestExecutor.java
/** * Make tokenrequest.//w w w . j a va 2 s .com * * @param tokenurl * the tokenurl * @param urlParameters * the url parameters * @param authheader * the authheader * @return the string */ protected String makeTokenrequest(String tokenurl, String urlParameters, String authheader, MessageContext messageContext) { ICallresponse icallresponse = null; String retStr = ""; URL neturl; HttpURLConnection connection = null; log.info("url : " + tokenurl + " | urlParameters : " + urlParameters + " | authheader : " + authheader + " Request ID: " + UID.getRequestID(messageContext)); if ((tokenurl != null && tokenurl.length() > 0) && (urlParameters != null && urlParameters.length() > 0) && (authheader != null && authheader.length() > 0)) { try { byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8); int postDataLength = postData.length; URL url = new URL(tokenurl); connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setInstanceFollowRedirects(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Authorization", authheader); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("charset", "utf-8"); connection.setRequestProperty("Content-Length", Integer.toString(postDataLength)); connection.setUseCaches(false); DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.write(postData); wr.flush(); wr.close(); if ((connection.getResponseCode() != 200) && (connection.getResponseCode() != 201) && (connection.getResponseCode() != 400) && (connection.getResponseCode() != 401)) { log.info("connection.getResponseMessage() : " + connection.getResponseMessage()); throw new RuntimeException("Failed : HTTP error code : " + connection.getResponseCode()); } InputStream is = null; if ((connection.getResponseCode() == 200) || (connection.getResponseCode() == 201)) { is = connection.getInputStream(); } else { is = connection.getErrorStream(); } BufferedReader br = new BufferedReader(new InputStreamReader(is)); String output; while ((output = br.readLine()) != null) { retStr += output; } br.close(); } catch (Exception e) { log.error("[WSRequestService ], makerequest, " + e.getMessage(), e); return null; } finally { if (connection != null) { connection.disconnect(); } } } else { log.error("Token refresh details are invalid."); } return retStr; }
From source file:com.att.android.arodatacollector.main.AROCollectorService.java
/** * Logs the output "dumpsys alarm" to the provided filename. * * Output contains list of scheduled alarms & historical alarm statistics since * device bootup.// w w w .j a v a 2 s. c o m * * Note: On Froyo & older Android OS versions dumpsys output consists of absolute * times (in ms), but on later versions is relative in the format 1h1m20s32ms. * * Requires root permission. * * @param file Alarm log output filename */ private void getAlarmInfo(String file) { Process sh = null; DataOutputStream os = null; try { sh = Runtime.getRuntime().exec("su"); os = new DataOutputStream(sh.getOutputStream()); String Command = "dumpsys alarm > " + mApp.getTcpDumpTraceFolderName() + file + "\n"; os.writeBytes(Command); os.flush(); } catch (IOException e) { Log.e(TAG, "exception in getAlarmInfo ", e); } finally { try { os.close(); } catch (IOException e) { Log.e(TAG, "exception in getAlarmInfo closing output stream ", e); } } }
From source file:br.org.indt.ndg.servlets.PostSurveys.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { InputStreamReader dis = new InputStreamReader(request.getInputStream(), "UTF-8"); DataOutputStream dos = new DataOutputStream(response.getOutputStream()); if (dis != null) { BufferedReader reader = new BufferedReader(dis); StringBuffer tempBuffer = new StringBuffer(); String line = null;// ww w.jav a2s . c o m if (validateUser(request.getParameter("user"), request.getParameter("psw"))) { while ((line = reader.readLine()) != null) { tempBuffer.append(line + '\n'); } try { msmBD.postSurvey(request.getParameter("user"), tempBuffer, createTransactionLogVO(request), false); dos.writeBytes(SUCCESS); } catch (MSMApplicationException e) { dos.writeBytes(FAILURE); e.printStackTrace(); } catch (MSMSystemException e) { dos.writeBytes(FAILURE); e.printStackTrace(); } } else { dos.writeBytes(USERINVALID); } reader.close(); dos.close(); } else { dos.writeBytes(FAILURE); log.info("Failed processing stream from " + request.getRemoteAddr()); } }
From source file:com.smartbear.collaborator.issue.IssueRest.java
/** * Uploads zip file of raw files to Collaborator server * // ww w .jav a2s . c o m * @param targetZipFile * @throws Exception */ private void uploadRawFilesToCollab(java.io.File targetZipFile) throws Exception { HttpURLConnection httpUrlConnection = null; try { String crlf = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; URL url = new URL(configModel.getUrl() + URI_COLAB_UPLOAD); httpUrlConnection = (HttpURLConnection) url.openConnection(); httpUrlConnection.setUseCaches(false); httpUrlConnection.setDoOutput(true); httpUrlConnection.setRequestMethod("POST"); httpUrlConnection.setRequestProperty("Connection", "Keep-Alive"); httpUrlConnection.setRequestProperty("Cache-Control", "no-cache"); httpUrlConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); String loginCookie = "CodeCollaboratorLogin=" + configModel.getLogin(); String ticketCookie = "CodeCollaboratorTicketId=" + configModel.getAuthTicket(); httpUrlConnection.setRequestProperty("Cookie", loginCookie + "," + ticketCookie); DataOutputStream request = new DataOutputStream(httpUrlConnection.getOutputStream()); request.writeBytes(twoHyphens + boundary + crlf); request.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + targetZipFile.getName() + "\"" + crlf); request.writeBytes("Content-Type: application/x-zip-compressed" + crlf); request.writeBytes(crlf); InputStream fileInStream = new FileInputStream(targetZipFile); request.write(IOUtils.toByteArray(fileInStream)); request.writeBytes(crlf); request.writeBytes(twoHyphens + boundary + twoHyphens + crlf); request.flush(); request.close(); if (httpUrlConnection.getResponseCode() != HttpURLConnection.HTTP_OK) { throw new Exception(); } } catch (Exception e) { LOGGER.error(e); throw new Exception( "Can't upload raw versions to Collaborator Server. Check plugin collaborator configuration (url, login, password).", e); } finally { if (httpUrlConnection != null) { httpUrlConnection.disconnect(); } } }
From source file:com.att.android.arodatacollector.main.AROCollectorService.java
/** * Starts collecting kernel log/* w w w .j av a 2 s.c om*/ * * Requires root permission. * * @param file kernel log output filename */ private void startDmesg(String file) { Process sh = null; DataOutputStream os = null; try { sh = Runtime.getRuntime().exec("su"); os = new DataOutputStream(sh.getOutputStream()); // Appending String Command = "cat /proc/kmsg >> " + mApp.getTcpDumpTraceFolderName() + file + "\n"; os.writeBytes(Command); os.flush(); } catch (IOException e) { Log.e(TAG, "exception in startDmesg ", e); } finally { try { os.close(); } catch (IOException e) { Log.e(TAG, "exception in startDmesg closing output stream ", e); } } }
From source file:com.wso2telco.dep.mediator.RequestExecutor.java
/** * Make delete request.// w ww. ja v a 2 s . c om * * @param operatorendpoint * the operatorendpoint * @param url * the url * @param requestStr * the request str * @param auth * the auth * @param messageContext * the message context * @return the string */ public String makeDeleteRequest(OperatorEndpoint operatorendpoint, String url, String requestStr, boolean auth, MessageContext messageContext, boolean inclueHeaders) { int statusCode = 0; String retStr = ""; URL neturl; HttpURLConnection connection = null; try { // String Authtoken = AccessToken; // //FileUtil.getApplicationProperty("wow.api.bearer.token"); // DefaultHttpClient httpClient = new DefaultHttpClient(); String encurl = (requestStr != null) ? url + requestStr : url; neturl = new URL(encurl); connection = (HttpURLConnection) neturl.openConnection(); connection.setRequestMethod("DELETE"); connection.setRequestProperty("Content-Type", "application/json"); if (auth) { connection.setRequestProperty("Authorization", "Bearer " + getAccessToken(operatorendpoint.getOperator(), messageContext)); // Add JWT token header org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext) .getAxis2MessageContext(); Object headers = axis2MessageContext .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS); if (headers != null && headers instanceof Map) { Map headersMap = (Map) headers; String jwtparam = (String) headersMap.get("x-jwt-assertion"); if (jwtparam != null) { connection.setRequestProperty("x-jwt-assertion", jwtparam); } } } if (inclueHeaders) { org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext) .getAxis2MessageContext(); Object headers = axis2MessageContext .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS); if (headers != null && headers instanceof Map) { Map headersMap = (Map) headers; Iterator it = headersMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); connection.setRequestProperty((String) entry.getKey(), (String) entry.getValue()); // avoids a // ConcurrentModificationException } } } connection.setUseCaches(false); log.info("Southbound Request URL: " + connection.getRequestMethod() + " " + connection.getURL() + " Request ID: " + UID.getRequestID(messageContext)); if (log.isDebugEnabled()) { log.debug("Southbound Request Headers: " + connection.getRequestProperties()); } log.info("Southbound Request Body: " + requestStr + " Request ID: " + UID.getRequestID(messageContext)); if (requestStr != null) { connection.setDoOutput(true); DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(requestStr); wr.flush(); wr.close(); } statusCode = connection.getResponseCode(); log.info("response code: " + statusCode); if ((statusCode != 200) && (statusCode != 201) && (statusCode != 400) && (statusCode != 401) && (statusCode != 204)) { throw new RuntimeException("Failed : HTTP error code : " + statusCode); } InputStream is = null; if ((statusCode == 200) || (statusCode == 201) || (statusCode == 204)) { is = connection.getInputStream(); } else { is = connection.getErrorStream(); } BufferedReader br = new BufferedReader(new InputStreamReader(is)); String output; while ((output = br.readLine()) != null) { retStr += output; } br.close(); log.info("Southbound Response Status: " + statusCode + " " + connection.getResponseMessage() + " Request ID: " + UID.getRequestID(messageContext)); if (log.isDebugEnabled()) { log.debug("Southbound Response Headers: " + connection.getHeaderFields()); } log.info("Southbound Response Body: " + retStr + " Request ID: " + UID.getRequestID(messageContext)); } catch (Exception e) { log.error("[WSRequestService ], makerequest, " + e.getMessage(), e); return null; } finally { if (connection != null) { connection.disconnect(); } } return retStr; }
From source file:edu.pdx.cecs.orcycle.NoteUploader.java
boolean uploadOneNote(long noteId) { boolean result = false; final String postUrl = mCtx.getResources().getString(R.string.post_url); try {// w w w .j a v a 2s .c o m JSONArray jsonNoteResponses = getNoteResponsesJSON(noteId); URL url = new URL(postUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); // Allow Inputs conn.setDoOutput(true); // Allow Outputs conn.setUseCaches(false); // Don't use a Cached Copy conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("ENCTYPE", "multipart/form-data"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); conn.setRequestProperty("Cycleatl-Protocol-Version", "4"); DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); JSONObject jsonNote; if (null != (jsonNote = getNoteJSON(noteId))) { try { String deviceId = userId; dos.writeBytes(notesep + ContentField("note") + jsonNote.toString() + "\r\n"); dos.writeBytes( notesep + ContentField("version") + String.valueOf(kSaveNoteProtocolVersion) + "\r\n"); dos.writeBytes(notesep + ContentField("device") + deviceId + "\r\n"); dos.writeBytes(notesep + ContentField("noteResponses") + jsonNoteResponses.toString() + "\r\n"); if (null != imageData) { dos.writeBytes(notesep + "Content-Disposition: form-data; name=\"file\"; filename=\"" + deviceId + ".jpg\"\r\n" + "Content-Type: image/jpeg\r\n\r\n"); dos.write(imageData); dos.writeBytes("\r\n"); } dos.writeBytes(notesep); dos.flush(); } catch (Exception ex) { Log.e(MODULE_TAG, ex.getMessage()); return false; } finally { dos.close(); } int serverResponseCode = conn.getResponseCode(); String serverResponseMessage = conn.getResponseMessage(); // JSONObject responseData = new JSONObject(serverResponseMessage); Log.v("Jason", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode); if (serverResponseCode == 201 || serverResponseCode == 202) { mDb.open(); mDb.updateNoteStatus(noteId, NoteData.STATUS_SENT); mDb.close(); result = true; } } else { result = false; } } catch (IllegalStateException ex) { Log.e(MODULE_TAG, ex.getMessage()); return false; } catch (IOException ex) { Log.e(MODULE_TAG, ex.getMessage()); return false; } catch (JSONException ex) { Log.e(MODULE_TAG, ex.getMessage()); return false; } finally { NoteUploader.setPending(noteId, false); } return result; }