protected long computeEstimatedMemorySize() { long result = 0; if (!textArray.isDirect()) result += (Character.SIZE / 8) * textArray.capacity(); result += (Integer.SIZE / 8) * textIndexArray.length; result += (Double.SIZE / 8) * latlonArray.length; return result; }
/** * Write a string to the network. The length header of the string is written automatically and its * encoded to the correct CharSet automatically. * * @param value the string that shall be send to the server */ @Override public void writeString(@Nonnull String value) throws CharacterCodingException { int startIndex = buffer.position(); buffer.putShort((short) 0); encodingBuffer.clear(); encodingBuffer.put(value, 0, Math.min(encodingBuffer.capacity(), value.length())); encodingBuffer.flip(); do { CoderResult encodingResult = encoder.encode(encodingBuffer, buffer, true); if (!encodingResult.isError()) { break; } if (encodingResult.isUnmappable()) { log.warn( "Found a character that failed to encode for the transfer to the server: {} - SKIP", encodingBuffer.get()); } else { encodingResult.throwException(); } } while (encodingBuffer.hasRemaining()); int lastIndex = buffer.position(); buffer.position(startIndex); writeUShort(lastIndex - startIndex - 2); buffer.position(lastIndex); }
/** * Adds the data to the buffer for textual data. If a binary message is currently in progress that * message will be completed and a new textual message started. If the buffer for textual data is * full, the buffer will be flushed and a new textual continuation fragment started. * * @param c The character to send to the client. * @throws IOException If a flush is required and an error occurs writing the WebSocket frame to * the client */ public void writeTextData(char c) throws IOException { try { synchronized (stateLock) { if (closed) { throw new IOException(sm.getString("outbound.closed")); } if (cb.position() == cb.capacity()) { doFlush(false); } if (text == null) { text = Boolean.TRUE; } else if (text == Boolean.FALSE) { // Flush the binary data flush(); text = Boolean.TRUE; } cb.append(c); } } catch (IOException ioe) { // Any IOException is terminal. Make sure the Inbound side knows // that something went wrong. // The exception handling needs to be outside of the sync to avoid // possible deadlocks (e.g. BZ55524) when triggering the inbound // close as that will execute user code streamInbound.doOnClose(Constants23.getStatusClosedUnexpectedly()); throw ioe; } }
public void createBufferObjects(GL gl) { checkGLError(gl); // Generate a the vertex and element buffer IDs int[] vboIds = new int[2]; GL11 gl11 = (GL11) gl; gl11.glGenBuffers(2, vboIds, 0); mVertexBufferObjectId = vboIds[0]; mElementBufferObjectId = vboIds[1]; // Upload the vertex data gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mVertexBufferObjectId); mVertexByteBuffer.position(0); gl11.glBufferData( GL11.GL_ARRAY_BUFFER, mVertexByteBuffer.capacity(), mVertexByteBuffer, GL11.GL_STATIC_DRAW); gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, mElementBufferObjectId); mIndexBuffer.position(0); gl11.glBufferData( GL11.GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer.capacity() * CHAR_SIZE, mIndexBuffer, GL11.GL_STATIC_DRAW); // We don't need the in-memory data any more mVertexBuffer = null; mVertexByteBuffer = null; mIndexBuffer = null; checkGLError(gl); }
private void ensureCharBuffRemaining(final int size) { if (charBuff.remaining() < size) { final int cpcty = (charBuff.capacity() + size) * 2; final java.nio.CharBuffer newChBuf = java.nio.CharBuffer.allocate(cpcty); newChBuf.put(charBuff); charBuff = newChBuf; } }
public void mergeNormals() { FloatBuffer normalAccumulationBuffer = FloatBuffer.allocate(mVertices.capacity()); for (int i = 0; i < mIndices.capacity(); i++) { int indice = mIndices.get(i); int offset_indice = indice * D3DMesh.nbFloatPerVertex; for (int j = 0; j < mIndices.capacity(); j++) { char indice2 = mIndices.get(j); int offset_indice2 = indice2 * D3DMesh.nbFloatPerVertex; float x = mVertices.get(indice * D3DMesh.nbFloatPerVertex); float y = mVertices.get(indice * D3DMesh.nbFloatPerVertex + 1); float z = mVertices.get(indice * D3DMesh.nbFloatPerVertex + 2); float x2 = mVertices.get(indice2 * D3DMesh.nbFloatPerVertex); float y2 = mVertices.get(indice2 * D3DMesh.nbFloatPerVertex + 1); float z2 = mVertices.get(indice2 * D3DMesh.nbFloatPerVertex + 2); if ((x == x2) && (y == y2) && (z == z2)) { normalAccumulationBuffer.put( offset_indice2 + 3, normalAccumulationBuffer.get(offset_indice2 + 3) + mVertices.get(offset_indice + 3)); normalAccumulationBuffer.put( offset_indice2 + 4, normalAccumulationBuffer.get(offset_indice2 + 4) + mVertices.get(offset_indice + 4)); normalAccumulationBuffer.put( offset_indice2 + 5, normalAccumulationBuffer.get(offset_indice2 + 5) + mVertices.get(offset_indice + 5)); } } } D3DVector normal = new D3DVector(); for (int i = 0; i < mIndices.capacity(); i++) { int indice = mIndices.get(i); int offset_indice = indice * D3DMesh.nbFloatPerVertex; normal.set(0, normalAccumulationBuffer.get(offset_indice + 3)); normal.set(1, normalAccumulationBuffer.get(offset_indice + 4)); normal.set(2, normalAccumulationBuffer.get(offset_indice + 5)); normal.normalize(); mVertices.put(offset_indice + 3, normal.get(0)); mVertices.put(offset_indice + 4, normal.get(1)); mVertices.put(offset_indice + 5, normal.get(2)); } }
private void emit(int c) throws IOException { assert c >= 0; CharBuffer buf = lineBuffer; if (buf.remaining() == 0) { if (buf.capacity() == BUFFER_LIMIT) { throw new IOException( MessageFormat.format( "Line is too large (near {0}:{1}, size={2}, record-number={3})", path, currentPhysicalHeadLine, BUFFER_LIMIT, currentRecordNumber)); } CharBuffer newBuf = CharBuffer.allocate(Math.min(buf.capacity() * 2, BUFFER_LIMIT)); newBuf.clear(); buf.flip(); newBuf.put(buf); buf = newBuf; lineBuffer = newBuf; } buf.put((char) c); }
public void copy(D3DMesh mesh) { mIndices = CharBuffer.allocate(mesh.mIndices.position()); mVertices = FloatBuffer.allocate(mesh.mVertices.position()); for (int j = 0; j < mIndices.capacity(); j++) mIndices.put(mesh.mIndices.get(j)); for (int j = 0; j < mVertices.capacity(); j++) { mVertices.put(mesh.mVertices.get(j)); } mIndices.position(0); mVertices.position(0); }
/** * Allocates hardware buffers on the graphics card and fills them with data if a buffer has not * already been previously allocated. Note that this function uses the GL_OES_vertex_buffer_object * extension, which is not guaranteed to be supported on every device. * * @param gl A pointer to the OpenGL ES context. */ public void generateHardwareBuffers(GL10 gl) { if (!mUseHardwareBuffers) { if (gl instanceof GL11) { GL11 gl11 = (GL11) gl; int[] buffer = new int[1]; // Allocate and fill the vertex buffer. gl11.glGenBuffers(1, buffer, 0); mVertBufferIndex = buffer[0]; gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mVertBufferIndex); final int vertexSize = mVertexBuffer.capacity() * mCoordinateSize; gl11.glBufferData(GL11.GL_ARRAY_BUFFER, vertexSize, mVertexBuffer, GL11.GL_STATIC_DRAW); // Allocate and fill the texture coordinate buffer. gl11.glGenBuffers(1, buffer, 0); mTextureCoordBufferIndex = buffer[0]; gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mTextureCoordBufferIndex); final int texCoordSize = mTexCoordBuffer.capacity() * mCoordinateSize; gl11.glBufferData(GL11.GL_ARRAY_BUFFER, texCoordSize, mTexCoordBuffer, GL11.GL_STATIC_DRAW); // Allocate and fill the color buffer. gl11.glGenBuffers(1, buffer, 0); mColorBufferIndex = buffer[0]; gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mColorBufferIndex); final int colorSize = mColorBuffer.capacity() * mCoordinateSize; gl11.glBufferData(GL11.GL_ARRAY_BUFFER, colorSize, mColorBuffer, GL11.GL_STATIC_DRAW); // Unbind the array buffer. gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0); // Allocate and fill the index buffer. gl11.glGenBuffers(1, buffer, 0); mIndexBufferIndex = buffer[0]; gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, mIndexBufferIndex); // A char is 2 bytes. final int indexSize = mIndexBuffer.capacity() * 2; gl11.glBufferData( GL11.GL_ELEMENT_ARRAY_BUFFER, indexSize, mIndexBuffer, GL11.GL_STATIC_DRAW); // Unbind the element array buffer. gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0); mUseHardwareBuffers = true; assert mVertBufferIndex != 0; assert mTextureCoordBufferIndex != 0; assert mIndexBufferIndex != 0; assert gl11.glGetError() == 0; } } }
/** {@inheritDoc} */ @Override public void write(final byte[] b, final int off, final int len) throws IOException { final ByteBuffer input = ByteBuffer.wrap(b, off, len); final int required = encoder.outputSize(len - off); if (output == null || output.capacity() < required) { output = CharBuffer.allocate(required); } else { output.clear(); } encoder.encode(input, output); output.flip(); writer.write(output.toString()); writer.flush(); }
public void prepareForSending(CharBuffer hdr, CharBuffer cntnt) { if (hdr == null || hdr.capacity() < 1) return; try { ByteBuffer hdrBytes = Charset.forName("iso-8859-1").newEncoder().encode(hdr); ByteBuffer cntntBytes = Charset.forName(Server.srv.DEFAULT_CHARSET).newEncoder().encode(cntnt); buf = ByteBuffer.allocate(hdrBytes.capacity() + cntntBytes.capacity()); buf.put(hdrBytes); buf.put(cntntBytes); buf.flip(); } catch (Exception e) { Server.debug( this, "Exception during prepareForSending(hdr/cntnt)", e, Server.MSG_ERROR, Server.LVL_MAJOR); } }
public static void test(CharBuffer buff, CharBuffer slice) throws RuntimeException { boolean marked = false; try { slice.reset(); marked = true; } catch (InvalidMarkException ime) { // expected } if (marked || slice.position() != 0 || buff.remaining() != slice.limit() || buff.remaining() != slice.capacity()) { throw new RuntimeException("Calling the CharBuffer.slice method failed."); } }
public int mergeVertexIndices() { int countmerge = 0; for (int i = 0; i < mIndices.capacity(); i++) { int indice = mIndices.get(i); for (int j = 0; j < i; j++) { char indice2 = mIndices.get(j); float x = mVertices.get(indice * D3DMesh.nbFloatPerVertex); float y = mVertices.get(indice * D3DMesh.nbFloatPerVertex + 1); float z = mVertices.get(indice * D3DMesh.nbFloatPerVertex + 2); float x2 = mVertices.get(indice2 * D3DMesh.nbFloatPerVertex); float y2 = mVertices.get(indice2 * D3DMesh.nbFloatPerVertex + 1); float z2 = mVertices.get(indice2 * D3DMesh.nbFloatPerVertex + 2); if ((x == x2) && (y == y2) && (z == z2)) { mIndices.put(i, indice2); countmerge++; } } } return countmerge; }
public static void main(String[] args) throws Exception { System.out.println(">>> StringCharBufferSliceTest-main: testing the slice method..."); final String in = "for testing"; System.out.println(">>> StringCharBufferSliceTest-main: testing with the position 0."); CharBuffer buff = CharBuffer.wrap(in); test(buff, buff.slice()); System.out.println(">>> StringCharBufferSliceTest-main: testing with new position."); buff.position(2); test(buff, buff.slice()); System.out.println( ">>> StringCharBufferSliceTest-main: testing with non zero initial position."); buff = CharBuffer.wrap(in, 3, in.length()); test(buff, buff.slice()); System.out.println(">>> StringCharBufferSliceTest-main: testing slice result with get()"); buff.position(4); buff.limit(7); CharBuffer slice = buff.slice(); for (int i = 0; i < 3; i++) { if (slice.get() != buff.get()) { throw new RuntimeException("Wrong characters in slice result."); } } System.out.println(">>> StringCharBufferSliceTest-main: testing slice result with get(int)"); buff.position(4); buff.limit(7); slice = buff.slice(); for (int i = 0; i < 3; i++) { if (slice.get(i) != buff.get(4 + i)) { throw new RuntimeException("Wrong characters in slice result."); } } System.out.println(">>> StringCharBufferSliceTest-main: testing slice with result of slice"); buff.position(0); buff.limit(buff.capacity()); slice = buff.slice(); for (int i = 0; i < 4; i++) { slice.position(i); CharBuffer nextSlice = slice.slice(); if (nextSlice.position() != 0) throw new RuntimeException("New buffer's position should be zero"); if (!nextSlice.equals(slice)) throw new RuntimeException("New buffer should be equal"); slice = nextSlice; } System.out.println(">>> StringCharBufferSliceTest-main: testing toString."); buff.position(4); buff.limit(7); slice = buff.slice(); if (!slice.toString().equals("tes")) { throw new RuntimeException("bad toString() after slice(): " + slice.toString()); } System.out.println(">>> StringCharBufferSliceTest-main: testing subSequence."); buff.position(4); buff.limit(8); slice = buff.slice(); CharSequence subSeq = slice.subSequence(1, 3); if (subSeq.charAt(0) != 'e' || subSeq.charAt(1) != 's') { throw new RuntimeException("bad subSequence() after slice(): '" + subSeq + "'"); } System.out.println(">>> StringCharBufferSliceTest-main: testing duplicate."); buff.position(4); buff.limit(8); slice = buff.slice(); CharBuffer dupe = slice.duplicate(); if (dupe.charAt(0) != 't' || dupe.charAt(1) != 'e' || dupe.charAt(2) != 's' || dupe.charAt(3) != 't') { throw new RuntimeException("bad duplicate() after slice(): '" + dupe + "'"); } System.out.println(">>> StringCharBufferSliceTest-main: done!"); }
@Override public void read(Multiplexer multiplexer, SelectionKey key) throws Exception { /* Protect against clients sending large commands. We * could enlarge the command buffer, but this would open * the server to DOS attacks from clients sending very * large commands. */ if (!_command.hasRemaining()) { throw new FTPException("Command buffer full"); } /* Read available data. */ long nbytes = _socket.read(_command); if (nbytes == -1) { if (_state == SenderState.WAIT_READY) { /* From the GridFTP v2 spec: "Passive receiver may * close new data socket without sending 'READY' * message or even stop accepting new * connections." * * We extend this to also cover active receivers. */ close(multiplexer, key, _eof); return; } else { throw new FTPException("Lost connection"); } } /* Decode buffer. */ _command.flip(); _decoder.decode(_command, _decodedCommand, false); _command.compact(); /* Remove first line from buffer. */ char c; StringBuffer line = new StringBuffer(); _decodedCommand.flip(); do { /* Return early if command is incomplete. */ if (!_decodedCommand.hasRemaining()) { _decodedCommand.limit(_decodedCommand.capacity()); return; } c = _decodedCommand.get(); line.append(c); } while (c != '\n'); _decodedCommand.compact(); /* Split line into arguments. */ String[] arg = Pattern.compile("\\s").split(line); if (arg.length == 0) { throw new FTPException("Empty command received (protocol violation)"); } /* Interpret command. */ String cmd = arg[0]; if (cmd.equals("READY") && _state == SenderState.WAIT_READY) { _state = SenderState.NEXT_BLOCK; key.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE); } else if (cmd.equals("BYE") && _state == SenderState.WAIT_BYE) { // shut down close(multiplexer, key, _eof); } else if (cmd.equals("CLOSE")) { // shutdown the channel at the end of the current block _closeAtNextBlock = true; } else if (cmd.equals("RESEND")) { // resend a block throw new FTPException("RESEND is not implemented"); } else { throw new FTPException("Unexpected command '" + cmd + "' in state " + _state); } }
/** Check if the buffer is big enough */ private void updateBuffer() { if (termBuffer.capacity() < termAtt.length()) { termBuffer = CharBuffer.allocate(termAtt.length()); } termBuffer.clear(); }
/** * @return <code>true</code> if sufficient data was present to process all of the initial header */ private boolean processInitialHeader() throws IOException { // Need at least two bytes of data to do this if (writePos - readPos < 2) { return false; } int b = inputBuffer[readPos++]; fin = (b & 0x80) > 0; rsv = (b & 0x70) >>> 4; opCode = (byte) (b & 0x0F); if (!transformation.validateRsv(rsv, opCode)) { throw new WsIOException( new CloseReason( CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.wrongRsv", Integer.valueOf(rsv), Integer.valueOf(opCode)))); } if (Util.isControl(opCode)) { if (!fin) { throw new WsIOException( new CloseReason(CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.controlFragmented"))); } if (opCode != Constants.OPCODE_PING && opCode != Constants.OPCODE_PONG && opCode != Constants.OPCODE_CLOSE) { throw new WsIOException( new CloseReason( CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.invalidOpCode", Integer.valueOf(opCode)))); } } else { if (continuationExpected) { if (!Util.isContinuation(opCode)) { throw new WsIOException( new CloseReason(CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.noContinuation"))); } } else { try { if (opCode == Constants.OPCODE_BINARY) { // New binary message textMessage = false; int size = wsSession.getMaxBinaryMessageBufferSize(); if (size != messageBufferBinary.capacity()) { messageBufferBinary = ByteBuffer.allocate(size); } binaryMsgHandler = wsSession.getBinaryMessageHandler(); textMsgHandler = null; } else if (opCode == Constants.OPCODE_TEXT) { // New text message textMessage = true; int size = wsSession.getMaxTextMessageBufferSize(); if (size != messageBufferText.capacity()) { messageBufferText = CharBuffer.allocate(size); } binaryMsgHandler = null; textMsgHandler = wsSession.getTextMessageHandler(); } else { throw new WsIOException( new CloseReason( CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.invalidOpCode", Integer.valueOf(opCode)))); } } catch (IllegalStateException ise) { // Thrown if the session is already closed throw new WsIOException( new CloseReason(CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.sessionClosed"))); } } continuationExpected = !fin; } b = inputBuffer[readPos++]; // Client data must be masked if ((b & 0x80) == 0 && isMasked()) { throw new WsIOException( new CloseReason(CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.notMasked"))); } payloadLength = b & 0x7F; state = State.PARTIAL_HEADER; return true; }
private String checkDeclaration(String defEncoding) { // Convert the CharBuffer to a string buffer.limit(buffer.position()); buffer.position(0); StringBuffer text = new StringBuffer(buffer.toString()); // Look for XML encoding declaration String encoding = defEncoding; Matcher m = xmlEncDecl.matcher(text); if (m.find()) { // We have an XML encoding declaration isXML = true; // Get the declared encoding String delim = String.valueOf(text.charAt(m.end() - 1)); int end = text.indexOf(delim, m.end()); if (end != -1) { encoding = text.substring(m.end(), end); // End replace the current declaration by the new one text.replace(m.end(), end, outputEncoding); } } else { // No XML encoding declaration found: Check if it is XML m = xmlDecl.matcher(text); if (m.find()) { // It is XML without encoding declaration isXML = true; // Encoding should UTF-8 or UTF-16/32, we will detect those later encoding = "UTF-8"; // Add the encoding after the version String delim = String.valueOf(text.charAt(m.end() - 1)); int end = text.indexOf(delim, m.end()); if (end != -1) { text.insert(end + 1, " encoding=\"" + outputEncoding + "\""); } } else { // No XML declaration found, maybe it's an XML without one if (isXML) { // Was a .xml extension, assume UTF-8 encoding = "UTF-8"; text.insert(0, "<?xml version=\"1.0\" encoding=\"" + outputEncoding + "\" ?>"); } } } // Look for HTML declarations m = htmlEncDecl.matcher(text); if (m.find()) { isHTML = true; // Group 11 contains the encoding name encoding = m.group(11); // Replace it by the new encoding int n = text.indexOf(encoding, m.start()); text.replace(n, n + encoding.length(), outputEncoding); } else if (isHTML) { // No HTML encoding found, but try to update if it was seen as HTML from // extension // Try to place it after <head> m = htmlHead.matcher(text); if (m.find()) { text.insert( m.end(), String.format( "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\"></meta>", outputEncoding)); } else { // If no <head>, try <html> m = htmlDecl.matcher(text); if (m.find()) { int n = text.indexOf(">", m.end()); if (n != -1) { text.insert( n + 1, String.format( "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\"></meta></head>", outputEncoding)); } } } } // Convert the string back to a CharBuffer int len = text.length(); // Make sure we have room for added characters if (len > buffer.capacity()) { buffer = CharBuffer.allocate(len); } else { buffer.clear(); } buffer.append(text.toString()); buffer.limit(len); return encoding; }