// Loads traffic from file public final void load() { sessionInTraffic = 0; sessionOutTraffic = 0; savedCost = 0; lastTimeUsed = new Date(1); costPerDaySum = 0; savedSince = new Date(); Storage traffic = new Storage("traffic"); try { traffic.open(false); byte[] buf = traffic.getRecord(2); ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream dis = new DataInputStream(bais); allInTraffic = dis.readInt(); allOutTraffic = dis.readInt(); savedSince.setTime(dis.readLong()); lastTimeUsed.setTime(dis.readLong()); savedCost = dis.readInt(); } catch (Exception e) { savedSince.setTime(new Date().getTime()); allInTraffic = 0; sessionOutTraffic = 0; savedCost = 0; } traffic.close(); }
/** * Sort-based shuffle data uses an index called "shuffle_ShuffleId_MapId_0.index" into a data file * called "shuffle_ShuffleId_MapId_0.data". This logic is from IndexShuffleBlockResolver, and the * block id format is from ShuffleDataBlockId and ShuffleIndexBlockId. */ private ManagedBuffer getSortBasedShuffleBlockData( ExecutorShuffleInfo executor, int shuffleId, int mapId, int reduceId) { File indexFile = getFile( executor.localDirs, executor.subDirsPerLocalDir, "shuffle_" + shuffleId + "_" + mapId + "_0.index"); DataInputStream in = null; try { in = new DataInputStream(new FileInputStream(indexFile)); in.skipBytes(reduceId * 8); long offset = in.readLong(); long nextOffset = in.readLong(); return new FileSegmentManagedBuffer( conf, getFile( executor.localDirs, executor.subDirsPerLocalDir, "shuffle_" + shuffleId + "_" + mapId + "_0.data"), offset, nextOffset - offset); } catch (IOException e) { throw new RuntimeException("Failed to open file: " + indexFile, e); } finally { if (in != null) { JavaUtils.closeQuietly(in); } } }
private void processRequest(DataInputStream in) throws IOException { long serverId = in.readLong(); ServerObject server = servingById.get(serverId); long requestId = in.readLong(); Request r = new Request(this, server.getObject()); r.deserialize(in); r.invoke(); if (requestId != -1) { sendResponse(requestId, r.getResult(), r.getResultDeclaredType()); } }
@Nullable private DataInputStream createFSDataStream(File dataStorageRoot) { if (myInitialFSDelta == null) { // this will force FS rescan return null; } try { final File file = new File(dataStorageRoot, FS_STATE_FILE); final InputStream fs = new FileInputStream(file); byte[] bytes; try { bytes = FileUtil.loadBytes(fs, (int) file.length()); } finally { fs.close(); } final DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes)); final int version = in.readInt(); if (version != FSState.VERSION) { return null; } final long savedOrdinal = in.readLong(); if (savedOrdinal + 1L != myInitialFSDelta.getOrdinal()) { return null; } return in; } catch (FileNotFoundException ignored) { } catch (Throwable e) { LOG.error(e); } return null; }
/** * Read a constant from the constant pool. * * @param in The stream from which to read. * @return The constant. * @exception IOException If an error occurs while reading. */ Constant readConstant(DataInputStream in) throws IOException { int tag = in.readUnsignedByte(); Object value; switch (tag) { case Constant.CLASS: case Constant.STRING: case Constant.METHOD_TYPE: // @since 1.8 value = Integer.valueOf(in.readUnsignedShort()); break; case Constant.FIELD_REF: case Constant.METHOD_REF: case Constant.INTERFACE_METHOD_REF: case Constant.NAME_AND_TYPE: case Constant.INVOKE_DYNAMIC: // @since 1.8 value = new int[2]; ((int[]) value)[0] = in.readUnsignedShort(); ((int[]) value)[1] = in.readUnsignedShort(); break; case Constant.INTEGER: value = Integer.valueOf(in.readInt()); break; case Constant.FLOAT: value = Float.valueOf(in.readFloat()); break; case Constant.LONG: // Longs take up 2 constant pool entries. value = Long.valueOf(in.readLong()); break; case Constant.DOUBLE: // Doubles take up 2 constant pool entries. value = Double.valueOf(in.readDouble()); break; case Constant.UTF8: value = in.readUTF(); break; case Constant.METHOD_HANDLE: // @since 1.8 value = new int[2]; ((int[]) value)[0] = in.readUnsignedByte(); ((int[]) value)[1] = in.readUnsignedShort(); break; default: throw new ClassFormatError("Invalid constant tag: " + tag); } return new Constant(tag, value); }
private void processResponse(DataInputStream in) throws IOException { long requestId = in.readLong(); Request r = requests.remove(requestId); if (r == null) { throw new IllegalStateException( "Request " + requestId + " is unknown (last request generated was " + nextRequest.get()); } Object o = null; if (in.readBoolean()) { o = serializerFor(classForName(in.readUTF()), r.getResultDeclaredType()).deserialize(in); } r.set(o); }
@Override public void readData(DataInputStream in) throws IOException { super.readData(in); int length = in.readInt(); EntityProperty[] properties = new EntityProperty[length]; for (int i = 0; i < length; i++) { String name = readString(in); double value = in.readDouble(); EntityProperty property = new EntityProperty(name, value); short modifiers = in.readShort(); for (int j = 0; j < modifiers; j++) { long msb = in.readLong(); long lsb = in.readLong(); UUID uuid = new UUID(msb, lsb); double amount = in.readDouble(); int operation = in.read(); property.addModifier(uuid, amount, operation); } properties[i] = property; } this.properties = properties; }
@SuppressWarnings("rawtypes") private static Object readObjectFromStream(DataInputStream data, Class curClass) throws IOException { if (curClass.equals(Boolean.class)) { return data.readBoolean(); } else if (curClass.equals(Byte.class)) { return data.readByte(); } else if (curClass.equals(Integer.class)) { return data.readInt(); } else if (curClass.equals(String.class)) { return data.readUTF(); } else if (curClass.equals(Double.class)) { return data.readDouble(); } else if (curClass.equals(Float.class)) { return data.readFloat(); } else if (curClass.equals(Long.class)) { return data.readLong(); } else if (curClass.equals(Short.class)) { return data.readShort(); } return null; }
/** Run all previously added search queries. */ public SphinxResult[] RunQueries() throws SphinxException { if (_reqs == null || _reqs.size() < 1) { _error = "no queries defined, issue AddQuery() first"; return null; } /* build the mega-request */ int nreqs = _reqs.size(); ByteArrayOutputStream reqBuf = new ByteArrayOutputStream(); try { DataOutputStream req = new DataOutputStream(reqBuf); /* its a client */ req.writeInt(0); req.writeInt(nreqs); for (int i = 0; i < nreqs; i++) req.write((byte[]) _reqs.get(i)); req.flush(); } catch (Exception e) { _error = "internal error: failed to build request: " + e; return null; } DataInputStream in = _DoRequest(SEARCHD_COMMAND_SEARCH, VER_COMMAND_SEARCH, reqBuf); if (in == null) return null; SphinxResult[] results = new SphinxResult[nreqs]; _reqs = new ArrayList(); try { for (int ires = 0; ires < nreqs; ires++) { SphinxResult res = new SphinxResult(); results[ires] = res; int status = in.readInt(); res.setStatus(status); if (status != SEARCHD_OK) { String message = readNetUTF8(in); if (status == SEARCHD_WARNING) { res.warning = message; } else { res.error = message; continue; } } /* read fields */ int nfields = in.readInt(); res.fields = new String[nfields]; int pos = 0; for (int i = 0; i < nfields; i++) res.fields[i] = readNetUTF8(in); /* read arrts */ int nattrs = in.readInt(); res.attrTypes = new int[nattrs]; res.attrNames = new String[nattrs]; for (int i = 0; i < nattrs; i++) { String AttrName = readNetUTF8(in); int AttrType = in.readInt(); res.attrNames[i] = AttrName; res.attrTypes[i] = AttrType; } /* read match count */ int count = in.readInt(); int id64 = in.readInt(); res.matches = new SphinxMatch[count]; for (int matchesNo = 0; matchesNo < count; matchesNo++) { SphinxMatch docInfo; docInfo = new SphinxMatch((id64 == 0) ? readDword(in) : in.readLong(), in.readInt()); /* read matches */ for (int attrNumber = 0; attrNumber < res.attrTypes.length; attrNumber++) { String attrName = res.attrNames[attrNumber]; int type = res.attrTypes[attrNumber]; /* handle bigints */ if (type == SPH_ATTR_BIGINT) { docInfo.attrValues.add(attrNumber, new Long(in.readLong())); continue; } /* handle floats */ if (type == SPH_ATTR_FLOAT) { docInfo.attrValues.add(attrNumber, new Float(in.readFloat())); continue; } /* handle strings */ if (type == SPH_ATTR_STRING) { String s = readNetUTF8(in); docInfo.attrValues.add(attrNumber, s); continue; } /* handle everything else as unsigned ints */ long val = readDword(in); if (type == SPH_ATTR_MULTI) { long[] vals = new long[(int) val]; for (int k = 0; k < val; k++) vals[k] = readDword(in); docInfo.attrValues.add(attrNumber, vals); } else if (type == SPH_ATTR_MULTI64) { val = val / 2; long[] vals = new long[(int) val]; for (int k = 0; k < val; k++) vals[k] = in.readLong(); docInfo.attrValues.add(attrNumber, vals); } else { docInfo.attrValues.add(attrNumber, new Long(val)); } } res.matches[matchesNo] = docInfo; } res.total = in.readInt(); res.totalFound = in.readInt(); res.time = in.readInt() / 1000.0f; res.words = new SphinxWordInfo[in.readInt()]; for (int i = 0; i < res.words.length; i++) res.words[i] = new SphinxWordInfo(readNetUTF8(in), readDword(in), readDword(in)); } return results; } catch (IOException e) { _error = "incomplete reply"; return null; } }
public void play(String name, String version, String vendor, String desc) throws Exception { Info port = null; for (Info info : MidiSystem.getMidiDeviceInfo()) { if (name == null && version == null && vendor == null && desc == null) { System.err.println( "At least one of name, version, vendor, or description must be specified."); break; } if (name != null && !info.getName().equals(name)) { continue; } if (vendor != null && !info.getVendor().equals(vendor)) { continue; } if (version != null && !info.getVersion().equals(version)) { continue; } if (desc != null && !info.getDescription().equals(desc)) { continue; } try (MidiDevice dev = MidiSystem.getMidiDevice(info)) { Receiver r = null; try { r = dev.getReceiver(); } catch (Exception e) { r = null; } if (r == null) { continue; } } if (port != null) { System.err.println( "Multiple MIDI ports match the given parameters. Please be more specific."); port = null; break; } port = info; continue; } if (port == null) { System.err.println("Unable to locate MIDI port"); System.err.println("Available ports:"); for (Info info : MidiSystem.getMidiDeviceInfo()) { try (MidiDevice dev = MidiSystem.getMidiDevice(info)) { Receiver r = null; try { r = dev.getReceiver(); } catch (Exception e) { r = null; } if (r == null) { continue; } } System.out.println( "Device [name=" + info.getName() + ", version=" + info.getVersion() + ", vendor=" + info.getVendor() + ", desc=" + info.getDescription() + "]"); } return; } System.out.println( "Using device [name=" + port.getName() + ", version=" + port.getVersion() + ", vendor=" + port.getVendor() + ", desc=" + port.getDescription() + "]"); try (MidiDevice dev = MidiSystem.getMidiDevice(port)) { int channel = CHANNEL_RAW_STEREO; dev.open(); try (Receiver rx = dev.getReceiver()) { rxCleanup = rx; try (DataInputStream dis = new DataInputStream(new FileInputStream(ymz))) { resetController(rx, channel, registers); long startTime = System.nanoTime(); // read a buffer long prevClock = 0; while (true) { long clock = dis.readLong(); byte register = dis.readByte(); byte value = dis.readByte(); if (prevClock != clock) { sendAll(rx, channel, registers); prevClock = clock; } sleepUntil(startTime, clock); System.out.println( "Clock: " + clock + " register: " + register + ", value: " + (value & 0xff)); if (register < 0 || register > 13) { System.err.println("Invalid register " + register + " found, ignoring."); continue; } registers[register] = value; } } catch (EOFException e) { System.err.println("EOF reached"); } finally { resetController(rx, channel, registers); rxCleanup = null; } } } }