List of usage examples for java.io ByteArrayOutputStream write
public synchronized void write(int b)
From source file:Main.java
public static String loadFromAssetsFile(String fname, Context context) { String result = null;/*from ww w . j a v a 2s . c o m*/ try { InputStream in = context.getAssets().open(fname); int ch = 0; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((ch = in.read()) != -1) { baos.write(ch); } byte[] buff = baos.toByteArray(); baos.close(); in.close(); result = new String(buff, "UTF-8"); result = result.replaceAll("\\r\\n", "\n"); } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:Main.java
public static String drain(InputStream inputStream) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int buffer;/*from w w w. j a va 2 s.c o m*/ while ((buffer = inputStream.read()) >= 0) { baos.write(buffer); } return baos.toString(); }
From source file:Main.java
public static String readStream(InputStream is) { try {//from ww w.j av a 2s. co m ByteArrayOutputStream bo = new ByteArrayOutputStream(); int i = is.read(); while (i != -1) { bo.write(i); i = is.read(); } return bo.toString(); } catch (IOException e) { return ""; } }
From source file:Main.java
private static String readStream(InputStream is) { try {// w w w .j ava 2 s .co m ByteArrayOutputStream bo = new ByteArrayOutputStream(); int i = is.read(); while (i != -1) { bo.write(i); i = is.read(); } return bo.toString(); } catch (IOException e) { e.printStackTrace(); return ""; } }
From source file:Main.java
public static byte[] combine(byte[]... elements) { try {//from ww w .ja v a2 s .co m ByteArrayOutputStream baos = new ByteArrayOutputStream(); for (byte[] element : elements) { baos.write(element); } return baos.toByteArray(); } catch (IOException e) { throw new AssertionError(e); } }
From source file:Main.java
public static String readString(String filePath) { File file = new File(filePath); if (!file.exists()) return null; FileInputStream fileInput = null; FileChannel channel = null;//from w w w. j a v a 2 s. c o m try { fileInput = new FileInputStream(filePath); channel = fileInput.getChannel(); ByteBuffer buffer = ByteBuffer.allocate((int) channel.size()); channel.read(buffer); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byteArrayOutputStream.write(buffer.array()); return byteArrayOutputStream.toString(); } catch (Exception e) { } finally { if (fileInput != null) { try { fileInput.close(); } catch (IOException e) { e.printStackTrace(); } } if (channel != null) { try { channel.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }
From source file:Main.java
public static Image getImage(Class relativeClass, String filename) { Image returnValue = null;/*www. j a v a 2s . com*/ InputStream is = relativeClass.getResourceAsStream(filename); if (is != null) { BufferedInputStream bis = new BufferedInputStream(is); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { int ch; while ((ch = bis.read()) != -1) { baos.write(ch); } Toolkit t = Toolkit.getDefaultToolkit(); returnValue = t.createImage(baos.toByteArray()); } catch (IOException exception) { System.err.println("Error loading: " + filename); } } return returnValue; }
From source file:Main.java
public static String inputStreamToString(InputStream inputStream) { if (inputStream == null) { System.out.println("InputStream is null"); return null; }/*from w w w . j a v a 2s . c o m*/ try { int i = -1; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((i = inputStream.read()) != -1) { baos.write(i); } String content = baos.toString(); return content; } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static byte[] readInputStream(InputStream inputStream) throws IOException { byte[] buffer = new byte[4096]; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(4096); // Do the first byte via a blocking read outputStream.write(inputStream.read()); // Slurp the rest int available = 0;// inputStream.available(); boolean run = true; while (run && (available = inputStream.available()) > 0) { // Log.d(TAG, "slurp " + available); while (available > 0) { int cbToRead = Math.min(buffer.length, available); int cbRead = inputStream.read(buffer, 0, cbToRead); if (cbRead <= 0) { run = false;//from w w w . jav a 2 s . c o m break; } outputStream.write(buffer, 0, cbRead); available -= cbRead; } } return outputStream.toByteArray(); }
From source file:haxball.util.Serializer.java
public static byte[] serializeState(@NonNull Ball ball, byte score0, byte score1, @NonNull Collection<Player> players) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write(0xff); baos.write(score0);//from www .j a v a 2s . c om baos.write(score1); baos.write(serializePoint(ball.position), 0, 8); baos.write(serializePoint(ball.velocity), 0, 8); for (Player p : players) { baos.write(p.getId()); baos.write(p.isShooting() ? 0x01 : 0x02); baos.write(serializePoint(p.position), 0, 8); baos.write(serializePoint(p.velocity), 0, 8); } baos.write(0x00); return baos.toByteArray(); }