// @include public static String findStudentWithTopThreeAverageScores(InputStream ifs) { Map<String, TreeSet<UniqueInteger>> studentScores = new HashMap<>(); try { long sequence = 0; ObjectInputStream ois = new ObjectInputStream(ifs); while (true) { String name = ois.readUTF(); int score = ois.readInt(); TreeSet<UniqueInteger> scores = studentScores.get(name); if (scores == null) { scores = new TreeSet<>(); } scores.add(new UniqueInteger(score, sequence++)); studentScores.put(name, scores); } } catch (IOException e) { } String topStudent = "no such student"; int currentTopThreeScoresSum = 0; for (Map.Entry<String, TreeSet<UniqueInteger>> scores : studentScores.entrySet()) { if (scores.getValue().size() >= 3) { int currentScoresSum = getTopThreeScoresSum(scores.getValue()); if (currentScoresSum > currentTopThreeScoresSum) { currentTopThreeScoresSum = currentScoresSum; topStudent = scores.getKey(); } } } return topStudent; }
@Override public void run() { ObjectInputStream ois = null; ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(mSocket.getOutputStream()); ois = new ObjectInputStream(mSocket.getInputStream()); // Enviar o nosso g^Y oos.writeUTF(mPrivateKey.toString()); oos.flush(); // Receber o g^X da Alice BigInteger gX = new BigInteger(ois.readUTF()); // Calcular a chave mKey = mGpowModP.modPow(gX, mPrime); CipherUtils cipherUtils = new CipherUtils(ois, oos, mKey); String test; BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); while ((test = stdIn.readLine()) != null) { cipherUtils.encrypt(test); } } catch (Exception e) { e.printStackTrace(); } finally { if (ois != null) { try { ois.close(); } catch (IOException e) { e.printStackTrace(); } } if (oos != null) { try { oos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
// --------------------------------------------------------------------------- private void readCache() { Debug.print("Cache file %s", m_cacheFile); File cacheFile = new File(m_cacheFile); m_depCache = null; m_modificationCache = null; if (cacheFile.exists()) { try { Debug.print("Reading cache"); FileInputStream cacheIs = new FileInputStream(cacheFile); ObjectInputStream ois = new ObjectInputStream(cacheIs); m_depCache = (DependencyCache) ois.readObject(); Map<String, Long> modCache = new HashMap<String, Long>(); int modCacheSize = ois.readInt(); for (int I = 0; I < modCacheSize; I++) { String file = ois.readUTF(); Debug.print(" Read %s", file); long ts = ois.readLong(); modCache.put(file, ts); } m_modificationCache = modCache; ois.close(); } catch (Exception e) { Debug.print(e.getMessage()); StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); Debug.print(sw.toString()); } } if ((m_depCache == null) || (m_modificationCache == null)) { m_depCache = new DependencyCache(); m_modificationCache = new HashMap<String, Long>(); } }
/** * Creates source for sources with two orders: firstly in RIC, secondly in time. * * @param filePath complete filename and path containing prices. * @param symbols set of symbols present into the file. * @param lineProcessor strategy to part each line. * @throws IOException if one symbol (at least) cannot be found. */ public MultipleSymbolScanner( final String filePath, final String[] symbols, final LineParser lineParser, final SpreadTradesMgr[] spreadTradesMgr) throws IOException { this.lineParser = lineParser; this.spreadTradesMgr = spreadTradesMgr; this.filePath = filePath; this.symbols = symbols; this.mainReader = new RandomAccessFile(filePath, "r"); this.partReader = new RandomAccessFile[symbols.length]; this.nextMilliseconds = new long[symbols.length]; this.nextLine = new String[symbols.length]; boolean optimisticJumps = true; final ErrorControl errorControl = this.lineParser.getErrorControl(); this.lineParser.setErrorControl(LineParser.nullErrorControl); final String cacheFilePath = filePath + ".cachejump"; if (new File(cacheFilePath).exists()) { final ObjectInputStream cacheJumpsFileIn = new ObjectInputStream(new FileInputStream(cacheFilePath)); log.info("Using stored cached jumps in file " + cacheFilePath); try { try { for (; ; ) { final String s = cacheJumpsFileIn.readUTF(); final long pos = cacheJumpsFileIn.readLong(); for (int i = 0; i < symbols.length; i++) { if (symbols[i].equals(s)) { partReader[i] = new RandomAccessFile(filePath, "r"); partReader[i].seek(pos); log.info("Using cached information: position " + pos + " for " + s); break; } } } } catch (IOException ioe) { cacheJumpsFileIn.close(); } for (int i = 0; i < partReader.length; i++) { if (partReader[i] == null) { throw new IOException("Symbol " + symbols[i] + " not placed"); } } } catch (IOException ioe) { log.log(Level.SEVERE, ioe.toString(), ioe); log.severe("Error reading file " + cacheFilePath + ". Removing"); new File(cacheFilePath).delete(); } } else { final ObjectOutputStream cacheJumpsFileOut = new ObjectOutputStream(new FileOutputStream(cacheFilePath)); log.fine("Caching jumps for future uses in file " + cacheFilePath); log.info("Scannig file to place reading pointers"); for (int i = 0; i < symbols.length; i++) { long lastPosition = this.mainReader.getFilePointer(); long foundPosition; log.fine("Starting to search symbol " + symbols[i] + " from position " + lastPosition); for (; ; ) { foundPosition = this.mainReader.getFilePointer(); final String line = readNextLineCycling(lastPosition); if (line == null) { // All file's been read without finding desired symbol. throw new IOException( "Cannot find symbol '" + symbols[i] + "' in file '" + filePath + "'"); } if (this.lineParser.isValid(line) && symbols[i].equals(this.lineParser.getSymbol(line, 0))) { break; } } log.info("Located place for reader on " + symbols[i] + ":" + foundPosition); cacheJumpsFileOut.writeUTF(symbols[i]); cacheJumpsFileOut.writeLong(foundPosition); partReader[i] = new RandomAccessFile(filePath, "r"); partReader[i].seek(foundPosition); final int optimisticJump = (int) (2 * (foundPosition - lastPosition) / 3); this.mainReader.skipBytes(optimisticJump); } cacheJumpsFileOut.flush(); cacheJumpsFileOut.close(); } log.info("File scanned. Placing reading pointers"); // Boot up for (int i = 0; i < symbols.length; i++) { for (; ; ) { try { String line = partReader[i].readLine(); while (line != null && !this.lineParser.isValid(line)) { line = partReader[i].readLine(); } if (!symbols[i].equals(this.lineParser.parse(line).getSymbol(0))) { this.nextLine[i] = null; } else { this.nextLine[i] = line; this.nextMilliseconds[i] = this.lineParser.parse(line).getUTCTimestampCopy().getTimeInMillis(); break; } } catch (Exception e) { log.log( Level.SEVERE, "Exception reading first line " + "of file '" + this.filePath + "'", e); log.severe("Maybe cache file is not correct?"); } } } log.info("Readers placed correctly. Source started"); this.lineParser.setErrorControl(errorControl); }
private void readObject(ObjectInputStream in) throws IOException { _jdkSerializeValue = in.readUTF(); }
public void run() { Server.preRunOperations(this); while (true) { try { String type = in.readUTF(); if (type.equals("newObject")) { Object object = in.readObject(); if (object instanceof String) { // Adding player.. this.player = new Player((String) object, Entity.BLUE); boolean addPlayer = true; for (int i = 0; i < Server.players.size(); i++) { if (this.player.name.equals(Server.players.get(i).name)) { // Set to online! if (Server.players.get(i).online) { System.out.println( "Player " + player.name + " is already connected! " + (userID + 1)); this.player = null; addPlayer = false; } else { this.player = Server.players.get(i); System.out.println("Returning User: "******", " + (userID + 1)); this.player.online = true; addPlayer = false; } break; } } if (addPlayer) { System.out.println("New User: "******", " + (userID + 1)); Server.players.add(this.player); } } } else if (type.equals( "MoveUnit")) { // this needs to require the player to be the owner of the unit short unit = in.readShort(); double cX = in.readDouble(); double cY = in.readDouble(); double x = in.readDouble(); double y = in.readDouble(); boolean stacked = in.readBoolean(); // Pretty simple: (If the unit doesn't "exist", game will take care of that.) [Keep in // mind, this currently could easily let the player control enemy units] new PathFind( unit, cX, cY, x, y, stacked); // All commands take the liberty of adding themselves to the game stack. } else if (type.equals( "SetTarget")) { // this needs to require the player to be the owner of the unit short unit = in.readShort(); short enemy = in.readShort(); boolean stacked = in.readBoolean(); // Pretty simple: new SetTarget(unit, enemy, stacked); } else if (type.equals("AddOrder")) { short unit = in.readShort(); byte order = in.readByte(); double atX = in.readDouble(); double atY = in.readDouble(); boolean stacked = in.readBoolean(); new AddOrder(unit, order, atX, atY, stacked); } else if (type.equals("Uber")) { short unit = in.readShort(); boolean stacked = in.readBoolean(); new Uber(unit, stacked); } else if (type.equals("Exiting")) { disconnect(); return; } } catch (IOException e) { disconnect(); return; } catch (Exception e) { System.out.println("Alternate error."); e.printStackTrace(); } } }