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


Java ByteArrayOutputStream.write方法代码示例

本文整理汇总了Java中java.io.ByteArrayOutputStream.write方法的典型用法代码示例。如果您正苦于以下问题:Java ByteArrayOutputStream.write方法的具体用法?Java ByteArrayOutputStream.write怎么用?Java ByteArrayOutputStream.write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.io.ByteArrayOutputStream的用法示例。


在下文中一共展示了ByteArrayOutputStream.write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testWriteRandomNumberOfBytes

import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public void testWriteRandomNumberOfBytes() throws IOException {
    Integer randomBufferSize = randomIntBetween(BUFFER_SIZE, 2 * BUFFER_SIZE);
    MockDefaultS3OutputStream out = newS3OutputStream(randomBufferSize);

    Integer randomLength = randomIntBetween(1, 2 * BUFFER_SIZE);
    ByteArrayOutputStream content = new ByteArrayOutputStream(randomLength);
    for (int i = 0; i < randomLength; i++) {
        content.write(randomByte());
    }

    copy(content.toByteArray(), out);

    // Checks length & content
    assertThat(out.getLength(), equalTo((long) randomLength));
    assertThat(Arrays.equals(content.toByteArray(), out.toByteArray()), equalTo(true));

    assertThat(out.getBufferSize(), equalTo(randomBufferSize));
    int times = (int) Math.ceil(randomLength.doubleValue() / randomBufferSize.doubleValue());
    assertThat(out.getFlushCount(), equalTo(times));
    if (times > 1) {
        assertTrue(out.isMultipart());
    } else {
        assertFalse(out.isMultipart());
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:26,代码来源:S3OutputStreamTests.java

示例2: readStream

import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private byte[] readStream(InputStream stream) throws Exception
{
    int sizeHint = 0;
    if (getHttpResponse() != null) {
        sizeHint = (int)(getHttpResponse().getEntity().getContentLength());
    }
    sizeHint = Math.max(sizeHint, BUFFER_SIZE);

    ByteArrayOutputStream output = new ByteArrayOutputStream(sizeHint);
    byte[] buffer = new byte[BUFFER_SIZE];
    int count = 0;
    while ((count = stream.read(buffer, 0, buffer.length)) >= 0) {
        output.write(buffer, 0, count);
    }
    return output.toByteArray();
}
 
开发者ID:suomenriistakeskus,项目名称:oma-riista-android,代码行数:17,代码来源:ImageTask.java

示例3: testSerializeDelimited

import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public void testSerializeDelimited() throws Exception {
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  TestUtil.getAllSet().writeDelimitedTo(output);
  output.write(12);
  TestUtil.getPackedSet().writeDelimitedTo(output);
  output.write(34);

  ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());

  TestUtil.assertAllFieldsSet(TestAllTypes.parseDelimitedFrom(input));
  assertEquals(12, input.read());
  TestUtil.assertPackedFieldsSet(TestPackedTypes.parseDelimitedFrom(input));
  assertEquals(34, input.read());
  assertEquals(-1, input.read());

  // We're at EOF, so parsing again should return null.
  assertTrue(TestAllTypes.parseDelimitedFrom(input) == null);
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:19,代码来源:WireFormatTest.java

示例4: parse

import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public static byte[] parse(String s) {
    try {
        int n = s.length();
        ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);
        StringReader r = new StringReader(s);
        while (true) {
            int b1 = nextNibble(r);
            if (b1 < 0) {
                break;
            }
            int b2 = nextNibble(r);
            if (b2 < 0) {
                throw new RuntimeException("Invalid string " + s);
            }
            int b = (b1 << 4) | b2;
            out.write(b);
        }
        return out.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:23,代码来源:DigestKAT.java

示例5: readResponseLine

import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
static String readResponseLine(InputStream is) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int b;
    while ((b = is.read()) != -1) {
        if (b == '\r') {
            int next = is.read();
            if (next == '\n') {
                return bos.toString("ISO-8859-1"); // NOI18N
            } else if (next == -1) {
                return null;
            } else {
                bos.write(b);
                bos.write(next);
            }
        } else {
            bos.write(b);
        }
    }
    return null;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:21,代码来源:HttpUtils.java

示例6: convertImageStreamToByteArray

import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/**
 * Converts an InputStream containing an image to a byte array
 * @param imageStream An InputStream containing an image
 * @return The supplied image in byte[] from
 */

public static byte[] convertImageStreamToByteArray(InputStream imageStream){
    // Taken from: https://stackoverflow.com/questions/10296734/image-uri-to-bytesarray
    // Date: December 1, 2017
    ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
    int bufferSize = 1024;
    byte[] buffer = new byte[bufferSize];

    int len = 0;
    try {
        while ((len = imageStream.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
        }
    } catch (IOException e){

    }

    return byteBuffer.toByteArray();
}
 
开发者ID:CMPUT301F17T23,项目名称:routineKeen,代码行数:25,代码来源:PhotoHelpers.java

示例7: findInClassPath

import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
@NotNull
public static TestSource findInClassPath(@NotNull String root, @NotNull String qualifiedClassName) {
    InputStream in = ClassLoader
            .getSystemClassLoader()
            .getResourceAsStream(root + "/" + qualifiedClassName.replace('.', '/') + ".java");
    byte[] buffer = new byte[1024];
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    int read;
    try {
        while ((read = in.read(buffer)) >= 0) {
            bOut.write(buffer, 0, read);
        }
    } catch (IOException e) {
        throw new IllegalStateException(String.format("Can't read source text for the class %s from source "
                                                      + "root '%s'", qualifiedClassName, root), e);
    }
    return new TestSourceImpl(new String(bOut.toByteArray(), StandardCharsets.UTF_8), qualifiedClassName);
}
 
开发者ID:denis-zhdanov,项目名称:traute,代码行数:19,代码来源:TestUtil.java

示例8: addPkcs1PaddingForPrivateKeyOperation

import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/** A&ntilde;ade relleno PKCS#1 para operaciones con clave privada.
 * @param in Datos a los que se quiere a&ntilde;adir relleno PKCS#1.
 * @param keySize Tama&ntilde;o de la clave privada que operar&aacute; posteriormente con estos datos con relleno.
 * @return Datos con el relleno PKCS#1 a&ntilde;adido.
 * @throws IOException En caso de error el el tratamiento de datos. */
public final static byte[] addPkcs1PaddingForPrivateKeyOperation(final byte[] in, final int keySize) throws IOException {
	if (in == null) {
		throw new IllegalArgumentException("Los datos de entrada no pueden ser nulos"); //$NON-NLS-1$
	}
	if (keySize != 1024 && keySize != 2048) {
		throw new IllegalArgumentException("Solo se soportan claves de 1024 o 2048 bits, y se ha indicado " + keySize); //$NON-NLS-1$
	}
	final int len = keySize == 1024 ? PKCS1_LEN_1024 : PKCS1_LEN_2048;
	if (in.length > len - 3) {
		throw new IllegalArgumentException(
			"Los datos son demasiado grandes para el valor de clave indicado: " + in.length + " > " + len + "-3" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		);
	}
	final ByteArrayOutputStream baos = new ByteArrayOutputStream(len);
	baos.write(PKCS1_DELIMIT);    // Delimitador :   00
	baos.write(PKCS1_BLOCK_TYPE); // Tipo de bloque: 01
	while (baos.size() < len - (1 + in.length)) { // Se rellena hasta dejar sitio justo para un delimitador y los datos
		baos.write(PKCS1_FILL);
	}
	baos.write(PKCS1_DELIMIT);    // Delimitador :   00
	baos.write(in);               // Datos

	return baos.toByteArray();
}
 
开发者ID:MiFirma,项目名称:mi-firma-android,代码行数:30,代码来源:CryptoHelper.java

示例9: decompress

import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public static byte[] decompress(byte[] value) throws DataFormatException {

    ByteArrayOutputStream bos = new ByteArrayOutputStream(value.length);

    Inflater decompressor = new Inflater();

    try {
      decompressor.setInput(value);

      final byte[] buf = new byte[1024];
      while (!decompressor.finished()) {
        int count = decompressor.inflate(buf);
        bos.write(buf, 0, count);
      }
    } finally {
      decompressor.end();
    }

    return bos.toByteArray();
  }
 
开发者ID:MUFCRyan,项目名称:BilibiliClient,代码行数:21,代码来源:BiliDanmukuCompressionTools.java

示例10: appVersionData

import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private byte[] appVersionData() throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    String version = RndTool.class.getPackage().getImplementationVersion();
    if (version != null) {
        bos.write(version.getBytes(StandardCharsets.UTF_8));
    }

    return bos.toByteArray();
}
 
开发者ID:patrickfav,项目名称:dice,代码行数:10,代码来源:PersonalizationSource.java

示例11: testBinary

import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public void testBinary() throws IOException, ClassificationException {
	File file = new File("src/test/resources/data/SampleData.txt");
	FileInputStream fileInputStream = new FileInputStream(file);
	ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
	int available;
	while ((available = fileInputStream.available()) > 0) {
		byte[] data = new byte[available];
		fileInputStream.read(data);
		byteArrayOutputStream.write(data);
	}
	fileInputStream.close();
	byteArrayOutputStream.close();

	Result result1 = classificationClient.getClassifiedDocument(byteArrayOutputStream.toByteArray(), "SampleData.txt");
	assertEquals("Hash 1", "f7be152b1d057570b892dbe3dc39bd70", result1.getHash());

	Map<String, Collection<String>> metadata = new HashMap<String, Collection<String>>();
	Collection<String> cheeses = new Vector<String>();
	cheeses.add("Brie");
	cheeses.add("Camenbert");
	cheeses.add("Cheddar");
	metadata.put("cheeses", cheeses);

	Result result2 = classificationClient.getClassifiedDocument(byteArrayOutputStream.toByteArray(), "SampleData.txt", new Title("title"), metadata);
	assertEquals("Hash 2", "c723555e774c94d0ead71d5c1cc06efc", result2.getHash());

}
 
开发者ID:Smartlogic-Semaphore-Limited,项目名称:Java-APIs,代码行数:28,代码来源:ClassifyDocumentWithHashTest.java

示例12: getResourceAsString

import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private static String getResourceAsString(String path) throws IOException {
	InputStream inputStream = JrpcService.class.getResourceAsStream(path);

	ByteArrayOutputStream result = new ByteArrayOutputStream();
	byte[] buffer = new byte[1024];
	int length;
	while ((length = inputStream.read(buffer)) != -1) {
		result.write(buffer, 0, length);
	}
	return result.toString(StringConstant.messageEncoding);
}
 
开发者ID:EonTechnology,项目名称:server,代码行数:12,代码来源:DummyServiceTest.java

示例13: readTestData

import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private byte[] readTestData(String testDataFileName) throws IOException {
  InputStream in = getClass().getResourceAsStream("testdata/" + testDataFileName);
  Assert.assertNotNull("test data file doesn't exist: " + testDataFileName, in);
  ByteArrayOutputStream result = new ByteArrayOutputStream();
  byte[] buffer = new byte[32768];
  int numRead = 0;
  while ((numRead = in.read(buffer)) >= 0) {
    result.write(buffer, 0, numRead);
  }
  return stripNewlineIfNecessary(result.toByteArray());
}
 
开发者ID:lizhangqu,项目名称:CorePatch,代码行数:12,代码来源:BsPatchTest.java

示例14: open

import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public void open(AudioInputStream stream) throws LineUnavailableException,
        IOException {
    if (isOpen()) {
        throw new IllegalStateException("Clip is already open with format "
                + getFormat() + " and frame lengh of " + getFrameLength());
    }
    if (AudioFloatConverter.getConverter(stream.getFormat()) == null)
        throw new IllegalArgumentException("Invalid format : "
                + stream.getFormat().toString());

    if (stream.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
        byte[] data = new byte[(int) stream.getFrameLength()
                * stream.getFormat().getFrameSize()];
        int readsize = 512 * stream.getFormat().getFrameSize();
        int len = 0;
        while (len != data.length) {
            if (readsize > data.length - len)
                readsize = data.length - len;
            int ret = stream.read(data, len, readsize);
            if (ret == -1)
                break;
            if (ret == 0)
                Thread.yield();
            len += ret;
        }
        open(stream.getFormat(), data, 0, len);
    } else {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] b = new byte[512 * stream.getFormat().getFrameSize()];
        int r = 0;
        while ((r = stream.read(b)) != -1) {
            if (r == 0)
                Thread.yield();
            baos.write(b, 0, r);
        }
        open(stream.getFormat(), baos.toByteArray(), 0, baos.size());
    }

}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:40,代码来源:SoftMixingClip.java

示例15: getBytes

import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private static byte[] getBytes(InputStream in) throws IOException {
	byte[] bytes = new byte[0];
	byte[] buffer = new byte[1024];
	ByteArrayOutputStream bout = new ByteArrayOutputStream();
	int len = 0;
	while((len = in.read(buffer)) > -1) {
		bout.write(buffer, 0, len);
	}
	bytes = bout.toByteArray();
	return bytes;
}
 
开发者ID:huang-up,项目名称:mycat-src-1.6.1-RELEASE,代码行数:12,代码来源:ServerPrepareTest.java


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