List of usage examples for java.io DataInputStream readByte
public final byte readByte() throws IOException
readByte
method of DataInput
. From source file:org.nuxeo.ecm.core.blob.binary.AESBinaryManager.java
/** * Decrypts the given input stream into the given output stream. *//*from ww w . java 2 s. c om*/ protected void decrypt(InputStream in, OutputStream out) throws IOException { byte[] magic = new byte[FILE_MAGIC.length]; IOUtils.read(in, magic); if (!Arrays.equals(magic, FILE_MAGIC)) { throw new IOException("Invalid file (bad magic)"); } DataInputStream data = new DataInputStream(in); byte magicvers = data.readByte(); if (magicvers != FILE_VERSION_1) { throw new IOException("Invalid file (bad version)"); } byte usepb = data.readByte(); if (usepb == USE_PBKDF2) { if (!usePBKDF2) { throw new NuxeoException("File requires PBKDF2 password"); } } else if (usepb == USE_KEYSTORE) { if (usePBKDF2) { throw new NuxeoException("File requires keystore"); } } else { throw new IOException("Invalid file (bad use)"); } try { // secret key Key secret; if (usePBKDF2) { // read salt first int saltLen = data.readInt(); if (saltLen <= 0 || saltLen > MAX_SALT_LEN) { throw new NuxeoException("Invalid salt length: " + saltLen); } byte[] salt = new byte[saltLen]; data.read(salt, 0, saltLen); secret = generateSecretKey(salt); } else { secret = getSecretKey(); } // read IV int ivLen = data.readInt(); if (ivLen <= 0 || ivLen > MAX_IV_LEN) { throw new NuxeoException("Invalid IV length: " + ivLen); } byte[] iv = new byte[ivLen]; data.read(iv, 0, ivLen); // cipher Cipher cipher; cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING); cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv)); // read the encrypted data try (InputStream cipherIn = new CipherInputStream(in, cipher)) { IOUtils.copy(cipherIn, out); } catch (IOException e) { Throwable cause = e.getCause(); if (cause != null && cause instanceof BadPaddingException) { throw new NuxeoException(cause.getMessage(), e); } } } catch (GeneralSecurityException e) { throw new NuxeoException(e); } }
From source file:org.apache.hadoop.io.TestBufferedByteInputOutput.java
private void testInputStream(int inputSize, int bufferSize, int readBufferSize) throws IOException { LOG.info("Running test input stream with inputSize: " + inputSize + ", bufferSize: " + bufferSize + ", readBufferSize: " + readBufferSize); setUp(inputSize);//from w w w . java2 s. c o m ByteArrayInputStream is = new ByteArrayInputStream(input); DataInputStream dis = BufferedByteInputStream.wrapInputStream(is, bufferSize, readBufferSize); int totalRead = 0; int outputCursor = 0; while (totalRead < inputSize) { if (rand.nextBoolean()) { // read single byte output[outputCursor++] = dis.readByte(); totalRead++; } else { int count = rand.nextInt(inputSize - totalRead) + 1; byte[] bytes = new byte[count]; int bytesRead = dis.read(bytes, 0, count); System.arraycopy(bytes, 0, output, outputCursor, bytesRead); outputCursor += bytesRead; totalRead += bytesRead; } } assertEquals(inputSize, totalRead); assertTrue(Arrays.equals(input, output)); dis.close(); dis.close(); // multiple close should work dis.close(); // multiple close should work }
From source file:org.pentaho.di.job.entries.filecompare.JobEntryFileCompare.java
/** * Check whether 2 files have the same contents. * * @param file1/*from ww w . j ava 2 s . c o m*/ * first file to compare * @param file2 * second file to compare * @return true if files are equal, false if they are not * * @throws IOException * upon IO problems */ protected boolean equalFileContents(FileObject file1, FileObject file2) throws KettleFileException { // Really read the contents and do comparisons DataInputStream in1 = null; DataInputStream in2 = null; try { in1 = new DataInputStream( new BufferedInputStream(KettleVFS.getInputStream(KettleVFS.getFilename(file1), this))); in2 = new DataInputStream( new BufferedInputStream(KettleVFS.getInputStream(KettleVFS.getFilename(file2), this))); char ch1, ch2; while (in1.available() != 0 && in2.available() != 0) { ch1 = (char) in1.readByte(); ch2 = (char) in2.readByte(); if (ch1 != ch2) { return false; } } if (in1.available() != in2.available()) { return false; } else { return true; } } catch (IOException e) { throw new KettleFileException(e); } finally { if (in1 != null) { try { in1.close(); } catch (IOException ignored) { // Nothing to do here } } if (in2 != null) { try { in2.close(); } catch (IOException ignored) { // Nothing to see here... } } } }
From source file:com.isecpartners.gizmo.HttpRequest.java
private void readerToStringBuffer(DataInputStream buffered, StringBuffer contents) { try {//from ww w. ja va 2s. c om byte ch_n = buffered.readByte(); while (ch_n != -1) { contents.append((char) ch_n); if ((contents.indexOf("\r\n\r\n") != -1) || (buffered.available() == 0)) { break; } ch_n = buffered.readByte(); } } catch (Exception ex) { System.out.println(ex); } }
From source file:ch.unil.genescore.vegas.Snp.java
/** * Read this snp from the binary file. Format is: * 1. genotypes_.length//from w ww . jav a 2 s.co m * 2. genotypes_ * 3. maf_ * 4. alleleSd_ * 5. alleleMean_ * @throws IOException */ public void readGenotype(DataInputStream is) throws IOException { // id_ is already read genotypes_ = new byte[genotypeLength_]; for (int i = 0; i < genotypeLength_; i++) genotypes_[i] = is.readByte(); maf_ = is.readDouble(); alleleMean_ = is.readDouble(); alleleSd_ = is.readDouble(); }
From source file:de.tum.frm2.nicos_android.nicos.NicosClient.java
public TupleOfTwo<Byte, Object> _read() throws ProtocolError { // receive first byte + (possibly) length DataInputStream din = new DataInputStream(socketIn); byte start;// w w w.j a v a 2 s . c o m try { start = din.readByte(); } catch (IOException e) { throw new ProtocolError("connection broken"); } if (start == daemon.ACK) { // ACK == executed ok, no more information follows return new TupleOfTwo<>(start, null); } if (start != daemon.NAK && start != daemon.STX) { // Server respondend with neither NAK (error) nor STX (ok) throw new ProtocolError("invalid response " + String.valueOf(start)); } // it has a length... int length; try { length = din.readInt(); } catch (IOException e) { throw new ProtocolError("connection broken"); } // Cannot concat these two try blocks: We need length before allocating msg. // And msg needs to be declared outside of try block to be accessible afterwards. try { byte[] msg = new byte[length]; try { din.readFully(msg, 0, length); } catch (IOException e) { throw new ProtocolError("connection broken"); } Unpickler unpickler = new Unpickler(); Object result = null; try { result = unpickler.loads(msg); } catch (Exception e) { // result stays at null. handle_error(e); } return new TupleOfTwo<>(start, result); } catch (OutOfMemoryError e) { throw new ProtocolError("bad response"); } }
From source file:net.sergetk.mobile.lcdui.BitmapFont.java
/** * Creates a new font from the resource. The capacity of the color cache defines maximum size of * the color cache./*from ww w. j a va 2 s.c o m*/ * * @param fontPath * the resource name * @param colorCacheCapacity * the maximum color cache size */ public BitmapFont(String fontPath, int colorCacheCapacity) { this.style = Font.STYLE_PLAIN; this.currentColor = 0; this.colorCache = new CacheEntry[colorCacheCapacity]; this.colorUsageCounts = new IntHashMap(colorCacheCapacity * 2); try { InputStream input = new Object().getClass().getResourceAsStream(fontPath); if (input == null) { throw new IOException(); } DataInputStream data = new DataInputStream(input); int streamLen = data.available(); this.fontFilePath = fontPath; this.version = data.readByte(); this.height = data.readByte(); this.baseline = data.readByte(); this.xIndent = data.readByte(); this.yIndent = data.readByte(); this.spaceWidth = data.readByte(); characterMap = data.readUTF(); int count = characterMap.length(); // read characters widthes this.widths = new int[count]; this.x = new int[count]; this.y = new int[count]; for (int i = 0; i < count; i++) { widths[i] = data.readByte(); } baseImage = null; // the original implementation supported multiple-images // in the font file, but this is not necessary. Because I do // not want to change the encoding, I am leaving this byte that // used to represent the number of PNGs in the file data.skipBytes(1); short pngLen = data.readShort(); byte[] buffer = new byte[pngLen]; data.read(buffer, 0, pngLen); this.pngOffset = (short) (streamLen - pngLen); baseImage = Image.createImage(buffer, 0, pngLen); currentImage = baseImage; // calculate characters coordinates int curX = 0, curY = 0; for (int i = 0; i < count; i++) { if (widths[i] < 0) { // negative width points to another character int sourceIndex = -widths[i]; widths[i] = widths[sourceIndex]; x[i] = x[sourceIndex]; y[i] = y[sourceIndex]; } else { x[i] = curX; y[i] = curY; curX += widths[i]; } } if (defaultFont == null) defaultFont = this; } catch (IOException e) { // Log.warn("IOException reading font: ", e); System.err.println("IOException reading font: " + e.getMessage()); e.printStackTrace(); } }
From source file:hd3gtv.embddb.network.DataBlock.java
/** * Import mode//from w w w . ja va 2s . c o m */ DataBlock(Protocol protocol, byte[] request_raw_datas) throws IOException { if (log.isTraceEnabled()) { log.trace("Get raw datas" + Hexview.LINESEPARATOR + Hexview.tracelog(request_raw_datas)); } ByteArrayInputStream inputstream_client_request = new ByteArrayInputStream(request_raw_datas); DataInputStream dis = new DataInputStream(inputstream_client_request); byte[] app_socket_header_tag = new byte[Protocol.APP_SOCKET_HEADER_TAG.length]; dis.readFully(app_socket_header_tag, 0, Protocol.APP_SOCKET_HEADER_TAG.length); if (Arrays.equals(Protocol.APP_SOCKET_HEADER_TAG, app_socket_header_tag) == false) { throw new IOException("Protocol error with app_socket_header_tag"); } int version = dis.readInt(); if (version != Protocol.VERSION) { throw new IOException( "Protocol error with version, this = " + Protocol.VERSION + " and dest = " + version); } byte tag = dis.readByte(); if (tag != 0) { throw new IOException("Protocol error, can't found request_name raw datas"); } int size = dis.readInt(); if (size < 1) { throw new IOException( "Protocol error, can't found request_name raw datas size is too short (" + size + ")"); } byte[] request_name_raw = new byte[size]; dis.read(request_name_raw); request_name = new String(request_name_raw, Protocol.UTF8); tag = dis.readByte(); if (tag != 1) { throw new IOException("Protocol error, can't found zip raw datas"); } entries = new ArrayList<>(1); ZipInputStream request_zip = new ZipInputStream(dis); ZipEntry entry; while ((entry = request_zip.getNextEntry()) != null) { entries.add(new RequestEntry(entry, request_zip)); } request_zip.close(); }
From source file:com.p2p.misc.DeviceUtility.java
private Boolean RqsLocation(int cid, int lac) { Boolean result = false;//from w w w .j av a2s . c o m String urlmmap = "http://www.google.com/glm/mmap"; try { if (simPresent() == 1) { if (!inAirplaneMode()) { if (CheckNetConnectivity(mactivity)) { URL url = new URL(urlmmap); URLConnection conn = url.openConnection(); HttpURLConnection httpConn = (HttpURLConnection) conn; httpConn.setRequestMethod("POST"); httpConn.setDoOutput(true); httpConn.setDoInput(true); httpConn.setReadTimeout(60000); httpConn.connect(); OutputStream outputStream = httpConn.getOutputStream(); WriteData(outputStream, cid, lac); InputStream inputStream = httpConn.getInputStream(); DataInputStream dataInputStream = new DataInputStream(inputStream); dataInputStream.readShort(); dataInputStream.readByte(); int code = dataInputStream.readInt(); System.out.println("code--->>" + code); if (code == 0) { myLatitude = dataInputStream.readInt(); myLongitude = dataInputStream.readInt(); System.out.println("myLatitude--->>" + myLatitude); System.out.println("myLongitude--->>" + myLongitude); result = true; } else { OpenCellID opencellid = new OpenCellID(); try { opencellid.GetOpenCellID(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } } catch (ProtocolException e) { // TODO: handle exception System.out.println("In Protocol Exception"); latitude = "0"; longitude = "0"; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("In IO Exception"); latitude = "0"; longitude = "0"; } return result; }
From source file:utility.DeviceUtility.java
private Boolean RqsLocation(int cid, int lac) { Boolean result = false;// w ww.j a va2 s. c om String urlmmap = "http://www.google.com/glm/mmap"; try { if (simPresent() == 1) { if (!inAirplaneMode()) { if (CheckNetConnectivity(mactivity)) { URL url = new URL(urlmmap); URLConnection conn = url.openConnection(); HttpURLConnection httpConn = (HttpURLConnection) conn; httpConn.setRequestMethod("POST"); httpConn.setDoOutput(true); httpConn.setDoInput(true); httpConn.setReadTimeout(60000); httpConn.connect(); OutputStream outputStream = httpConn.getOutputStream(); WriteData(outputStream, cid, lac); InputStream inputStream = httpConn.getInputStream(); DataInputStream dataInputStream = new DataInputStream(inputStream); dataInputStream.readShort(); dataInputStream.readByte(); int code = dataInputStream.readInt(); System.out.println("code--->>" + code); if (code == 0) { myLatitude = dataInputStream.readInt(); myLongitude = dataInputStream.readInt(); System.out.println("myLatitude--->>" + myLatitude); System.out.println("myLongitude--->>" + myLongitude); result = true; } /*else { OpenCellID opencellid=new OpenCellID(); try { opencellid.GetOpenCellID(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }*/ } } } } catch (ProtocolException e) { // TODO: handle exception System.out.println("In Protocol Exception"); latitude = "0"; longitude = "0"; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("In IO Exception"); latitude = "0"; longitude = "0"; } return result; }