@Override public void write(final int b) { if (position >= array.length) array = ByteArrays.grow(array, position + 1, length); array[position++] = (byte)b; if (length < position) length = position; }
/** * Grows the given array to the maximum between the given length and the current * length increased by 50%, provided that the given length is larger than the * current length. * * <p> * If you want complete control on the array growth, you should probably use * {@code ensureCapacity()} instead. * * @param array * an array. * @param length * the new minimum length for this array. * @return {@code array}, if it can contain {@code length} entries; otherwise, * an array with max({@code length},{@code array.length}/φ) entries * whose first {@code array.length} entries are the same as those of * {@code array}. */ public static byte[] grow(final byte[] array, final int length) { return grow(array, length, array.length); } /**
@Override public void write(final byte[] b, final int off, final int len) throws IOException { ByteArrays.ensureOffsetLength(b, off, len); if (position + len > array.length) array = ByteArrays.grow(array, position + len, position); System.arraycopy(b, off, array, position, len); if (position + len > length) length = position += len; }
@Override public void enqueue(byte x) { if (size == heap.length) heap = ByteArrays.grow(heap, size + 1); heap[size++] = x; ByteHeaps.upHeap(heap, size, size - 1, c); } @Override
private final int readLine( final FastBufferedInputStream fbis ) throws IOException { int start = 0, len; while( ( len = fbis.readLine( lineBuffer, start, lineBuffer.length - start, FastBufferedInputStream.ALL_TERMINATORS ) ) == lineBuffer.length - start ) { start += len; lineBuffer = ByteArrays.grow( lineBuffer, lineBuffer.length + 1 ); } if ( len != -1 ) start += len; return len == -1 ? -1 : start; }
private final int readLine( final FastBufferedInputStream fbis ) throws IOException { int start = 0, len; while( ( len = fbis.readLine( lineBuffer, start, lineBuffer.length - start, FastBufferedInputStream.ALL_TERMINATORS ) ) == lineBuffer.length - start ) { start += len; lineBuffer = ByteArrays.grow( lineBuffer, lineBuffer.length + 1 ); } if ( len != -1 ) start += len; return len == -1 ? -1 : start; }
private final int readLine( final FastBufferedInputStream fbis ) throws IOException { int start = 0, len; while( ( len = fbis.readLine( lineBuffer, start, lineBuffer.length - start, FastBufferedInputStream.ALL_TERMINATORS ) ) == lineBuffer.length - start ) { start += len; lineBuffer = ByteArrays.grow( lineBuffer, lineBuffer.length + 1 ); } if ( len != -1 ) start += len; return len == -1 ? -1 : start; }
private final int readLine( final FastBufferedInputStream fbis ) throws IOException { int start = 0, len; while( ( len = fbis.readLine( lineBuffer, start, lineBuffer.length - start, FastBufferedInputStream.ALL_TERMINATORS ) ) == lineBuffer.length - start ) { start += len; lineBuffer = ByteArrays.grow( lineBuffer, lineBuffer.length + 1 ); } if ( len != -1 ) start += len; return len == -1 ? -1 : start; }
@Override public void enqueue(byte x) { if (size == array.length) array = ByteArrays.grow(array, size + 1); if (firstIndexValid) { if (c == null) { if (((x) < (array[firstIndex]))) firstIndex = size; } else if (c.compare(x, array[firstIndex]) < 0) firstIndex = size; } else firstIndexValid = false; array[size++] = x; } @Override
private void write( final int b ) { if ( avail == 0 ) { final int oldLength = buffer.length; try { buffer = ByteArrays.grow( buffer, buffer.length + 1 ); } catch( OutOfMemoryError e ) { outOfMemoryError = true; try { // We try at all costs to avoid out-of-memory errors: we dump the buffer, try to allocate a slightly larger array and reload it. File temp = File.createTempFile( ByteArrayPostingList.class.getSimpleName(), "dump" ); temp.deleteOnExit(); BinIO.storeBytes( buffer, temp ); buffer = null; buffer = new byte[ oldLength + POSTINGS_EMERGENCY_INCREMENT ]; BinIO.loadBytes( temp, buffer ); temp.delete(); } catch ( IOException f ) { throw new RuntimeException( f ); } } avail += buffer.length - oldLength; } avail--; buffer[ pos++ ] = (byte)b; }
private void write( final int b ) { if ( avail == 0 ) { final int oldLength = buffer.length; try { buffer = ByteArrays.grow( buffer, buffer.length + 1 ); } catch( OutOfMemoryError e ) { outOfMemoryError = true; try { // We try at all costs to avoid out-of-memory errors: we dump the buffer, try to allocate a slightly larger array and reload it. File temp = File.createTempFile( ByteArrayPostingList.class.getSimpleName(), "dump" ); temp.deleteOnExit(); BinIO.storeBytes( buffer, temp ); buffer = null; buffer = new byte[ oldLength + POSTINGS_EMERGENCY_INCREMENT ]; BinIO.loadBytes( temp, buffer ); temp.delete(); } catch ( IOException f ) { throw new RuntimeException( f ); } } avail += buffer.length - oldLength; } avail--; buffer[ pos++ ] = (byte)b; }
private void write( final int b ) { if ( avail == 0 ) { final int oldLength = buffer.length; try { buffer = ByteArrays.grow( buffer, buffer.length + 1 ); } catch( OutOfMemoryError e ) { outOfMemoryError = true; try { // We try at all costs to avoid out-of-memory errors: we dump the buffer, try to allocate a slightly larger array and reload it. File temp = File.createTempFile( ByteArrayPostingList.class.getSimpleName(), "dump" ); temp.deleteOnExit(); BinIO.storeBytes( buffer, temp ); buffer = null; buffer = new byte[ oldLength + POSTINGS_EMERGENCY_INCREMENT ]; BinIO.loadBytes( temp, buffer ); temp.delete(); } catch ( IOException f ) { throw new RuntimeException( f ); } } avail += buffer.length - oldLength; } avail--; buffer[ pos++ ] = (byte)b; }
private void write( final int b ) { if ( avail == 0 ) { final int oldLength = buffer.length; try { buffer = ByteArrays.grow( buffer, buffer.length + 1 ); } catch( OutOfMemoryError e ) { outOfMemoryError = true; try { // We try at all costs to avoid out-of-memory errors: we dump the buffer, try to allocate a slightly larger array and reload it. File temp = File.createTempFile( ByteArrayPostingList.class.getSimpleName(), "dump" ); temp.deleteOnExit(); BinIO.storeBytes( buffer, temp ); buffer = null; buffer = new byte[ oldLength + POSTINGS_EMERGENCY_INCREMENT ]; BinIO.loadBytes( temp, buffer ); temp.delete(); } catch ( IOException f ) { throw new RuntimeException( f ); } } avail += buffer.length - oldLength; } avail--; buffer[ pos++ ] = (byte)b; }
@Override public boolean hasNext() { if (ready) return true; if (fbis == null) return false; ready = false; try { read = 0; int len; while((len = fbis.readLine(buffer, read, buffer.length - read, terminators)) == buffer.length - read) { ready = true; read += len; buffer = ByteArrays.grow(buffer, buffer.length + 1); } if (len != -1) { ready = true; read += len; } if (len == -1) close(); } catch (final IOException e) { throw new RuntimeException(e); } return ready; }
while((len = inputStream.readLine(array, start, array.length - start, terminators)) == array.length - start) { start += len; array = ByteArrays.grow(array, array.length + 1); };
/** * Unwraps an iterator, returning an array, with a limit on the number of * elements. * * <p> * This method iterates over the given type-specific iterator and returns an * array containing the elements returned by the iterator. At most {@code max} * elements will be returned. * * @param i * a type-specific iterator. * @param max * the maximum number of elements to be unwrapped. * @return an array containing the elements returned by the iterator (at most * {@code max}). */ public static byte[] unwrap(final ByteIterator i, int max) { if (max < 0) throw new IllegalArgumentException("The maximum number of elements (" + max + ") is negative"); byte array[] = new byte[16]; int j = 0; while (max-- != 0 && i.hasNext()) { if (j == array.length) array = ByteArrays.grow(array, j + 1); array[j++] = i.nextByte(); } return ByteArrays.trim(array, j); } /**
while((len = inputStream.readLine(array, start, array.length - start, terminators)) == array.length - start) { start += len; array = ByteArrays.grow(array, array.length + 1); };
for( int i = 0; i < NUM_FIELDS; i++ ) { buffer[ i ] = ByteArrays.grow( buffer[ i ], bufferSize[ i ] + 3 ); buffer[ i ][ bufferSize[ i ]++ ] = (byte)0xc2; buffer[ i ][ bufferSize[ i ]++ ] = (byte)0xb6; buffer[ field ] = ByteArrays.grow( buffer[ field ], bufferSize[ field ] + 2 ); buffer[ field ][ bufferSize[ field ]++ ] = lineBuffer[ i ];