jint ByteChannel::read(jobject destination) { const ByteBuffer::ClassImpl& bufimpl = ByteBuffer::impl(m_env); const jint remaining = get_remaining(m_env, destination, bufimpl.m_mid_get_remaining); if (!remaining) { // No space in the buffer; don't try to read anything. return 0; } const jint position = get_position(m_env, destination, bufimpl.m_mid_get_position); jint bytes_read = 0; void* data = m_env.GetDirectBufferAddress(destination); if (data) { data = static_cast<char*>(data) + position; bytes_read = m_reader(m_env, data, remaining); } else { // It was not a direct buffer ... see if it has an array. jbyteArray raw_array = get_array(m_env, destination, bufimpl.m_mid_has_array, bufimpl.m_mid_get_array); if (raw_array) { const jint array_offset = get_array_offset( m_env, destination, bufimpl.m_mid_get_array_offset); ByteArray array(m_env, raw_array); ByteArray::MutableContents contents(array); data = contents.data(); data = static_cast<char*>(data) + position + array_offset; bytes_read = m_reader(m_env, data, remaining); } } if (data) { if (bytes_read > 0) set_position(m_env, destination, bufimpl.m_mid_set_position, position + bytes_read); return bytes_read; } // No accessible array, either. Oh well. Create a byte array and // push it into the buffer. ByteArray array(m_env, remaining); ByteArray::MutableContents contents(array); bytes_read = m_reader(m_env, contents.data(), contents.length()); if (bytes_read > 0) put_bytearray(m_env, destination, bufimpl.m_mid_put_bytearray, array, bytes_read); return bytes_read; }
jint ByteChannel::write(jobject source) { const ByteBuffer::ClassImpl& bufimpl = ByteBuffer::impl(m_env); const jint remaining = get_remaining(m_env, source, bufimpl.m_mid_get_remaining); if (!remaining) { // No data in the buffer; don't try to write anything. return 0; } const jint position = get_position(m_env, source, bufimpl.m_mid_get_position); jint bytes_written = 0; const void* data = m_env.GetDirectBufferAddress(source); if (data) { data = static_cast<const char*>(data) + position; bytes_written = m_writer(m_env, data, remaining); } else { // It was not a direct buffer ... see if it has an array. jbyteArray raw_array = get_array(m_env, source, bufimpl.m_mid_has_array, bufimpl.m_mid_get_array); if (raw_array) { const jint array_offset = get_array_offset( m_env, source, bufimpl.m_mid_get_array_offset); const ByteArray array(m_env, raw_array); ByteArray::Contents contents(array); data = contents.data(); data = static_cast<const char*>(data) + position + array_offset; bytes_written = m_writer(m_env, data, remaining); } } if (data) { if (bytes_written > 0) set_position(m_env, source, bufimpl.m_mid_set_position, position + bytes_written); return bytes_written; } // No accessible array, either. Oh well. Get an array from the // buffer and read data from that. ByteArray array(m_env, remaining); get_bytearray(m_env, source, bufimpl.m_mid_get_bytearray, array); ByteArray::Contents contents(array); bytes_written = m_writer(m_env, contents.data(), contents.length()); return bytes_written; }
bool data_stream::read_array(std::vector<uint8>& buf) { if (get_remaining() > 1024U*1024U*1024U) return false; uint32 bytes_to_read = static_cast<uint32>(get_remaining()); if (!bytes_to_read) { buf.resize(0); return true; } if (buf.size() < bytes_to_read) buf.resize(bytes_to_read); return read(&buf[0], bytes_to_read) == bytes_to_read; }
bool data_stream::read_array(vector<uint8>& buf) { if (buf.size() < get_remaining()) { if (get_remaining() > 1024U*1024U*1024U) return false; buf.resize((uint)get_remaining()); } if (!get_remaining()) { buf.resize(0); return true; } return read(&buf[0], buf.size()) == buf.size(); }
int DXBC_Reader::get_int32() { if (get_remaining() < 4) throw Exception("Unknown DXBC format"); int value = *(read_ptr++); value |= *(read_ptr++) << 8; value |= *(read_ptr++) << 16; value |= *(read_ptr++) << 24; return value; }
bool data_stream::read_array(vector<uint8> &buf) { if (buf.size() < get_remaining()) { //if (get_remaining() > 1024U*1024U*1024U) if (get_remaining() > static_cast<uint64_t>(cINT32_MAX)) return false; if (!buf.try_resize(static_cast<uint>(get_remaining()))) return false; } if (!get_remaining()) { buf.resize(0); return true; } return read(&buf[0], buf.size()) == buf.size(); }
const char * DXBC_Reader::get_string(int offset) { if ((offset < 0) || (offset > get_remaining())) throw Exception("Unknown DXBC format"); const char *str_ptr = (const char *) read_ptr; str_ptr += offset; // Validate string .. check for end NUL const unsigned char *check_ptr = (const unsigned char *) str_ptr; while(*check_ptr) { check_ptr++; if (check_ptr >= end_ptr) throw Exception("Unknown DXBC format"); } return str_ptr; }
void DXBC_Reader::skip_bytes(int size) { if (get_remaining() < size) throw Exception("Unknown DXBC format"); read_ptr+=size; }