/** * Lis et retourne une piece depuis le fichier temporaire sur le disque (la piece doit exister) * * @param num : numéros de la piece */ private synchronized byte[] readPieceTmpFile(int num) { if (num < 0 || num >= this.nbPieces()) { throw new IllegalArgumentException(); } try { FileInputStream reader_tmp = new FileInputStream(this); FileChannel reader = reader_tmp.getChannel(); int index_piece = Tools.readInt(reader, 4 + _key.length() + 4 + 4 + 4 * num); if (index_piece < 0) { throw new IllegalArgumentException(); } int size = _piecesize; if (num == this.nbPieces() - 1) { size = _size - _piecesize * (this.nbPieces() - 1); } byte[] piece = Tools.readBytes(reader, this.headerSize() + _piecesize * index_piece, size); reader_tmp.close(); return piece; } catch (Exception e) { System.out.println("Unable to read tmp file piece"); e.printStackTrace(); } return new byte[0]; }
/** * Lis et retourne une pièce depuis le fichier complet sur le disque * * @param num : numéros de la piece */ private byte[] readPieceCompleteFile(int num) { if (num < 0 || num >= this.nbPieces()) { throw new IllegalArgumentException(); } try { FileInputStream reader_tmp = new FileInputStream(this); FileChannel reader = reader_tmp.getChannel(); int size = _piecesize; if (num == this.nbPieces() - 1) { size = _size - _piecesize * (this.nbPieces() - 1); } byte[] piece = Tools.readBytes(reader, _piecesize * num, size); reader_tmp.close(); return piece; } catch (Exception e) { System.out.println("Unable to read complete file piece"); e.printStackTrace(); } return new byte[0]; }