本文整理汇总了Java中java.io.ByteArrayOutputStream.writeTo方法的典型用法代码示例。如果您正苦于以下问题:Java ByteArrayOutputStream.writeTo方法的具体用法?Java ByteArrayOutputStream.writeTo怎么用?Java ByteArrayOutputStream.writeTo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.ByteArrayOutputStream
的用法示例。
在下文中一共展示了ByteArrayOutputStream.writeTo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeToResponse
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/**
* Write the given temporary OutputStream to the HTTP response.
* @param response current HTTP response
* @param baos the temporary OutputStream to write
* @throws IOException if writing/flushing failed
*/
protected void writeToResponse(HttpServletResponse response, ByteArrayOutputStream baos) throws IOException {
// Write content type and also length (determined via byte array).
response.setContentType(getContentType());
response.setContentLength(baos.size());
// Flush byte array to servlet output stream.
ServletOutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
}
示例2: jsGet_body
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public Object jsGet_body() {
ByteArrayOutputStream body = javaContext.getBodyStream();
if (body == null) {
return Context.getUndefinedValue();
}
final Scriptable buffer = cx.newObject(scope, "Uint8Array");
try {
body.writeTo(new OutputStream() {
int count = 0;
@Override
public void write(int b) throws IOException {
buffer.put(count++, buffer, b);
}
});
} catch (Exception e) {
return e;
}
return buffer;
}
示例3: encodeKeyValues
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
static ByteBuffer encodeKeyValues(DataBlockEncoding encoding, List<KeyValue> kvs,
HFileBlockEncodingContext encodingContext) throws IOException {
DataBlockEncoder encoder = encoding.getEncoder();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(HConstants.HFILEBLOCK_DUMMY_HEADER);
DataOutputStream dos = new DataOutputStream(baos);
encoder.startBlockEncoding(encodingContext, dos);
for (KeyValue kv : kvs) {
encoder.encode(kv, encodingContext, dos);
}
BufferGrabbingByteArrayOutputStream stream = new BufferGrabbingByteArrayOutputStream();
baos.writeTo(stream);
encoder.endBlockEncoding(encodingContext, dos, stream.getBuffer());
byte[] encodedData = new byte[baos.size() - ENCODED_DATA_OFFSET];
System.arraycopy(baos.toByteArray(), ENCODED_DATA_OFFSET, encodedData, 0, encodedData.length);
return ByteBuffer.wrap(encodedData);
}
示例4: export
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
protected void export(GeoSet geoSet, OutputStream outputStream) throws IOException {
this.recordCounter = 1;
this.shxRecords.clear();
// Accumulate the data in a ByteArrayOutputStream.
// This allows for finding the size of the resulting file.
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
BufferedOutputStream buffOs = new BufferedOutputStream(byteArrayOutputStream);
MixedEndianDataOutputStream geom = new MixedEndianDataOutputStream(buffOs);
this.writeGeoSet(geom, geoSet);
// add total size of geometry to shx records
this.shxRecords.add(new Integer(geom.size()));
// Close the ByteArrayOutputStream.
// This is not closing the destination outputStream
geom.close();
// write file header
MixedEndianDataOutputStream head = new MixedEndianDataOutputStream(outputStream);
this.writeHeader(geoSet, head, geom.size());
// copy the geometry to the outputStream
byteArrayOutputStream.writeTo(outputStream);
}
示例5: writeSection
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/**
* Write a section with header and data.
*
* @param type
* the name of the section
* @param data
* the data of the section
* @param name
* the name, must be set if the id == 0
* @throws IOException
* if any I/O error occur
*/
void writeSection( SectionType type, WasmOutputStream data, String name ) throws IOException {
ByteArrayOutputStream baos = (ByteArrayOutputStream)data.out;
int size = baos.size();
if( size == 0 ) {
return;
}
writeVaruint32( type.ordinal() );
writeVaruint32( size );
if( type == SectionType.Custom ) {
byte[] bytes = name.getBytes( StandardCharsets.ISO_8859_1 );
writeVaruint32( bytes.length );
write( bytes );
}
baos.writeTo( this );
}
示例6: getServlet
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
@Override
public HttpHandler getServlet() {
return exchange -> {
final ByteArrayOutputStream response = new ByteArrayOutputStream(1 << 20);
final OutputStreamWriter osw = new OutputStreamWriter(response);
TextFormat.write004(osw, registry.metricFamilySamples());
osw.flush();
osw.close();
response.flush();
response.close();
exchange.getResponseHeaders().set("Content-Type", TextFormat.CONTENT_TYPE_004);
exchange.getResponseHeaders().set("Content-Length", String.valueOf(response.size()));
exchange.sendResponseHeaders(200, response.size());
response.writeTo(exchange.getResponseBody());
exchange.close();
};
}
示例7: saveFile
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/**
* 保存文件
*
* @param baos
* @param fileName
* @throws Exception
*/
public static String saveFile(ByteArrayOutputStream baos, String fileName)
throws Exception {
String Path = App.getAppContext().getFilesDir().getPath();
File dirFile = new File(Path);
if (!dirFile.exists()) {
dirFile.mkdir();
}
File myCaptureFile = new File(Path + "/" + fileName);
FileOutputStream fi = new FileOutputStream(myCaptureFile);
baos.writeTo(fi);
baos.flush();
baos.close();
return myCaptureFile.getAbsolutePath();
}
示例8: getBytes
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/**
* <p>解析报文协议,返回字节数组,用于应用间的消息的传输</p>
* @Title: getBytes
* @Description: 解析报文协议,返回字节数组,用于应用间的消息的传输
* @return byte[] 用于传输的报文包
* @author [email protected]
* @date 2017年3月24日 上午11:13:39
*/
public byte[] getBytes(){
//结果返回数据内容
byte[] resultContent = new byte[0];
try{
//创建一个不用关闭的输出流,用于最后的byte[]转换,不用维护数组的pos
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//写入tpdu
baos.write(getTpduBytes());
//写入header
baos.write(getHeaderBytes());
//写入mti+bitmap+8583报文数据(设置有校验位,则包含,没设置校验位,则不包含)
baos.write(getMacBlock());
//写入校验域
baos.write(getMac());
//计算报文整体长度
String size = StringUtil.repeat("00", factory.getMsgLength())+Integer.toHexString(baos.size());
size = size.substring(size.length() - factory.getMsgLength() * 2);
byte[] bSize = EncodeUtil.bcd(size);
//准备最终报文数据
ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
//写入报文长度
resultStream.write(bSize);
//写入报文信息
baos.writeTo(resultStream);
resultContent = resultStream.toByteArray();
}catch(IOException e){
e.printStackTrace();
}
return resultContent;
}
示例9: compressQuality
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private void compressQuality(File outFile, long maxSize, int maxQuality) throws IOException {
long length = outFile.length();
int quality = 90;
if (length > maxSize) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
BoxingLog.d("source file size : " + outFile.length() + ",path : " + outFile);
while (true) {
compressPhotoByQuality(outFile, bos, quality);
long size = bos.size();
BoxingLog.d("compressed file size : " + size);
if (quality <= maxQuality) {
break;
}
if (size < maxSize) {
break;
} else {
quality -= 10;
bos.reset();
}
}
OutputStream fos = new FileOutputStream(outFile);
bos.writeTo(fos);
bos.flush();
fos.close();
bos.close();
}
}
示例10: serializeAsPB
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/**
* Write trailer data as protobuf
* @param outputStream
* @throws IOException
*/
void serializeAsPB(DataOutputStream output) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
HFileProtos.FileTrailerProto.Builder builder = HFileProtos.FileTrailerProto.newBuilder()
.setFileInfoOffset(fileInfoOffset)
.setLoadOnOpenDataOffset(loadOnOpenDataOffset)
.setUncompressedDataIndexSize(uncompressedDataIndexSize)
.setTotalUncompressedBytes(totalUncompressedBytes)
.setDataIndexCount(dataIndexCount)
.setMetaIndexCount(metaIndexCount)
.setEntryCount(entryCount)
.setNumDataIndexLevels(numDataIndexLevels)
.setFirstDataBlockOffset(firstDataBlockOffset)
.setLastDataBlockOffset(lastDataBlockOffset)
// TODO this is a classname encoded into an HFile's trailer. We are going to need to have
// some compat code here.
.setComparatorClassName(comparatorClassName)
.setCompressionCodec(compressionCodec.ordinal());
if (encryptionKey != null) {
builder.setEncryptionKey(ByteStringer.wrap(encryptionKey));
}
// We need this extra copy unfortunately to determine the final size of the
// delimited output, see use of baos.size() below.
builder.build().writeDelimitedTo(baos);
baos.writeTo(output);
// Pad to make up the difference between variable PB encoding length and the
// length when encoded as writable under earlier V2 formats. Failure to pad
// properly or if the PB encoding is too big would mean the trailer wont be read
// in properly by HFile.
int padding = getTrailerSize() - NOT_PB_SIZE - baos.size();
if (padding < 0) {
throw new IOException("Pbuf encoding size exceeded fixed trailer size limit");
}
for (int i = 0; i < padding; i++) {
output.write(0);
}
}
示例11: realWrite
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public void realWrite(boolean printHeadBodyTags) throws IOException {
flush();
ByteArrayOutputStream stream = (ByteArrayOutputStream) this.out;
this.out = outputStream;
if (!printHeadBodyTags) {
stream.writeTo(outputStream);
return;
}
println("<html>");
println("<head>");
println("<style>");
for (Style style : styles)
println(style);
println("</style>");
println("</head>");
String htmlText = new String(stream.toByteArray(), "UTF-8");
Source source = new Source(htmlText);
source.fullSequentialParse();
List<StartTag> startTags = source.getAllStartTags("body");
if (startTags.size() == 0) {
println("<body>");
println(htmlText);
println("</body>");
} else {
println(new StringBuffer(startTags.get(0).getElement()));
}
println("</html>");
}
示例12: serialize
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/**
* Write the trailer to a data stream. We support writing version 1 for
* testing and for determining version 1 trailer size. It is also easy to see
* what fields changed in version 2.
*
* @param outputStream
* @throws IOException
*/
void serialize(DataOutputStream outputStream) throws IOException {
HFile.checkFormatVersion(majorVersion);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream baosDos = new DataOutputStream(baos);
BlockType.TRAILER.write(baosDos);
serializeAsPB(baosDos);
// The last 4 bytes of the file encode the major and minor version universally
baosDos.writeInt(materializeVersion(majorVersion, minorVersion));
baos.writeTo(outputStream);
}
示例13: outputToJar
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/**
* Generate output JAR-file and packages
*/
public void outputToJar() throws IOException {
// create the manifest
final Manifest manifest = new Manifest();
final java.util.jar.Attributes atrs = manifest.getMainAttributes();
atrs.put(java.util.jar.Attributes.Name.MANIFEST_VERSION, "1.2");
final Map<String, Attributes> map = manifest.getEntries();
// create manifest
final String now = (new Date()).toString();
final java.util.jar.Attributes.Name dateAttr =
new java.util.jar.Attributes.Name("Date");
final File jarFile = new File(_destDir, _jarFileName);
final JarOutputStream jos =
new JarOutputStream(new FileOutputStream(jarFile), manifest);
for (JavaClass clazz : _bcelClasses) {
final String className = clazz.getClassName().replace('.', '/');
final java.util.jar.Attributes attr = new java.util.jar.Attributes();
attr.put(dateAttr, now);
map.put(className + ".class", attr);
jos.putNextEntry(new JarEntry(className + ".class"));
final ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
clazz.dump(out); // dump() closes it's output stream
out.writeTo(jos);
}
jos.close();
}
示例14: printNode
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public static void printNode(Node node) {
DOMSource source = new DOMSource(node);
String msgString = null;
try {
Transformer xFormer = TransformerFactory.newInstance().newTransformer();
xFormer.setOutputProperty("omit-xml-declaration", "yes");
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
Result result = new StreamResult(outStream);
xFormer.transform(source, result);
outStream.writeTo(System.out);
} catch (Exception ex) {
ex.printStackTrace();
}
}
示例15: write
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
@Override
public void write(Cell cell) throws IOException {
if (encryptor == null) {
super.write(cell);
return;
}
byte[] iv = nextIv();
encryptor.setIv(iv);
encryptor.reset();
// TODO: Check if this is a cell for an encrypted CF. If not, we can
// write a 0 here to signal an unwrapped cell and just dump the KV bytes
// afterward
StreamUtils.writeRawVInt32(out, iv.length);
out.write(iv);
// TODO: Add support for WAL compression
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream cout = encryptor.createEncryptionStream(baos);
int tlen = cell.getTagsLength();
// Write the KeyValue infrastructure as VInts.
StreamUtils.writeRawVInt32(cout, KeyValueUtil.keyLength(cell));
StreamUtils.writeRawVInt32(cout, cell.getValueLength());
// To support tags
StreamUtils.writeRawVInt32(cout, tlen);
// Write row, qualifier, and family
StreamUtils.writeRawVInt32(cout, cell.getRowLength());
cout.write(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
StreamUtils.writeRawVInt32(cout, cell.getFamilyLength());
cout.write(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
StreamUtils.writeRawVInt32(cout, cell.getQualifierLength());
cout.write(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
// Write the rest ie. ts, type, value and tags parts
StreamUtils.writeLong(cout, cell.getTimestamp());
cout.write(cell.getTypeByte());
cout.write(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
if (tlen > 0) {
cout.write(cell.getTagsArray(), cell.getTagsOffset(), tlen);
}
cout.close();
StreamUtils.writeRawVInt32(out, baos.size());
baos.writeTo(out);
// Increment IV given the final payload length
incrementIv(baos.size());
}