List of usage examples for java.io DataOutputStream close
@Override public void close() throws IOException
From source file:com.netflix.zeno.examples.MultipleImageSerializationExample.java
private byte[] writeSnapshot(FastBlobWriter writer) { /// Create an output stream to somewhere. This can be to a local file on disk /// or directly to the blob destination. The VMS team writes this data directly /// to disk, and then spawns a new thread to upload the data to S3. /// We need to pass a DataOutputStream. Remember that DataOutputStream is not buffered, /// so if you write to a FileOutputStream or other non-buffered source, you likely want to /// make the DataOutputStream wrap a BufferedOutputStream /// (e.g. new DataOutputStream(new BufferedOutputStream(new FileOutputStream(destinationFile)))) ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream outputStream = new DataOutputStream(baos); try {//from w w w. j a v a2 s. c o m /// write the snapshot to the output stream. writer.writeSnapshot(outputStream); } catch (IOException e) { /// the FastBlobWriter throws an IOException if it is /// unable to write to the provided OutputStream } finally { /// it is your responsibility to close the stream. The FastBlobWriter will not do this. try { outputStream.close(); } catch (IOException ignore) { } } byte snapshot[] = baos.toByteArray(); return snapshot; }
From source file:com.bcmcgroup.flare.client.ClientUtil.java
/** * Sends an HTTPS POST request and returns the response * * @param conn the HTTPS connection object * @param payload the payload for the POST request * @return the response from the remote server * *//* ww w.j a va2 s . c om*/ public static int sendPost(HttpsURLConnection conn, String payload) { OutputStream outputStream = null; DataOutputStream wr = null; InputStream is = null; int response = 0; logger.debug("Attempting HTTPS POST..."); try { outputStream = conn.getOutputStream(); wr = new DataOutputStream(outputStream); wr.write(payload.getBytes("UTF-8")); wr.flush(); is = conn.getInputStream(); response = conn.getResponseCode(); } catch (IOException e) { logger.debug("IOException when attempting to send a post message. "); } finally { if (is != null) { try { is.close(); } catch (IOException e) { logger.debug("IOException when attempting to close an input stream. "); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { logger.debug("IOException when attempting to close an output stream. "); } } if (wr != null) { try { wr.close(); } catch (IOException e) { logger.debug("IOException when attempting to close a data output stream. "); } } } logger.debug("HTTPS POST Response: " + response); return response; }
From source file:com.datatorrent.contrib.hdht.HDHTWalManager.java
public void copyPreviousWalFiles(List<PreviousWALDetails> parentWals, Set<PreviousWALDetails> alreadyCopiedWals) { try {/* ww w.j ava 2 s .c om*/ PreviousWALDetails parentWal = parentWals.iterator().next(); // Copy Files to new WAL location for (long i = parentWal.getStartPosition().fileId; i <= parentWal.getEndPosition().fileId; i++) { DataInputStream in = bfs.getInputStream(parentWal.getWalKey(), WAL_FILE_PREFIX + i); DataOutputStream out = bfs.getOutputStream(walKey, WAL_FILE_PREFIX + i); IOUtils.copyLarge(in, out); in.close(); out.close(); } } catch (IOException e) { throw Throwables.propagate(e); } }
From source file:es.yrbcn.graph.weighted.WeightedPageRankPowerMethod.java
/** Computes the next step of the Power Method. *///w ww . ja v a 2 s. co m public void step() throws IOException { double[] oldRank = rank, newRank = previousRank; DoubleArrays.fill(newRank, 0.0); // for each node, calculate its outdegree and redistribute its rank among pointed nodes double accum = 0.0; progressLogger.expectedUpdates = numNodes; progressLogger.start("Iteration " + (++iterationNumber) + "..."); final ArcLabelledNodeIterator nodeIterator = g.nodeIterator(); int i, outdegree, j, n = numNodes; int[] succ; Label[] lab; while (n-- != 0) { i = nodeIterator.nextInt(); outdegree = nodeIterator.outdegree(); if (outdegree == 0 || buckets != null && buckets.get(i)) accum += oldRank[i]; else { j = outdegree; succ = nodeIterator.successorArray(); lab = nodeIterator.labelArray(); while (j-- != 0) { newRank[succ[j]] += (oldRank[i] * lab[j].getFloat()) / sumoutweight[i]; } } progressLogger.update(); } progressLogger.done(); final double accumOverNumNodes = accum / numNodes; final double oneOverNumNodes = 1.0 / numNodes; if (preference != null) if (preferentialAdjustment == null) for (i = numNodes; i-- != 0;) newRank[i] = alpha * newRank[i] + (1 - alpha) * preference.getDouble(i) + alpha * accumOverNumNodes; else for (i = numNodes; i-- != 0;) newRank[i] = alpha * newRank[i] + (1 - alpha) * preference.getDouble(i) + alpha * accum * preferentialAdjustment.getDouble(i); else if (preferentialAdjustment == null) for (i = numNodes; i-- != 0;) newRank[i] = alpha * newRank[i] + (1 - alpha) * oneOverNumNodes + alpha * accumOverNumNodes; else for (i = numNodes; i-- != 0;) newRank[i] = alpha * newRank[i] + (1 - alpha) * oneOverNumNodes + alpha * accum * preferentialAdjustment.getDouble(i); //make the rank just computed the new rank rank = newRank; previousRank = oldRank; // Compute derivatives. n = iterationNumber; if (subset == null) { for (i = 0; i < order.length; i++) { final int k = order[i]; final double alphak = Math.pow(alpha, k); final double nFallingK = Util.falling(n, k); for (j = 0; j < numNodes; j++) derivative[i][j] += nFallingK * (rank[j] - previousRank[j]) / alphak; } } else { for (i = 0; i < order.length; i++) { final int k = order[i]; final double alphak = Math.pow(alpha, k); final double nFallingK = Util.falling(n, k); for (int t : subset) derivative[i][t] += nFallingK * (rank[t] - previousRank[t]) / alphak; } } // Compute coefficients, if required. if (coeffBasename != null) { final DataOutputStream coefficients = new DataOutputStream( new FastBufferedOutputStream(new FileOutputStream(coeffBasename + "-" + (iterationNumber)))); final double alphaN = Math.pow(alpha, n); for (i = 0; i < numNodes; i++) coefficients.writeDouble((rank[i] - previousRank[i]) / alphaN); coefficients.close(); } }
From source file:it.crs4.pydoop.mapreduce.pipes.TaskLog.java
private static synchronized void writeToIndexFile(String logLocation, boolean isCleanup) throws IOException { // To ensure atomicity of updates to index file, write to temporary index // file first and then rename. File tmpIndexFile = getTmpIndexFile(currentTaskid, isCleanup); BufferedOutputStream bos = new BufferedOutputStream(SecureIOUtils.createForWrite(tmpIndexFile, 0644)); DataOutputStream dos = new DataOutputStream(bos); //the format of the index file is //LOG_DIR: <the dir where the task logs are really stored> //STDOUT: <start-offset in the stdout file> <length> //STDERR: <start-offset in the stderr file> <length> //SYSLOG: <start-offset in the syslog file> <length> try {/* w w w . jav a 2s . c o m*/ dos.writeBytes(LogFileDetail.LOCATION + logLocation + "\n" + LogName.STDOUT.toString() + ":"); dos.writeBytes(Long.toString(prevOutLength) + " "); dos.writeBytes(Long.toString(new File(logLocation, LogName.STDOUT.toString()).length() - prevOutLength) + "\n" + LogName.STDERR + ":"); dos.writeBytes(Long.toString(prevErrLength) + " "); dos.writeBytes(Long.toString(new File(logLocation, LogName.STDERR.toString()).length() - prevErrLength) + "\n" + LogName.SYSLOG.toString() + ":"); dos.writeBytes(Long.toString(prevLogLength) + " "); dos.writeBytes(Long.toString(new File(logLocation, LogName.SYSLOG.toString()).length() - prevLogLength) + "\n"); dos.close(); dos = null; } finally { IOUtils.cleanup(LOG, dos); } File indexFile = getIndexFile(currentTaskid, isCleanup); Path indexFilePath = new Path(indexFile.getAbsolutePath()); Path tmpIndexFilePath = new Path(tmpIndexFile.getAbsolutePath()); if (localFS == null) {// set localFS once localFS = FileSystem.getLocal(new Configuration()); } localFS.rename(tmpIndexFilePath, indexFilePath); }
From source file:com.bcmcgroup.flare.client.ClientUtil.java
/** * Sends an HTTPS POST request and returns the response in String format * * @param conn the HTTPS connection object * @param payload the payload for the POST request * @return the response from the remote server in String format * *//* www. ja v a 2 s . c om*/ public static String sendPostGetStringResponse(HttpsURLConnection conn, String payload) { OutputStream outputStream = null; DataOutputStream wr = null; InputStream is = null; String response = ""; logger.debug("Attempting HTTPS POST"); try { outputStream = conn.getOutputStream(); wr = new DataOutputStream(outputStream); wr.write(payload.getBytes("UTF-8")); wr.flush(); is = conn.getInputStream(); response = IOUtils.toString(is, "UTF-8"); } catch (IOException e) { logger.debug("IOException when attempting to send a post message. "); } finally { if (is != null) { try { is.close(); } catch (IOException e) { logger.debug("IOException when attempting to close an input stream. "); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { logger.debug("IOException when attempting to close an output stream. "); } } if (wr != null) { try { wr.close(); } catch (IOException e) { logger.debug("IOException when attempting to close a data output stream. "); } } } return response; }
From source file:MixedRecordEnumerationExample.java
public void commandAction(Command command, Displayable displayable) { if (command == exit) { destroyApp(true);/* ww w . j ava 2s .c om*/ notifyDestroyed(); } else if (command == start) { try { recordstore = RecordStore.openRecordStore("myRecordStore", true); } catch (Exception error) { alert = new Alert("Error Creating", error.toString(), null, AlertType.WARNING); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } try { byte[] outputRecord; String outputString[] = { "First Record", "Second Record", "Third Record" }; int outputInteger[] = { 15, 10, 5 }; boolean outputBoolean[] = { true, false, true }; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); DataOutputStream outputDataStream = new DataOutputStream(outputStream); for (int x = 0; x < 3; x++) { outputDataStream.writeUTF(outputString[x]); outputDataStream.writeBoolean(outputBoolean[x]); outputDataStream.writeInt(outputInteger[x]); outputDataStream.flush(); outputRecord = outputStream.toByteArray(); recordstore.addRecord(outputRecord, 0, outputRecord.length); } outputStream.reset(); outputStream.close(); outputDataStream.close(); } catch (Exception error) { alert = new Alert("Error Writing", error.toString(), null, AlertType.WARNING); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } try { StringBuffer buffer = new StringBuffer(); byte[] byteInputData = new byte[300]; ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData); DataInputStream inputDataStream = new DataInputStream(inputStream); recordEnumeration = recordstore.enumerateRecords(null, null, false); while (recordEnumeration.hasNextElement()) { recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0); buffer.append(inputDataStream.readUTF()); buffer.append("\n"); buffer.append(inputDataStream.readBoolean()); buffer.append("\n"); buffer.append(inputDataStream.readInt()); buffer.append("\n"); alert = new Alert("Reading", buffer.toString(), null, AlertType.WARNING); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } inputStream.close(); } catch (Exception error) { alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } try { recordstore.closeRecordStore(); } catch (Exception error) { alert = new Alert("Error Closing", error.toString(), null, AlertType.WARNING); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } if (RecordStore.listRecordStores() != null) { try { RecordStore.deleteRecordStore("myRecordStore"); recordEnumeration.destroy(); } catch (Exception error) { alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } } } }
From source file:com.datatorrent.contrib.hdht.HDHTWalManager.java
/** * Restore state of wal just after last checkpoint. The Apex platform will * resend tuple after last operator checkpoint to the WAL, this will result in * duplicate tuples in WAL, if we don't restore the WAL just after checkpoint * state.//from w w w . j ava 2 s . c om */ private void truncateWal(WalPosition pos) throws IOException { if (pos.offset == 0) { return; } logger.info("recover wal file {}, data valid till offset {}", pos.fileId, pos.offset); DataInputStream in = bfs.getInputStream(walKey, WAL_FILE_PREFIX + pos.fileId); DataOutputStream out = bfs.getOutputStream(walKey, WAL_FILE_PREFIX + pos.fileId + "-truncate"); IOUtils.copyLarge(in, out, 0, pos.offset); in.close(); out.close(); bfs.rename(walKey, WAL_FILE_PREFIX + pos.fileId + "-truncate", WAL_FILE_PREFIX + pos.fileId); }
From source file:com.ebay.nest.io.sede.lazy.LazyUtils.java
/** * Write out a binary representation of a PrimitiveObject to a byte stream. * * @param out ByteStream.Output, an unsynchronized version of ByteArrayOutputStream, used as a * backing buffer for the the DataOutputStream * @param o the PrimitiveObject//from w ww .ja va 2s . c om * @param oi the PrimitiveObjectInspector * @throws IOException on error during the write operation */ public static void writePrimitive(OutputStream out, Object o, PrimitiveObjectInspector oi) throws IOException { DataOutputStream dos = new DataOutputStream(out); try { switch (oi.getPrimitiveCategory()) { case BOOLEAN: boolean b = ((BooleanObjectInspector) oi).get(o); dos.writeBoolean(b); break; case BYTE: byte bt = ((ByteObjectInspector) oi).get(o); dos.writeByte(bt); break; case SHORT: short s = ((ShortObjectInspector) oi).get(o); dos.writeShort(s); break; case INT: int i = ((IntObjectInspector) oi).get(o); dos.writeInt(i); break; case LONG: long l = ((LongObjectInspector) oi).get(o); dos.writeLong(l); break; case FLOAT: float f = ((FloatObjectInspector) oi).get(o); dos.writeFloat(f); break; case DOUBLE: double d = ((DoubleObjectInspector) oi).get(o); dos.writeDouble(d); break; default: throw new RuntimeException("Hive internal error."); } } finally { // closing the underlying ByteStream should have no effect, the data should still be // accessible dos.close(); } }
From source file:com.netflix.ice.basic.BasicReservationService.java
private void pollAPI() throws Exception { long currentTime = new DateMidnight().getMillis(); DescribeReservedInstancesOfferingsRequest req = new DescribeReservedInstancesOfferingsRequest().withFilters( new com.amazonaws.services.ec2.model.Filter().withName("marketplace").withValues("false")); String token = null;// w ww.j a v a 2s . co m boolean hasNewPrice = false; AmazonEC2Client ec2Client = new AmazonEC2Client(AwsUtils.awsCredentialsProvider, AwsUtils.clientConfig); for (Region region : Region.getAllRegions()) { ec2Client.setEndpoint("ec2." + region.name + ".amazonaws.com"); do { if (!StringUtils.isEmpty(token)) req.setNextToken(token); DescribeReservedInstancesOfferingsResult offers = ec2Client.describeReservedInstancesOfferings(req); token = offers.getNextToken(); for (ReservedInstancesOffering offer : offers.getReservedInstancesOfferings()) { if (offer.getProductDescription().indexOf("Amazon VPC") >= 0) continue; ReservationUtilization utilization = ReservationUtilization.get(offer.getOfferingType()); Ec2InstanceReservationPrice.ReservationPeriod term = offer.getDuration() / 24 / 3600 > 366 ? Ec2InstanceReservationPrice.ReservationPeriod.threeyear : Ec2InstanceReservationPrice.ReservationPeriod.oneyear; if (term != this.term) continue; double hourly = offer.getUsagePrice(); if (hourly <= 0) { for (RecurringCharge recurringCharge : offer.getRecurringCharges()) { if (recurringCharge.getFrequency().equals("Hourly")) { hourly = recurringCharge.getAmount(); break; } } } UsageType usageType = getUsageType(offer.getInstanceType(), offer.getProductDescription()); hasNewPrice = setPrice(utilization, currentTime, Zone.getZone(offer.getAvailabilityZone()).region, usageType, offer.getFixedPrice(), hourly) || hasNewPrice; logger.info("Setting RI price for " + Zone.getZone(offer.getAvailabilityZone()).region + " " + utilization + " " + usageType + " " + offer.getFixedPrice() + " " + hourly); } } while (!StringUtils.isEmpty(token)); } ec2Client.shutdown(); if (hasNewPrice) { for (ReservationUtilization utilization : files.keySet()) { File file = files.get(utilization); DataOutputStream out = new DataOutputStream(new FileOutputStream(file)); try { Serializer.serialize(out, this.ec2InstanceReservationPrices.get(utilization)); AwsUtils.upload(config.workS3BucketName, config.workS3BucketPrefix, file); } finally { out.close(); } } } }