protected final List<AbstractAudioChunk> parseChunks( DataInputStream dis, Map<Integer, AudioChunkParser> chunkParserMap) throws UnsupportedAudioFileException, IOException { ArrayList<AbstractAudioChunk> parsedChunks = new ArrayList<AbstractAudioChunk>(); int currKey; int chunkLength = 0; int chunkRead = 0; do { if (dis.available() == 0) break; advanceChunk(dis, chunkLength, chunkRead); if (dis.available() == 0) break; try { currKey = dis.readInt(); if (dis.available() == 0) break; chunkLength = readChunkLength(dis); } catch (IOException e) { break; } Integer currKeyObj = new Integer(currKey); if (chunkParserMap.containsKey(currKeyObj)) { parsedChunks.add((chunkParserMap.get(currKeyObj)).parseChunk(dis, chunkLength)); chunkRead = chunkLength; } else chunkRead = 0; } while (true); return parsedChunks; }
/** * Load an edit log, and apply the changes to the in-memory structure * * <p>This is where we apply edits that we've been writing to disk all along. */ int loadFSEdits(File edits) throws IOException { int numEdits = 0; if (edits.exists()) { DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(edits))); try { while (in.available() > 0) { byte opcode = in.readByte(); numEdits++; switch (opcode) { case OP_ADD: { UTF8 name = new UTF8(); name.readFields(in); ArrayWritable aw = new ArrayWritable(Block.class); aw.readFields(in); Writable writables[] = (Writable[]) aw.get(); Block blocks[] = new Block[writables.length]; System.arraycopy(writables, 0, blocks, 0, blocks.length); unprotectedAddFile(name, blocks); break; } case OP_RENAME: { UTF8 src = new UTF8(); UTF8 dst = new UTF8(); src.readFields(in); dst.readFields(in); unprotectedRenameTo(src, dst); break; } case OP_DELETE: { UTF8 src = new UTF8(); src.readFields(in); unprotectedDelete(src); break; } case OP_MKDIR: { UTF8 src = new UTF8(); src.readFields(in); unprotectedMkdir(src.toString()); break; } default: { throw new IOException("Never seen opcode " + opcode); } } } } finally { in.close(); } } return numEdits; }
public boolean buildSatsFromKeps(Vector fileList) { Enumeration filelist = fileList.elements(); while (filelist.hasMoreElements()) { String file = (String) filelist.nextElement(); FileInputStream fis = null; DataInputStream dis = null; String lineRead = null; try { if (dir != null) { fis = new FileInputStream(this.dir + file); } else { fis = new FileInputStream(file); } dis = new DataInputStream(fis); System.out.println("Found file '" + dir + file + "': Loading keps"); while (dis.available() > 0) { lineRead = dis.readLine().trim(); if (lineRead.length() > 0) { if (lineRead.length() > 20) lineRead = lineRead.substring(0, 19); Satellite sat = new Satellite(lineRead); String firstLine = dis.readLine().trim(); if (firstLine.length() < 69) { sat = null; } else { String secondLine = dis.readLine().trim(); if (secondLine.length() < 69) { sat = null; } else { sat.setKeps(firstLine, secondLine); satVector.addElement(sat); } } } } } catch (IOException _ex) { System.out.println("Sat data file '" + dir + file + "' not found"); _ex.printStackTrace(); return false; } } mapper.myData.allSats = satVector; return true; }
/** * Send a plugin JAR file to the client * * @param data the plugin name */ private void sendPlugin(ObjectConnection oc, String data) { DataInputStream dis = null; try { dis = new DataInputStream(new FileInputStream(APPLICATIONS_DIRECTORY + data + ".jar")); byte[] buf = new byte[dis.available()]; dis.readFully(buf); oc.write(buf); } catch (Exception e) { Logging.getLogger().warning("Unable to send the file: " + data + ".jar"); } finally { if (dis != null) { try { dis.close(); } catch (IOException ioe) { } } } }
@Override public void run() { try { // Create data input and output streams DataInputStream inputFromClient = new DataInputStream(socket.getInputStream()); DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream()); // Set up a byte buffer to capture the file from the client byte[] buffer = new byte[BUFFER_SIZE]; OutputStream outputStream = null; String fileName = ""; boolean createFile = true; int bytesReceived = 0; long totalBytesReceived = 0; long fileSize = 0; // Continuously serve the client while (true) { bytesReceived = inputFromClient.read(buffer); if (bytesReceived > 0) { // Get the file transmission header from the initial client packet String transmitHeader = new String(buffer, 0, bytesReceived); // transmitHeader = transmitHeader.substring(0, bytesReceived).trim().; String[] header = transmitHeader.split(HEADER_DEL); fileSize = Long.parseLong(header[0]); fileName = header[1]; // Send receipt acknowledgment back to the client. Just send back the number of bytes // received. outputToClient.writeInt(bytesReceived); // Reinitialize buffer values buffer = new byte[BUFFER_SIZE]; bytesReceived = 0; } // Wait for client to send bytes while ((bytesReceived = inputFromClient.read(buffer)) != -1) { if (inputFromClient.available() > 0) { if (createFile) { // Get a unique name for the file to be received fileName = textFolder.getText() + fileName; // getUniqueFileName(); outputStream = createFile(fileName); createFile = false; textArea.append("Receiving file from client.\n"); } // Write bytes to file outputStream.write(buffer, 0, bytesReceived); } else { // We get here if no more data is available, but some bytes were already // received // If bytes were received and the file wasn't already created, // it means that the file was smaller than our buffer size. // Create the file... if (bytesReceived > 0 && createFile) { // Get a unique name for the file to be received fileName = textFolder.getText() + fileName; // getUniqueFileName(); outputStream = createFile(fileName); createFile = false; textArea.append("Receiving file from client.\n"); } if (outputStream != null) { if (bytesReceived > 0) { // Write remaining bytes to file, if any outputStream.write(buffer, 0, bytesReceived); } outputStream.flush(); outputStream.close(); textArea.append("Received file successfully. Saved as " + fileName + "\n"); // Return success to client. outputToClient.writeInt(0); } // Reset creation flag createFile = true; break; } // Reinitialize buffer values buffer = new byte[BUFFER_SIZE]; bytesReceived = 0; } } } catch (IOException e) { System.err.println(e); } }