void isReadable(SelectionKey k) { EventableChannel ec = (EventableChannel) k.attachment(); long b = ec.getBinding(); if (ec.isWatchOnly()) { if (ec.isNotifyReadable()) eventCallback(b, EM_CONNECTION_NOTIFY_READABLE, null); } else { myReadBuffer.clear(); try { ec.readInboundData(myReadBuffer); myReadBuffer.flip(); if (myReadBuffer.limit() > 0) { if (ProxyConnections != null) { EventableChannel target = ProxyConnections.get(b); if (target != null) { ByteBuffer myWriteBuffer = ByteBuffer.allocate(myReadBuffer.limit()); myWriteBuffer.put(myReadBuffer); myWriteBuffer.flip(); target.scheduleOutboundData(myWriteBuffer); } else { eventCallback(b, EM_CONNECTION_READ, myReadBuffer); } } else { eventCallback(b, EM_CONNECTION_READ, myReadBuffer); } } } catch (IOException e) { UnboundConnections.add(b); } } }
@Override public int read(final ByteBuffer bb, final int pos) { this.m_isReset = false; bb.position(pos); while (bb.remaining() > 0) { boolean needToProcessPacket = true; if (this.m_decodeReady) { final Info info = this.m_info[this.m_currentLink]; final int totalSamples = this.m_dspState.synthesis_pcmout(this.m_pcmf_buffer, this._index); final float[][] pcm = this.m_pcmf_buffer[0]; if (totalSamples > 0) { final int channelsCount = info.channelsCount; final int bytesPerSample = channelsCount * 2; int samples = Math.min(totalSamples, bb.remaining() / bytesPerSample); samples = Math.min(samples, 8192 / bytesPerSample); needToProcessPacket = (samples == totalSamples); for (int i = 0; i < channelsCount; ++i) { int ptr = i * 2; final int mono = this._index[i]; for (int j = 0; j < samples; ++j) { int val = (int) (pcm[i][mono + j] * 32767.0f); if (val > 32767) { val = 32767; } if (val < -32768) { val = -32768; } if (val < 0) { val |= 0x8000; } if (JOrbisStream.m_bigEndian) { this.m_conversionBuffer[ptr] = (byte) (val >>> 8 & 0xFF); this.m_conversionBuffer[ptr + 1] = (byte) (val & 0xFF); } else { this.m_conversionBuffer[ptr] = (byte) (val & 0xFF); this.m_conversionBuffer[ptr + 1] = (byte) (val >>> 8 & 0xFF); } ptr += 2 * channelsCount; } } final int writtenBytesLength = samples * bytesPerSample; bb.put(this.m_conversionBuffer, 0, writtenBytesLength); this.m_dspState.synthesis_read(samples); this.m_pcmOffset += samples; } } if (needToProcessPacket) { switch (this.processPacket(true)) { case 0: { return -(bb.position() - pos); } case -1: { return -(bb.position() - pos); } default: { continue; } } } } return bb.position() - pos; }