本文整理汇总了Java中java.io.ByteArrayOutputStream.toString方法的典型用法代码示例。如果您正苦于以下问题:Java ByteArrayOutputStream.toString方法的具体用法?Java ByteArrayOutputStream.toString怎么用?Java ByteArrayOutputStream.toString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.ByteArrayOutputStream
的用法示例。
在下文中一共展示了ByteArrayOutputStream.toString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: uncompressToString
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public static String uncompressToString(byte[] b, String encoding) {
if (b == null || b.length == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(b);
try {
GZIPInputStream gunzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
return out.toString(encoding);
} catch (IOException e) {
log.error(e.getMessage(),e);
}
return null;
}
示例2: validateSnapshot
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private void validateSnapshot(boolean expectSuccess) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
PrintStream original = System.out;
try {
System.setOut(ps);
String args[] = new String[] { TESTNONCE, "--dir", TMPDIR };
SnapshotVerifier.main(args);
ps.flush();
String reportString = baos.toString("UTF-8");
System.err.println("Validate Snapshot :"+reportString);
if (expectSuccess) {
assertTrue(reportString.startsWith("Snapshot valid\n"));
System.err.println("Validate Snapshot :" + "Snapshot valid");
} else {
assertTrue(reportString.startsWith("Snapshot corrupted\n"));
System.err.println("Validate Snapshot :" + "Snapshot corrupted");
}
} catch (UnsupportedEncodingException e) {
} finally {
System.setOut(original);
}
}
示例3: toString
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/**
* Convert an {@link InputStream} into a {@link String}. Reference the SO below for an interesting
* performance comparison of various InputStream to String methodologies.
*
* @param inputStream An instance of {@link InputStream}.
*
* @return A {@link String}
*
* @throws IOException If the {@code inputStream} is unable to be read properly.
* @see "http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string"
*/
private String toString(final InputStream inputStream, final int lengthToRead)
throws IOException {
Objects.requireNonNull(inputStream);
ByteArrayOutputStream result = new ByteArrayOutputStream();
// Read lengthToRead bytes from the inputStream into the buffer...
byte[] buffer = new byte[lengthToRead];
int read = inputStream.read(buffer);
if (read != lengthToRead) {
throw new IOException(
"error reading " + lengthToRead + " bytes from stream, only read " + read);
}
result.write(buffer, 0, lengthToRead);
return result.toString(StandardCharsets.US_ASCII.name());
}
示例4: validateSnapshot
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private void validateSnapshot(boolean expectSuccess) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
PrintStream original = System.out;
try {
System.setOut(ps);
String args[] = new String[] { TESTNONCE, "--dir", TMPDIR };
SnapshotVerifier.main(args);
ps.flush();
String reportString = baos.toString("UTF-8");
// System.err.println("Validate Snapshot :"+reportString);
if (expectSuccess) {
assertTrue(reportString.startsWith("Snapshot valid\n"));
System.err.println("Validate Snapshot :" + "Snapshot valid");
} else {
assertTrue(reportString.startsWith("Snapshot corrupted\n"));
System.err.println("Validate Snapshot :" + "Snapshot corrupted");
}
} catch (UnsupportedEncodingException e) {
} finally {
System.setOut(original);
}
}
示例5: reloadList
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public void reloadList() throws IOException{
clear();
AssetInfo info=_AM.locateAsset(new AssetKey(_AM_PATH+"substances.json"));
if(info!=null){
InputStream is=info.openStream();
ByteArrayOutputStream bos=new ByteArrayOutputStream();
byte chunk[]=new byte[1024*1024];
int readed;
while((readed=is.read(chunk))!=-1)bos.write(chunk,0,readed);
String json=bos.toString("UTF-8");
Map<Object,Object> map=(Map)_JSON.parse(json);
for(java.util.Map.Entry<Object,Object> e:map.entrySet()){
Substance s=new Substance();
s.setSubstancesList(this);
s.putAll((Map)e.getValue());
put((String)e.getKey(),s);
}
}else{
LOGGER.log(Level.FINE,"substances.json not found");
}
}
示例6: decompressForZip
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/**
* Zip解压数据
*
* @param zipStr
* @return
*/
public static String decompressForZip(String zipStr) {
if (TextUtils.isEmpty(zipStr)) {
return "0";
}
byte[] t = Base64.decode(zipStr, Base64.DEFAULT);
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(t);
ZipInputStream zip = new ZipInputStream(in);
zip.getNextEntry();
byte[] buffer = new byte[1024];
int n = 0;
while ((n = zip.read(buffer, 0, buffer.length)) > 0) {
out.write(buffer, 0, n);
}
zip.close();
in.close();
out.close();
return out.toString();
} catch (IOException e) {
e.printStackTrace();
}
return "0";
}
示例7: gzipUncompressToString
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public static String gzipUncompressToString(byte[] b) {
if (b == null || b.length == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(b);
try {
GZIPInputStream gunzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
} catch (IOException e) {
e.printStackTrace();
}
return out.toString();
}
示例8: runTool
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private String runTool(Configuration conf, String[] args, boolean success)
throws Exception {
ByteArrayOutputStream o = new ByteArrayOutputStream();
PrintStream out = new PrintStream(o, true);
try {
int ret = ToolRunner.run(getTool(out), args);
assertEquals(success, ret == 0);
return o.toString();
} finally {
o.close();
out.close();
}
}
示例9: BytyToString
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public static String BytyToString(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int num = 0;
while ((num = in.read(buf)) != -1) {
out.write(buf, 0, buf.length);
}
String result = out.toString();
out.close();
return result;
}
示例10: dump
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private static void dump(ByteArrayBuffer buf, String caption, Map<String, List<String>> headers) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(baos, true);
pw.println("---["+caption +"]---");
if (headers != null) {
for (Entry<String, List<String>> header : headers.entrySet()) {
if (header.getValue().isEmpty()) {
// I don't think this is legal, but let's just dump it,
// as the point of the dump is to uncover problems.
pw.println(header.getValue());
} else {
for (String value : header.getValue()) {
pw.println(header.getKey() + ": " + value);
}
}
}
}
if (buf.size() > dump_threshold) {
byte[] b = buf.getRawData();
baos.write(b, 0, dump_threshold);
pw.println();
pw.println(WsservletMessages.MESSAGE_TOO_LONG(HttpAdapter.class.getName() + ".dumpTreshold"));
} else {
buf.writeTo(baos);
}
pw.println("--------------------");
String msg = baos.toString();
if (dump) {
System.out.println(msg);
}
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.log(Level.FINER, msg);
}
}
示例11: testJsonLdCustomSerializer
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
@Test
public void testJsonLdCustomSerializer() throws UnsupportedEncodingException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
service.write(getTriples(), out, JSONLD, rdf.createIRI("http://www.w3.org/ns/anno.jsonld"));
final String output = out.toString("UTF-8");
assertTrue(output.contains("\"dcterms:title\":\"A title\""));
assertTrue(output.contains("\"type\":\"Text\""));
assertTrue(output.contains("\"@context\":"));
assertTrue(output.contains("\"@context\":\"http://www.w3.org/ns/anno.jsonld\""));
assertFalse(output.contains("\"@graph\":"));
final Graph graph = rdf.createGraph();
service.read(new ByteArrayInputStream(output.getBytes(UTF_8)), null, JSONLD).forEach(graph::add);
validateGraph(graph);
}
示例12: taskResultIndexMapping
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public String taskResultIndexMapping() {
try (InputStream is = getClass().getResourceAsStream(TASK_RESULT_INDEX_MAPPING_FILE)) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.copy(is, out);
return out.toString(IOUtils.UTF_8);
} catch (Exception e) {
logger.error(
(Supplier<?>) () -> new ParameterizedMessage(
"failed to create tasks results index template [{}]", TASK_RESULT_INDEX_MAPPING_FILE), e);
throw new IllegalStateException("failed to create tasks results index template [" + TASK_RESULT_INDEX_MAPPING_FILE + "]", e);
}
}
示例13: convertStreamToString
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private String convertStreamToString(File file) throws IOException {
InputStream is = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = -1;
while ((i = is.read()) != -1) {
baos.write(i);
}
is.close();
return baos.toString();
}
示例14: testToJsonWithTags
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
@Test
public void testToJsonWithTags() throws Exception {
long epoch = System.currentTimeMillis();
long value = 1515;
DataPoint dataPoint = new DataPoint(tagEncodedMetricName.getMetricName(),
epoch, value, tagEncodedMetricName.getTags());
ByteArrayOutputStream out = new ByteArrayOutputStream();
dataPoint.toJson(new PrintStream(out), null);
String jsonTxt = out.toString();
DataPoint dp = Util.jsonToDataPoint(jsonTxt);
assertEquals(dataPoint, dp);
}
示例15: byteToString
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/**
* 输入流转为 String
*
* @param in InputStream
* @return String
*/
public static String byteToString(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int num = 0;
while ((num = in.read(buf)) != -1) {
out.write(buf, 0, buf.length);
}
String result = out.toString();
out.close();
return result;
}