IOUtils.closeStream(out);
/** * Copies from one stream to another. * * @param in * InputStrem to read from * @param out * OutputStream to write to * @param buffSize * the size of the buffer * @param close * whether or not close the InputStream and OutputStream at the * end. The streams are closed in the finally clause. */ public static void copyBytes(InputStream in, OutputStream out, int buffSize, boolean close) throws IOException { try { copyBytes(in, out, buffSize); if (close) { out.close(); out = null; in.close(); in = null; } } finally { if (close) { closeStream(out); closeStream(in); } } }
/** * Closes the stream ignoring {@link IOException}. Must only be called in * cleaning up from exception handlers. * * @param stream * the Stream to close */ public static void closeStream(Closeable stream) { cleanup(null, stream); }
public static String readFile(File file) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); BufferedInputStream is = new BufferedInputStream(new FileInputStream(file)); try { IOUtils.copyBytes(is, os, 1024, true); } finally { is.close(); } return os.toString(); }
IOUtils.closeStream(out);
/** * Copies from one stream to another. * * @param in * InputStrem to read from * @param out * OutputStream to write to * @param buffSize * the size of the buffer * @param close * whether or not close the InputStream and OutputStream at the * end. The streams are closed in the finally clause. */ public static void copyBytes(InputStream in, OutputStream out, int buffSize, boolean close) throws IOException { try { copyBytes(in, out, buffSize); if (close) { out.close(); out = null; in.close(); in = null; } } finally { if (close) { closeStream(out); closeStream(in); } } }
/** * Closes the stream ignoring {@link IOException}. Must only be called in * cleaning up from exception handlers. * * @param stream * the Stream to close */ public static void closeStream(Closeable stream) { cleanup(null, stream); }
/** * Builds a SetTraceMask request to be sent to the server, consisting of * "stmk" followed by the 8-byte long representation of the trace mask. * * @param mask trace mask to set * @return built request * @throws IOException if there is an I/O error */ private String buildSetTraceMaskRequest(long mask) throws IOException { ByteArrayOutputStream baos = null; DataOutputStream dos = null; try { baos = new ByteArrayOutputStream(); dos = new DataOutputStream(baos); dos.writeBytes("stmk"); dos.writeLong(mask); } finally { IOUtils.closeStream(dos); IOUtils.closeStream(baos); } return new String(baos.toByteArray()); } }
public static byte[] serializeRequest(Request request) { if (request == null || request.getHdr() == null) return null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); try { request.getHdr().serialize(boa, "hdr"); if (request.getTxn() != null) { request.getTxn().serialize(boa, "txn"); } } catch (IOException e) { LOG.error("This really should be impossible", e); } finally { IOUtils.cleanup(LOG, baos); } return baos.toByteArray(); } }
IOUtils.closeStream(out);
private static void writeJobSplitMetaInfo(FileSystem fs, Path filename, FsPermission p, int splitMetaInfoVersion, JobSplit.SplitMetaInfo[] allSplitMetaInfo) throws IOException { // write the splits meta-info to a file for the job tracker FSDataOutputStream out = null; try { out = FileSystem.create(fs, filename, p); out.write(META_SPLIT_FILE_HEADER); WritableUtils.writeVInt(out, splitMetaInfoVersion); WritableUtils.writeVInt(out, allSplitMetaInfo.length); for(JobSplit.SplitMetaInfo splitMetaInfo: allSplitMetaInfo) { splitMetaInfo.write(out); } } finally { IOUtils.closeStream(out); } } }
private static void writeJobSplitMetaInfo(FileSystem fs, Path filename, FsPermission p, int splitMetaInfoVersion, JobSplit.SplitMetaInfo[] allSplitMetaInfo) throws IOException { // write the splits meta-info to a file for the job tracker FSDataOutputStream out = null; try { out = FileSystem.create(fs, filename, p); out.write(META_SPLIT_FILE_HEADER); WritableUtils.writeVInt(out, splitMetaInfoVersion); WritableUtils.writeVInt(out, allSplitMetaInfo.length); for(JobSplit.SplitMetaInfo splitMetaInfo: allSplitMetaInfo) { splitMetaInfo.write(out); } } finally { IOUtils.closeStream(out); } } }
@SuppressWarnings({ "unchecked", "unused" }) private <T> T getSplitDetails(Path file, long offset) throws IOException { FileSystem fs = file.getFileSystem(getYarnConf()); FSDataInputStream inFile = null; T split = null; try { inFile = fs.open(file); inFile.seek(offset); String className = Text.readString(inFile); Class<T> cls; try { cls = (Class<T>) getYarnConf().getClassByName(className); } catch (ClassNotFoundException ce) { IOException wrap = new IOException(String.format("Split class %s not found", className)); wrap.initCause(ce); throw wrap; } SerializationFactory factory = new SerializationFactory(getYarnConf()); Deserializer<T> deserializer = (Deserializer<T>) factory.getDeserializer(cls); deserializer.open(inFile); split = deserializer.deserialize(null); } finally { IOUtils.closeStream(inFile); } return split; }
@SuppressWarnings({ "unchecked", "unused" }) private <T> T getSplitDetails(Path file, long offset) throws IOException { FileSystem fs = file.getFileSystem(getYarnConf()); FSDataInputStream inFile = null; T split = null; try { inFile = fs.open(file); inFile.seek(offset); String className = Text.readString(inFile); Class<T> cls; try { cls = (Class<T>) getYarnConf().getClassByName(className); } catch (ClassNotFoundException ce) { IOException wrap = new IOException(String.format("Split class %s not found", className)); wrap.initCause(ce); throw wrap; } SerializationFactory factory = new SerializationFactory(getYarnConf()); Deserializer<T> deserializer = (Deserializer<T>) factory.getDeserializer(cls); deserializer.open(inFile); split = deserializer.deserialize(null); } finally { IOUtils.closeStream(inFile); } return split; }