本文整理匯總了Java中java.io.ByteArrayOutputStream.flush方法的典型用法代碼示例。如果您正苦於以下問題:Java ByteArrayOutputStream.flush方法的具體用法?Java ByteArrayOutputStream.flush怎麽用?Java ByteArrayOutputStream.flush使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.io.ByteArrayOutputStream
的用法示例。
在下文中一共展示了ByteArrayOutputStream.flush方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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();
};
}
示例2: readMessageAsByteArray
import java.io.ByteArrayOutputStream; //導入方法依賴的package包/類
private byte[] readMessageAsByteArray(InputStream in, int messageLength) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int readCount;
int remaining = messageLength;
byte[] buffer = new byte[1024];
while ((readCount = in.read(buffer)) != -1) {
out.write(buffer, 0, readCount);
remaining -= readCount;
}
if (remaining > 0) {
throw new TaskwarriorClientException("Could not retrieve complete message, remaining '%d' of '%d' bytes.", remaining,
messageLength);
}
out.flush();
return out.toByteArray();
}
示例3: inputStreamToByteArray
import java.io.ByteArrayOutputStream; //導入方法依賴的package包/類
private static byte[] inputStreamToByteArray(InputStream is){
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
return buffer.toByteArray();
} catch (Exception e){
throw new RuntimeException(e);
}
}
示例4: exportXml
import java.io.ByteArrayOutputStream; //導入方法依賴的package包/類
@Override
public byte[] exportXml() throws IOException {
Lock lock = currentSolution().getLock().readLock();
lock.lock();
try {
boolean anonymize = ApplicationProperty.SolverXMLExportNames.isFalse();
boolean idconv = ApplicationProperty.SolverXMLExportConvertIds.isTrue();
ByteArrayOutputStream ret = new ByteArrayOutputStream();
Document document = createCurrentSolutionBackup(anonymize, idconv);
if (ApplicationProperty.SolverXMLExportConfiguration.isTrue())
saveProperties(document);
(new XMLWriter(ret, OutputFormat.createPrettyPrint())).write(document);
ret.flush(); ret.close();
return ret.toByteArray();
} finally {
lock.unlock();
}
}
示例5: readInputStreamAsBytes
import java.io.ByteArrayOutputStream; //導入方法依賴的package包/類
private byte[] readInputStreamAsBytes(InputStream inputStream,OnHttpListener listener) throws IOException{
if(inputStream == null){
return null;
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
int readCount = 0;
byte[] data = new byte[2048];
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
readCount += nRead;
if (listener != null) {
listener.onHttpResponseProgress(readCount);
}
}
buffer.flush();
return buffer.toByteArray();
}
示例6: getBitmapConsumer
import java.io.ByteArrayOutputStream; //導入方法依賴的package包/類
private Consumer<Bitmap> getBitmapConsumer() {
return new Consumer<Bitmap>() {
@Override
public void accept(Bitmap bitmap) throws Exception {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, byteArrayOutputStream);
byte[] b = byteArrayOutputStream.toByteArray();
String base64Str = org.java_websocket.util.Base64.encodeBytes(b);
RxBus.getDefault().post(base64Str);
try {
byteArrayOutputStream.flush();
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
bitmap.recycle();
}
}
};
}
示例7: inflate
import java.io.ByteArrayOutputStream; //導入方法依賴的package包/類
public static byte[] inflate(InputStream stream) throws IOException {
InflaterInputStream inputStream = new InflaterInputStream(stream);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
try {
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
} finally {
buffer = outputStream.toByteArray();
outputStream.flush();
outputStream.close();
inputStream.close();
}
return buffer;
}
示例8: deserializeTree
import java.io.ByteArrayOutputStream; //導入方法依賴的package包/類
private static void deserializeTree(int depth, int width, int len)
throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException {
BinaryInputArchive ia;
int count;
{
DataTree tree = new DataTree();
SerializationPerfTest.createNodes(tree, "/", depth, width, tree.getNode("/").stat.getCversion(), new byte[len]);
count = tree.getNodeCount();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BinaryOutputArchive oa = BinaryOutputArchive.getArchive(baos);
tree.serialize(oa, "test");
baos.flush();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ia = BinaryInputArchive.getArchive(bais);
}
DataTree dserTree = new DataTree();
System.gc();
long start = System.nanoTime();
dserTree.deserialize(ia, "test");
long end = System.nanoTime();
long durationms = (end - start) / 1000000L;
long pernodeus = ((end - start) / 1000L) / count;
Assert.assertEquals(count, dserTree.getNodeCount());
LOG.info("Deserialized " + count + " nodes in " + durationms
+ " ms (" + pernodeus + "us/node), depth=" + depth + " width="
+ width + " datalen=" + len);
}
示例9: rawMP3
import java.io.ByteArrayOutputStream; //導入方法依賴的package包/類
public static byte[] rawMP3(File file) {
byte[] ret = null;
try {
InputStream in = new BufferedInputStream(new FileInputStream(file), 8 * 1024);
in.mark(10);
int headerEnd = readID3v2Header(in);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = in.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
ret = buffer.toByteArray();
int end = ret.length;
if (end > 128 && ret[end-128] == 84 && ret[end-127] == 65 && ret[end-126] == 71) // Detect TAG from ID3v1
end -= 129;
ret = Arrays.copyOfRange(ret, headerEnd, end); // Discard header (ID3 v2) and last 128 bytes (ID3 v1)
} catch(IOException e) {
e.printStackTrace();
}
return ret;
}
示例10: uploadPhoto
import java.io.ByteArrayOutputStream; //導入方法依賴的package包/類
private static String uploadPhoto(String fileName) throws Exception {
URL url = new URI("http", ip, "/api/v1/photo", null, null).toURL();
Map<String, String> headers = new HashMap<>();
String base = Base64.getEncoder().encodeToString("admin:[email protected]".getBytes("UTF-8"));
headers.put("Authorization", "Basic " + base);
FileInputStream in = new FileInputStream(fileName);
ByteArrayOutputStream out = new ByteArrayOutputStream();
int r;
final byte[] buf = new byte[1024];
while ((r = in.read(buf)) != -1) {
out.write(buf, 0, r);
}
out.flush();
Response res = new Response();
JSONObject o = new JSONObject(readString(doPost(url, headers, out.toByteArray(), res)));
return o.getString("photoId");
}
示例11: setSerializableObject
import java.io.ByteArrayOutputStream; //導入方法依賴的package包/類
/**
* Sets the value for the placeholder as a serialized Java object (used by
* various forms of setObject()
*
* @param parameterIndex
* @param parameterObj
*
* @throws SQLException
*/
private final void setSerializableObject(int parameterIndex, Object parameterObj) throws SQLException {
try {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
ObjectOutputStream objectOut = new ObjectOutputStream(bytesOut);
objectOut.writeObject(parameterObj);
objectOut.flush();
objectOut.close();
bytesOut.flush();
bytesOut.close();
byte[] buf = bytesOut.toByteArray();
ByteArrayInputStream bytesIn = new ByteArrayInputStream(buf);
setBinaryStream(parameterIndex, bytesIn, buf.length);
this.parameterTypes[parameterIndex - 1 + getParameterIndexOffset()] = Types.BINARY;
} catch (Exception ex) {
SQLException sqlEx = SQLError.createSQLException(Messages.getString("PreparedStatement.54") + ex.getClass().getName(),
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
sqlEx.initCause(ex);
throw sqlEx;
}
}
示例12: serializationObject
import java.io.ByteArrayOutputStream; //導入方法依賴的package包/類
private <T extends Serializable> byte[] serializationObject(T obj) {
Kryo kryo = new Kryo();
kryo.setReferences(false);
kryo.register(obj.getClass(), new JavaSerializer());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Output output = new Output(baos);
kryo.writeClassAndObject(output, obj);
output.flush();
output.close();
byte[] b = baos.toByteArray();
try {
baos.flush();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
return b;
}
示例13: flattenBitmap
import java.io.ByteArrayOutputStream; //導入方法依賴的package包/類
/**
* Compresses the bitmap to a byte array for serialization.
*/
public static byte[] flattenBitmap(Bitmap bitmap) {
// Try go guesstimate how much space the icon will take when serialized
// to avoid unnecessary allocations/copies during the write.
int size = bitmap.getWidth() * bitmap.getHeight() * 4;
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
try {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
return out.toByteArray();
} catch (IOException e) {
Log.w(TAG, "Could not write bitmap");
return null;
}
}
示例14: imageToByteArray
import java.io.ByteArrayOutputStream; //導入方法依賴的package包/類
byte[] imageToByteArray(BufferedImage img) throws IOException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "jpg", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
return imageInByte;
}
示例15: getRequestBody
import java.io.ByteArrayOutputStream; //導入方法依賴的package包/類
@Override
public byte[] getRequestBody() throws IOException {
if (!requestBody.isPresent()) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
InputStream is = requestContext.getEntityStream();
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
requestBody = Nullable.of(buffer.toByteArray());
}
return requestBody.get();
}