List of usage examples for java.io DataOutputStream write
public synchronized void write(byte b[], int off, int len) throws IOException
len
bytes from the specified byte array starting at offset off
to the underlying output stream. From source file:Main.java
public static void main(String[] args) throws IOException { byte[] buf = { 87, 64, 72, 31, 90 }; ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.write(buf, 2, 3); dos.flush();// ww w . j a v a 2 s . c om for (byte b : baos.toByteArray()) { System.out.println(b); } }
From source file:Ch7_Images.java
public static void main(String[] args) { int numRows = 6, numCols = 11, pix = 20; PaletteData pd = new PaletteData( new RGB[] { new RGB(0x00, 0x00, 0x00), new RGB(0x80, 0x80, 0x80), new RGB(0xFF, 0xFF, 0xFF) }); ImageData[] flagArray = new ImageData[3]; for (int frame = 0; frame < flagArray.length; frame++) { flagArray[frame] = new ImageData(pix * numCols, pix * numRows, 4, pd); flagArray[frame].delayTime = 10; for (int x = 0; x < pix * numCols; x++) { for (int y = 0; y < pix * numRows; y++) { int value = (((x / pix) % 3) + (3 - ((y / pix) % 3)) + frame) % 3; flagArray[frame].setPixel(x, y, value); }/*from www. j a v a 2 s.c o m*/ } } ImageLoader gifloader = new ImageLoader(); ByteArrayOutputStream flagByte[] = new ByteArrayOutputStream[3]; byte[][] gifarray = new byte[3][]; gifloader.data = flagArray; for (int i = 0; i < 3; i++) { flagByte[i] = new ByteArrayOutputStream(); flagArray[0] = flagArray[i]; gifloader.save(flagByte[i], SWT.IMAGE_GIF); gifarray[i] = flagByte[i].toByteArray(); } byte[] gif = new byte[4628]; System.arraycopy(gifarray[0], 0, gif, 0, 61); System.arraycopy(new byte[] { 33, (byte) 255, 11 }, 0, gif, 61, 3); System.arraycopy("NETSCAPE2.0".getBytes(), 0, gif, 64, 11); System.arraycopy(new byte[] { 3, 1, -24, 3, 0, 33, -7, 4, -24 }, 0, gif, 75, 9); System.arraycopy(gifarray[0], 65, gif, 84, 1512); for (int i = 1; i < 3; i++) { System.arraycopy(gifarray[i], 61, gif, 1516 * i + 80, 3); gif[1516 * i + 83] = (byte) -24; System.arraycopy(gifarray[i], 65, gif, 1516 * i + 84, 1512); } try { DataOutputStream in = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(new File("FlagGIF.gif")))); in.write(gif, 0, gif.length); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
protected static void serialiseByteArray(DataOutputStream os, byte[] data, int start, int length, int max_length) throws IOException { serialiseLength(os, length, max_length); os.write(data, start, length); }
From source file:Main.java
public static String uploadFile(String filePath, String requestURL) { String result = ""; File file = new File(filePath); try {/* ww w. java 2 s .com*/ HttpURLConnection connection = initHttpURLConn(requestURL); if (filePath != null) { DataOutputStream dos = new DataOutputStream(connection.getOutputStream()); StringBuffer sb = new StringBuffer(); InputStream is = new FileInputStream(filePath); byte[] bytes = new byte[1024]; int len = 0; while ((len = is.read(bytes)) != -1) { dos.write(bytes, 0, len); } dos.flush(); is.close(); int res = connection.getResponseCode(); Log.e(TAG, "response code:" + res); if (res == 200) { Log.e(TAG, "request success"); InputStream input = connection.getInputStream(); StringBuffer sb1 = new StringBuffer(); int ss; while ((ss = input.read()) != -1) { sb1.append((char) ss); } result = sb1.toString(); Log.d(TAG, "result: " + result); input.close(); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; }
From source file:Main.java
public static void post(String actionUrl, String file) { try {//from w w w . jav a 2s . c om URL url = new URL(actionUrl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); con.setRequestMethod("POST"); con.setRequestProperty("Charset", "UTF-8"); con.setRequestProperty("Content-Type", "multipart/form-data;boundary=*****"); DataOutputStream ds = new DataOutputStream(con.getOutputStream()); FileInputStream fStream = new FileInputStream(file); int bufferSize = 1024; // 1MB byte[] buffer = new byte[bufferSize]; int bufferLength = 0; int length; while ((length = fStream.read(buffer)) != -1) { bufferLength = bufferLength + 1; ds.write(buffer, 0, length); } fStream.close(); ds.flush(); InputStream is = con.getInputStream(); int ch; StringBuilder b = new StringBuilder(); while ((ch = is.read()) != -1) { b.append((char) ch); } new String(b.toString().getBytes("ISO-8859-1"), "utf-8"); ds.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.adaptris.security.StdOutput.java
private static void write(DataOutputStream out, byte[] bytes) throws IOException { if (bytes == null) { out.writeInt(0);/* w w w . ja va 2s . co m*/ } else { out.writeInt(bytes.length); out.write(bytes, 0, bytes.length); } }
From source file:com.almarsoft.GroundhogReader.lib.FSUtils.java
public static long writeInputStreamAndGetSize(String directory, String filename, InputStream is) throws IOException { File outDir = new File(directory); if (!outDir.exists()) outDir.mkdirs();/*from ww w . j av a 2s .c om*/ File outFile = new File(directory, filename); DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outFile))); byte buf[] = new byte[1024]; int len; while ((len = is.read(buf)) > 0) dos.write(buf, 0, len); dos.close(); return outFile.length(); }
From source file:org.apache.hadoop.io.compress.TestCodec.java
private static void codecTest(Configuration conf, int seed, int count, String codecClass) throws IOException { // Create the codec CompressionCodec codec = null;//from w ww. ja va 2 s . co m try { codec = (CompressionCodec) ReflectionUtils.newInstance(conf.getClassByName(codecClass), conf); } catch (ClassNotFoundException cnfe) { throw new IOException("Illegal codec!"); } LOG.info("Created a Codec object of type: " + codecClass); // Generate data DataOutputBuffer data = new DataOutputBuffer(); RandomDatum.Generator generator = new RandomDatum.Generator(seed); for (int i = 0; i < count; ++i) { generator.next(); RandomDatum key = generator.getKey(); RandomDatum value = generator.getValue(); key.write(data); value.write(data); } DataInputBuffer originalData = new DataInputBuffer(); DataInputStream originalIn = new DataInputStream(new BufferedInputStream(originalData)); originalData.reset(data.getData(), 0, data.getLength()); LOG.info("Generated " + count + " records"); // Compress data DataOutputBuffer compressedDataBuffer = new DataOutputBuffer(); CompressionOutputStream deflateFilter = codec.createOutputStream(compressedDataBuffer); DataOutputStream deflateOut = new DataOutputStream(new BufferedOutputStream(deflateFilter)); deflateOut.write(data.getData(), 0, data.getLength()); deflateOut.flush(); deflateFilter.finish(); LOG.info("Finished compressing data"); // De-compress data DataInputBuffer deCompressedDataBuffer = new DataInputBuffer(); deCompressedDataBuffer.reset(compressedDataBuffer.getData(), 0, compressedDataBuffer.getLength()); CompressionInputStream inflateFilter = codec.createInputStream(deCompressedDataBuffer); DataInputStream inflateIn = new DataInputStream(new BufferedInputStream(inflateFilter)); // Check for (int i = 0; i < count; ++i) { RandomDatum k1 = new RandomDatum(); RandomDatum v1 = new RandomDatum(); k1.readFields(originalIn); v1.readFields(originalIn); RandomDatum k2 = new RandomDatum(); RandomDatum v2 = new RandomDatum(); k2.readFields(inflateIn); v2.readFields(inflateIn); assertTrue("original and compressed-then-decompressed-output not equal", k1.equals(k2) && v1.equals(v2)); } LOG.info("SUCCESS! Completed checking " + count + " records"); }
From source file:Main.java
/** * The URL looks like a request for a resource, such as a JavaScript or CSS file. Write * the given resource to the response output in binary format (needed for images). *//*ww w. j a v a 2 s .c o m*/ public static void readWriteBinaryUtil(String sxURL, String resource, OutputStream outStream) throws IOException { DataOutputStream outData = null; DataInputStream inData = null; int byteCnt = 0; byte[] buffer = new byte[4096]; // get full qualified path of class System.out.println("RW Base Directory - " + sxURL); // remove class and add resource relative path sxURL = getResourceURL(sxURL, resource); System.out.println("RW Loading - " + sxURL); try { outData = new DataOutputStream(outStream); inData = new DataInputStream(new URL(sxURL).openConnection().getInputStream()); while ((byteCnt = inData.read(buffer)) != -1) { if (outData != null && byteCnt > 0) { outData.write(buffer, 0, byteCnt); } } } catch (IOException e) { throw e; } finally { try { if (inData != null) { inData.close(); } } catch (IOException ioe) { } } }
From source file:Main.java
private static byte[] getBytesFromFile(File file) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream); try {/* w w w . ja v a2 s . c om*/ FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } int maxBufferSize = 1024 * 1024; int bufferSize = (int) Math.min(file.getTotalSpace(), maxBufferSize); byte[] buffer = new byte[bufferSize]; // read file and write it into form... int bytesRead = 0; if (fileInputStream != null) { bytesRead = fileInputStream.read(buffer, 0, bufferSize); } while (bytesRead > 0) { dataOutputStream.write(buffer, 0, bufferSize); int bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } } catch (Exception e) { e.printStackTrace(); } return byteArrayOutputStream.toByteArray(); }