当前位置: 首页>>代码示例>>Java>>正文


Java ByteArrayOutputStream.flush方法代码示例

本文整理汇总了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();
    };
}
 
开发者ID:secondbase,项目名称:secondbase,代码行数:19,代码来源:PrometheusWebConsole.java

示例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();
}
 
开发者ID:aaschmid,项目名称:taskwarrior-java-client,代码行数:20,代码来源:TaskwarriorClient.java

示例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);
	}
}
 
开发者ID:starn,项目名称:encdroidMC,代码行数:19,代码来源:PhotoView.java

示例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();
    }
}
 
开发者ID:Jenner4S,项目名称:unitimes,代码行数:25,代码来源:AbstractSolver.java

示例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();
}
 
开发者ID:weexext,项目名称:ucar-weex-core,代码行数:23,代码来源:DefaultWXHttpAdapter.java

示例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();
            }
        }
    };
}
 
开发者ID:OddCN,项目名称:screen-share-to-browser,代码行数:24,代码来源:RecordService.java

示例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;
}
 
开发者ID:CoreXDevelopment,项目名称:CoreX,代码行数:20,代码来源:Zlib.java

示例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);
}
 
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:34,代码来源:DeserializationPerfTest.java

示例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;
}
 
开发者ID:sovteam,项目名称:buddybox,代码行数:27,代码来源:SongUtils.java

示例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");
}
 
开发者ID:sherisaac,项目名称:TurteTracker_APIServer,代码行数:21,代码来源:TestClient.java

示例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;
    }
}
 
开发者ID:bragex,项目名称:the-vigilantes,代码行数:32,代码来源:PreparedStatement.java

示例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;
}
 
开发者ID:PAA-NCIC,项目名称:SparkSeq,代码行数:22,代码来源:TestMapDataSerialization.java

示例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;
    }
}
 
开发者ID:michelelacorte,项目名称:FlickLauncher,代码行数:19,代码来源:Utilities.java

示例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;
}
 
开发者ID:adobe,项目名称:stock-api-sdk,代码行数:9,代码来源:DownSampleUtilTest.java

示例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();
}
 
开发者ID:crnk-project,项目名称:crnk-framework,代码行数:16,代码来源:JaxrsRequestContext.java


注:本文中的java.io.ByteArrayOutputStream.flush方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。