List of usage examples for java.io DataInputStream read
public int read() throws IOException
From source file:org.apache.wink.itest.standard.JAXRSDataSourceTest.java
/** * Tests putting and then getting a DataSource entity. * /*ww w . j a va 2 s. c o m*/ * @throws HttpException * @throws IOException */ public void testPutDataSource() throws HttpException, IOException { HttpClient client = new HttpClient(); PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/datasource"); byte[] barr = new byte[1000]; Random r = new Random(); r.nextBytes(barr); putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr), "bytes/array")); try { client.executeMethod(putMethod); assertEquals(204, putMethod.getStatusCode()); } finally { putMethod.releaseConnection(); } GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/datasource"); try { client.executeMethod(getMethod); assertEquals(200, getMethod.getStatusCode()); InputStream is = getMethod.getResponseBodyAsStream(); byte[] receivedBArr = new byte[1000]; DataInputStream dis = new DataInputStream(is); dis.readFully(receivedBArr); int checkEOF = dis.read(); assertEquals(-1, checkEOF); for (int c = 0; c < barr.length; ++c) { assertEquals(barr[c], receivedBArr[c]); } String contentType = (getMethod.getResponseHeader("Content-Type") == null) ? null : getMethod.getResponseHeader("Content-Type").getValue(); assertNotNull(contentType, contentType); assertNull( (getMethod.getResponseHeader("Content-Length") == null) ? "" : getMethod.getResponseHeader("Content-Length").getValue(), getMethod.getResponseHeader("Content-Length")); } finally { getMethod.releaseConnection(); } }
From source file:com.vrs.qrw100s.NetworkReader.java
@Override public void run() { Thread.currentThread().setName("Network Reader"); HttpResponse res;// www. j av a2s .co m DefaultHttpClient httpclient = new DefaultHttpClient(); HttpParams httpParams = httpclient.getParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 5 * 1000); HttpConnectionParams.setSoTimeout(httpParams, 10 * 1000); Log.d(TAG, "1. Sending http request"); try { res = httpclient.execute(new HttpGet(URI.create(myURL))); Log.d(TAG, "2. Request finished, status = " + res.getStatusLine().getStatusCode()); if (res.getStatusLine().getStatusCode() == 401) { return; } DataInputStream bis = new DataInputStream(res.getEntity().getContent()); ByteArrayOutputStream jpgOut = new ByteArrayOutputStream(10000); int prev = 0; int cur; while ((cur = bis.read()) >= 0 && _runThread) { if (prev == 0xFF && cur == 0xD8) { // reset the output stream if (!skipFrame) { jpgOut.reset(); jpgOut.write((byte) prev); } } if (!skipFrame) { if (jpgOut != null) { jpgOut.write((byte) cur); } } if (prev == 0xFF && cur == 0xD9) { if (!skipFrame) { synchronized (curFrame) { curFrame = jpgOut.toByteArray(); } skipFrame = true; Message threadMessage = mainHandler.obtainMessage(); threadMessage.obj = curFrame; mainHandler.sendMessage(threadMessage); } else { if (skipNum < frameDecrement) { skipNum++; } else { skipNum = 0; skipFrame = false; } } } prev = cur; } } catch (ClientProtocolException e) { Log.d(TAG, "Request failed-ClientProtocolException", e); } catch (IOException e) { Log.d(TAG, "Request failed-IOException", e); } }
From source file:HttpGETMIDlet.java
private String requestUsingGET(String URLString) throws IOException { HttpConnection hpc = null;//from w w w . j ava 2 s .c om DataInputStream dis = null; boolean newline = false; String content = ""; try { hpc = (HttpConnection) Connector.open(URLString); dis = new DataInputStream(hpc.openInputStream()); int character; while ((character = dis.read()) != -1) { if ((char) character == '\\') { newline = true; continue; } else { if ((char) character == 'n' && newline) { content += "\n"; newline = false; } else if (newline) { content += "\\" + (char) character; newline = false; } else { content += (char) character; newline = false; } } } if (hpc != null) hpc.close(); if (dis != null) dis.close(); } catch (IOException e2) { } return content; }
From source file:com.yahoo.pulsar.testclient.LoadSimulationClient.java
private void handle(final Socket socket) throws Exception { final DataInputStream inputStream = new DataInputStream(socket.getInputStream()); int command;/*w ww . ja va 2s . co m*/ while ((command = inputStream.read()) != -1) { handle((byte) command, inputStream, new DataOutputStream(socket.getOutputStream())); } }
From source file:org.dcm4che3.tool.jpg2dcm.Jpg2Dcm.java
private void readHeader(Attributes attrs, DataInputStream jpgInput) throws IOException { if (jpgInput.read() != FF || jpgInput.read() != SOI || jpgInput.read() != FF) { throw new IOException("JPEG stream does not start with FF D8 FF"); }// w ww .j a v a 2 s . c o m int marker = jpgInput.read(); int segmLen; boolean seenSOF = false; buffer[0] = (byte) FF; buffer[1] = (byte) SOI; buffer[2] = (byte) FF; buffer[3] = (byte) marker; jpgHeaderLen = 4; while (marker != SOS) { segmLen = jpgInput.readUnsignedShort(); if (buffer.length < jpgHeaderLen + segmLen + 2) { growBuffer(jpgHeaderLen + segmLen + 2); } buffer[jpgHeaderLen++] = (byte) (segmLen >>> 8); buffer[jpgHeaderLen++] = (byte) segmLen; jpgInput.readFully(buffer, jpgHeaderLen, segmLen - 2); if ((marker & 0xf0) == SOF && marker != DHT && marker != DAC) { seenSOF = true; int p = buffer[jpgHeaderLen] & 0xff; int y = ((buffer[jpgHeaderLen + 1] & 0xff) << 8) | (buffer[jpgHeaderLen + 2] & 0xff); int x = ((buffer[jpgHeaderLen + 3] & 0xff) << 8) | (buffer[jpgHeaderLen + 4] & 0xff); int nf = buffer[jpgHeaderLen + 5] & 0xff; attrs.setInt(Tag.SamplesPerPixel, VR.US, nf); if (nf == 3) { attrs.setString(Tag.PhotometricInterpretation, VR.CS, "YBR_FULL_422"); attrs.setInt(Tag.PlanarConfiguration, VR.US, 0); } else { attrs.setString(Tag.PhotometricInterpretation, VR.CS, "MONOCHROME2"); } attrs.setInt(Tag.Rows, VR.US, y); attrs.setInt(Tag.Columns, VR.US, x); attrs.setInt(Tag.BitsAllocated, VR.US, p > 8 ? 16 : 8); attrs.setInt(Tag.BitsStored, VR.US, p); attrs.setInt(Tag.HighBit, VR.US, p - 1); attrs.setInt(Tag.PixelRepresentation, VR.US, 0); } if (noAPPn & (marker & 0xf0) == APP) { jpgLen -= segmLen + 2; jpgHeaderLen -= 4; } else { jpgHeaderLen += segmLen - 2; } if (jpgInput.read() != FF) { throw new IOException("Missing SOS segment in JPEG stream"); } marker = jpgInput.read(); buffer[jpgHeaderLen++] = (byte) FF; buffer[jpgHeaderLen++] = (byte) marker; } if (!seenSOF) { throw new IOException("Missing SOF segment in JPEG stream"); } }
From source file:com.intel.chimera.CryptoCodecTest.java
private void cryptoCodecTestForInputStream(int count, String encCodecClass, String decCodecClass, byte[] iv) throws IOException { CryptoCodec encCodec = null;//w w w . j av a 2 s . c o m try { encCodec = (CryptoCodec) ReflectionUtils.newInstance(ReflectionUtils.getClassByName(encCodecClass), props); } catch (ClassNotFoundException cnfe) { throw new IOException("Illegal crypto codec!"); } LOG.info("Created a Codec object of type: " + encCodecClass); // Generate data SecureRandom random = new SecureRandom(); byte[] originalData = new byte[count]; byte[] decryptedData = new byte[count]; random.nextBytes(originalData); LOG.info("Generated " + count + " records"); // Encrypt data ByteArrayOutputStream encryptedData = new ByteArrayOutputStream(); CryptoOutputStream out = new CryptoOutputStream(encryptedData, encCodec, bufferSize, key, iv); out.write(originalData, 0, originalData.length); out.flush(); out.close(); LOG.info("Finished encrypting data"); CryptoCodec decCodec = null; try { decCodec = (CryptoCodec) ReflectionUtils.newInstance(ReflectionUtils.getClassByName(decCodecClass), props); } catch (ClassNotFoundException cnfe) { throw new IOException("Illegal crypto codec!"); } LOG.info("Created a Codec object of type: " + decCodecClass); // Decrypt data CryptoInputStream in = new CryptoInputStream(new ByteArrayInputStream(encryptedData.toByteArray()), decCodec, bufferSize, key, iv); // Check int remainingToRead = count; int offset = 0; while (remainingToRead > 0) { int n = in.read(decryptedData, offset, decryptedData.length - offset); if (n >= 0) { remainingToRead -= n; offset += n; } } Assert.assertArrayEquals("originalData and decryptedData not equal", originalData, decryptedData); // Decrypt data byte-at-a-time in = new CryptoInputStream(new ByteArrayInputStream(encryptedData.toByteArray()), decCodec, bufferSize, key, iv); // Check DataInputStream originalIn = new DataInputStream( new BufferedInputStream(new ByteArrayInputStream(originalData))); int expected; do { expected = originalIn.read(); Assert.assertEquals("Decrypted stream read by byte does not match", expected, in.read()); } while (expected != -1); LOG.info("SUCCESS! Completed checking " + count + " records"); }
From source file:com.intel.chimera.CryptoCodecTest.java
private void cryptoCodecTestForReadableByteChannel(int count, String encCodecClass, String decCodecClass, byte[] iv) throws IOException { CryptoCodec encCodec = null;/* w w w.j a v a 2 s . com*/ try { encCodec = (CryptoCodec) ReflectionUtils.newInstance(ReflectionUtils.getClassByName(encCodecClass), props); } catch (ClassNotFoundException cnfe) { throw new IOException("Illegal crypto codec!"); } LOG.info("Created a Codec object of type: " + encCodecClass); // Generate data SecureRandom random = new SecureRandom(); byte[] originalData = new byte[count]; byte[] decryptedData = new byte[count]; random.nextBytes(originalData); LOG.info("Generated " + count + " records"); // Encrypt data ByteArrayOutputStream encryptedData = new ByteArrayOutputStream(); CryptoOutputStream out = new CryptoOutputStream(Channels.newChannel(encryptedData), encCodec, bufferSize, key, iv); out.write(originalData, 0, originalData.length); out.flush(); out.close(); LOG.info("Finished encrypting data"); CryptoCodec decCodec = null; try { decCodec = (CryptoCodec) ReflectionUtils.newInstance(ReflectionUtils.getClassByName(decCodecClass), props); } catch (ClassNotFoundException cnfe) { throw new IOException("Illegal crypto codec!"); } LOG.info("Created a Codec object of type: " + decCodecClass); // Decrypt data CryptoInputStream in = new CryptoInputStream( Channels.newChannel(new ByteArrayInputStream(encryptedData.toByteArray())), decCodec, bufferSize, key, iv); // Check int remainingToRead = count; int offset = 0; while (remainingToRead > 0) { int n = in.read(decryptedData, offset, decryptedData.length - offset); if (n >= 0) { remainingToRead -= n; offset += n; } } Assert.assertArrayEquals("originalData and decryptedData not equal", originalData, decryptedData); // Decrypt data byte-at-a-time in = new CryptoInputStream(Channels.newChannel(new ByteArrayInputStream(encryptedData.toByteArray())), decCodec, bufferSize, key, iv); // Check DataInputStream originalIn = new DataInputStream( new BufferedInputStream(new ByteArrayInputStream(originalData))); int expected; do { expected = originalIn.read(); Assert.assertEquals("Decrypted stream read by byte does not match", expected, in.read()); } while (expected != -1); LOG.info("SUCCESS! Completed checking " + count + " records"); }
From source file:com.intel.chimera.StreamCipherTest.java
private void cryptoCipherTestForInputStream(int count, String encCipherClass, String decCipherClass, byte[] iv) throws IOException { Cipher encCipher = null;//from w w w . jav a 2 s .c om try { encCipher = (Cipher) ReflectionUtils.newInstance(ReflectionUtils.getClassByName(encCipherClass), props, transformation); } catch (ClassNotFoundException cnfe) { throw new IOException("Illegal crypto cipher!"); } LOG.info("Created a cipher object of type: " + encCipherClass); // Generate data SecureRandom random = new SecureRandom(); byte[] originalData = new byte[count]; byte[] decryptedData = new byte[count]; random.nextBytes(originalData); LOG.info("Generated " + count + " records"); // Encrypt data ByteArrayOutputStream encryptedData = new ByteArrayOutputStream(); CTRCryptoOutputStream out = new CTRCryptoOutputStream(encryptedData, encCipher, bufferSize, key, iv); out.write(originalData, 0, originalData.length); out.flush(); out.close(); LOG.info("Finished encrypting data"); Cipher decCipher = null; try { decCipher = (Cipher) ReflectionUtils.newInstance(ReflectionUtils.getClassByName(decCipherClass), props, transformation); } catch (ClassNotFoundException cnfe) { throw new IOException("Illegal crypto cipher!"); } LOG.info("Created a cipher object of type: " + decCipherClass); // Decrypt data CTRCryptoInputStream in = new CTRCryptoInputStream(new ByteArrayInputStream(encryptedData.toByteArray()), decCipher, bufferSize, key, iv); // Check int remainingToRead = count; int offset = 0; while (remainingToRead > 0) { int n = in.read(decryptedData, offset, decryptedData.length - offset); if (n >= 0) { remainingToRead -= n; offset += n; } } Assert.assertArrayEquals("originalData and decryptedData not equal", originalData, decryptedData); // Decrypt data byte-at-a-time in = new CTRCryptoInputStream(new ByteArrayInputStream(encryptedData.toByteArray()), decCipher, bufferSize, key, iv); // Check DataInputStream originalIn = new DataInputStream( new BufferedInputStream(new ByteArrayInputStream(originalData))); int expected; do { expected = originalIn.read(); Assert.assertEquals("Decrypted stream read by byte does not match", expected, in.read()); } while (expected != -1); LOG.info("SUCCESS! Completed checking " + count + " records"); }
From source file:com.intel.chimera.StreamCipherTest.java
private void cryptoCipherTestForReadableByteChannel(int count, String encCipherClass, String decCipherClass, byte[] iv) throws IOException { Cipher encCipher = null;// ww w.j a v a 2s.com try { encCipher = (Cipher) ReflectionUtils.newInstance(ReflectionUtils.getClassByName(encCipherClass), props, transformation); } catch (ClassNotFoundException cnfe) { throw new IOException("Illegal crypto cipher!"); } LOG.info("Created a cipher object of type: " + encCipherClass); // Generate data SecureRandom random = new SecureRandom(); byte[] originalData = new byte[count]; byte[] decryptedData = new byte[count]; random.nextBytes(originalData); LOG.info("Generated " + count + " records"); // Encrypt data ByteArrayOutputStream encryptedData = new ByteArrayOutputStream(); CTRCryptoOutputStream out = new CTRCryptoOutputStream(Channels.newChannel(encryptedData), encCipher, bufferSize, key, iv); out.write(originalData, 0, originalData.length); out.flush(); out.close(); LOG.info("Finished encrypting data"); Cipher decCipher = null; try { decCipher = (Cipher) ReflectionUtils.newInstance(ReflectionUtils.getClassByName(decCipherClass), props, transformation); } catch (ClassNotFoundException cnfe) { throw new IOException("Illegal crypto cipher!"); } LOG.info("Created a cipher object of type: " + decCipherClass); // Decrypt data CTRCryptoInputStream in = new CTRCryptoInputStream( Channels.newChannel(new ByteArrayInputStream(encryptedData.toByteArray())), decCipher, bufferSize, key, iv); // Check int remainingToRead = count; int offset = 0; while (remainingToRead > 0) { int n = in.read(decryptedData, offset, decryptedData.length - offset); if (n >= 0) { remainingToRead -= n; offset += n; } } Assert.assertArrayEquals("originalData and decryptedData not equal", originalData, decryptedData); // Decrypt data byte-at-a-time in = new CTRCryptoInputStream(Channels.newChannel(new ByteArrayInputStream(encryptedData.toByteArray())), decCipher, bufferSize, key, iv); // Check DataInputStream originalIn = new DataInputStream( new BufferedInputStream(new ByteArrayInputStream(originalData))); int expected; do { expected = originalIn.read(); Assert.assertEquals("Decrypted stream read by byte does not match", expected, in.read()); } while (expected != -1); LOG.info("SUCCESS! Completed checking " + count + " records"); }
From source file:com.ning.arecibo.util.timeline.times.TimelineCoderImpl.java
@Override public int countTimeBytesSamples(final byte[] timeBytes) { int count = 0; try {/*w ww . j a v a 2s . co m*/ final ByteArrayInputStream byteStream = new ByteArrayInputStream(timeBytes); final DataInputStream byteDataStream = new DataInputStream(byteStream); int opcode; while ((opcode = byteDataStream.read()) != -1) { if (opcode == TimelineOpcode.FULL_TIME.getOpcodeIndex()) { byteDataStream.readInt(); count++; } else if (opcode <= TimelineOpcode.MAX_DELTA_TIME) { count++; } else if (opcode == TimelineOpcode.REPEATED_DELTA_TIME_BYTE.getOpcodeIndex()) { count += byteDataStream.read(); byteDataStream.read(); } else if (opcode == TimelineOpcode.REPEATED_DELTA_TIME_SHORT.getOpcodeIndex()) { count += byteDataStream.readUnsignedShort(); byteDataStream.read(); } else { throw new IllegalStateException(String .format("In TimelineCoder.countTimeBytesSamples(), unrecognized opcode %d", opcode)); } } return count; } catch (IOException e) { log.error(e, "IOException while counting timeline samples"); return count; } }