public static List<ByteBuffer> mergeAdjacentBuffers(List<ByteBuffer> samples) {
   ArrayList<ByteBuffer> nuSamples = new ArrayList<ByteBuffer>(samples.size());
   for (ByteBuffer buffer : samples) {
     int lastIndex = nuSamples.size() - 1;
     if (lastIndex >= 0
         && buffer.hasArray()
         && nuSamples.get(lastIndex).hasArray()
         && buffer.array() == nuSamples.get(lastIndex).array()
         && nuSamples.get(lastIndex).arrayOffset() + nuSamples.get(lastIndex).limit()
             == buffer.arrayOffset()) {
       ByteBuffer oldBuffer = nuSamples.remove(lastIndex);
       ByteBuffer nu =
           ByteBuffer.wrap(
                   buffer.array(), oldBuffer.arrayOffset(), oldBuffer.limit() + buffer.limit())
               .slice();
       // We need to slice here since wrap([], offset, length) just sets position and not the
       // arrayOffset.
       nuSamples.add(nu);
     } else if (lastIndex >= 0
         && buffer instanceof MappedByteBuffer
         && nuSamples.get(lastIndex) instanceof MappedByteBuffer
         && nuSamples.get(lastIndex).limit()
             == nuSamples.get(lastIndex).capacity() - buffer.capacity()) {
       // This can go wrong - but will it?
       ByteBuffer oldBuffer = nuSamples.get(lastIndex);
       oldBuffer.limit(buffer.limit() + oldBuffer.limit());
     } else {
       nuSamples.add(buffer);
     }
   }
   return nuSamples;
 }
  private static void encode(final int[][] table, ByteBuffer buffer, String s) {
    long current = 0;
    int n = 0;

    byte[] array = buffer.array();
    int p = buffer.arrayOffset() + buffer.position();

    int len = s.length();
    for (int i = 0; i < len; i++) {
      char c = s.charAt(i);
      if (c >= 128 || c < ' ') throw new IllegalArgumentException();
      int code = table[c][0];
      int bits = table[c][1];

      current <<= bits;
      current |= code;
      n += bits;

      while (n >= 8) {
        n -= 8;
        array[p++] = (byte) (current >> n);
      }
    }

    if (n > 0) {
      current <<= (8 - n);
      current |= (0xFF >>> n);
      array[p++] = (byte) current;
    }

    buffer.position(p - buffer.arrayOffset());
  }
 private Cell toOnheapCell(
     ByteBuffer valAndTagsBuffer, int vOffset, int tagsLenSerializationSize) {
   byte[] tagsArray = HConstants.EMPTY_BYTE_ARRAY;
   int tOffset = 0;
   if (this.includeTags) {
     if (this.tagCompressionContext == null) {
       tagsArray = valAndTagsBuffer.array();
       tOffset =
           valAndTagsBuffer.arrayOffset()
               + vOffset
               + this.valueLength
               + tagsLenSerializationSize;
     } else {
       tagsArray = Bytes.copy(tagsBuffer, 0, this.tagsLength);
       tOffset = 0;
     }
   }
   return new OnheapDecodedCell(
       Bytes.copy(keyBuffer, 0, this.keyLength),
       currentKey.getRowLength(),
       currentKey.getFamilyOffset(),
       currentKey.getFamilyLength(),
       currentKey.getQualifierOffset(),
       currentKey.getQualifierLength(),
       currentKey.getTimestamp(),
       currentKey.getTypeByte(),
       valAndTagsBuffer.array(),
       valAndTagsBuffer.arrayOffset() + vOffset,
       this.valueLength,
       memstoreTS,
       tagsArray,
       tOffset,
       this.tagsLength);
 }
    private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
      byte[] sa = src.array();
      int sp = src.arrayOffset() + src.position();
      int sl = src.arrayOffset() + src.limit();
      assert (sp <= sl);
      sp = (sp <= sl ? sp : sl);
      char[] da = dst.array();
      int dp = dst.arrayOffset() + dst.position();
      int dl = dst.arrayOffset() + dst.limit();
      assert (dp <= dl);
      dp = (dp <= dl ? dp : dl);

      try {
        while (sp < sl) {
          byte b = sa[sp];
          if (b >= 0) {
            if (dp >= dl) return CoderResult.OVERFLOW;
            da[dp++] = (char) b;
            sp++;
            continue;
          }
          return CoderResult.malformedForLength(1);
        }
        return CoderResult.UNDERFLOW;
      } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
      }
    }
Beispiel #5
0
  public static WavInfo readHeader(InputStream wavStream) throws IOException, DecoderException {

    ByteBuffer buffer = ByteBuffer.allocate(HEADER_SIZE);
    buffer.order(ByteOrder.LITTLE_ENDIAN);

    wavStream.read(buffer.array(), buffer.arrayOffset(), buffer.capacity());

    buffer.rewind();
    buffer.position(buffer.position() + 20);
    int format = buffer.getShort();
    checkFormat(format == 1, "Unsupported encoding: " + format); // 1 means Linear PCM
    int channels = buffer.getShort();
    checkFormat(channels == 1 || channels == 2, "Unsupported channels: " + channels);
    int rate = buffer.getInt();
    checkFormat(rate <= 48000 && rate >= 11025, "Unsupported rate: " + rate);
    buffer.position(buffer.position() + 6);
    int bits = buffer.getShort();
    checkFormat(bits == 16, "Unsupported bits: " + bits);
    int dataSize = 0;
    while (buffer.getInt() != 0x61746164) { // "data" marker
      Log.d(TAG, "Skipping non-data chunk");
      int size = buffer.getInt();
      wavStream.skip(size);

      buffer.rewind();
      wavStream.read(buffer.array(), buffer.arrayOffset(), 8);
      buffer.rewind();
    }
    dataSize = buffer.getInt();
    checkFormat(dataSize > 0, "wrong datasize: " + dataSize);

    return new WavInfo(rate, channels == 2, dataSize);
  }
Beispiel #6
0
  @Override
  public TransformationResult getMoreData(byte opCode, boolean fin, int rsv, ByteBuffer dest)
      throws IOException {
    // Control frames are never compressed and may appear in the middle of
    // a WebSocket method. Pass them straight through.
    if (Util.isControl(opCode)) {
      return next.getMoreData(opCode, fin, rsv, dest);
    }

    if (!Util.isContinuation(opCode)) {
      // First frame in new message
      skipDecompression = (rsv & RSV_BITMASK) == 0;
    }

    // Pass uncompressed frames straight through.
    if (skipDecompression) {
      return next.getMoreData(opCode, fin, rsv, dest);
    }

    int written;
    boolean usedEomBytes = false;

    while (dest.remaining() > 0) {
      // Space available in destination. Try and fill it.
      try {
        written =
            inflater.inflate(dest.array(), dest.arrayOffset() + dest.position(), dest.remaining());
      } catch (DataFormatException e) {
        throw new IOException(sm.getString("perMessageDeflate.deflateFailed"), e);
      }
      dest.position(dest.position() + written);

      if (inflater.needsInput() && !usedEomBytes) {
        if (dest.hasRemaining()) {
          readBuffer.clear();
          TransformationResult nextResult =
              next.getMoreData(opCode, fin, (rsv ^ RSV_BITMASK), readBuffer);
          inflater.setInput(readBuffer.array(), readBuffer.arrayOffset(), readBuffer.position());
          if (TransformationResult.UNDERFLOW.equals(nextResult)) {
            return nextResult;
          } else if (TransformationResult.END_OF_FRAME.equals(nextResult)
              && readBuffer.position() == 0) {
            if (fin) {
              inflater.setInput(EOM_BYTES);
              usedEomBytes = true;
            } else {
              return TransformationResult.END_OF_FRAME;
            }
          }
        }
      } else if (written == 0) {
        if (fin && (isServer && !clientContextTakeover || !isServer && !serverContextTakeover)) {
          inflater.reset();
        }
        return TransformationResult.END_OF_FRAME;
      }
    }

    return TransformationResult.OVERFLOW;
  }
    private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
      char[] sa = src.array();
      int sp = src.arrayOffset() + src.position();
      int sl = src.arrayOffset() + src.limit();
      assert (sp <= sl);
      sp = (sp <= sl ? sp : sl);
      byte[] da = dst.array();
      int dp = dst.arrayOffset() + dst.position();
      int dl = dst.arrayOffset() + dst.limit();
      assert (dp <= dl);
      dp = (dp <= dl ? dp : dl);

      try {
        while (sp < sl) {
          char c = sa[sp];
          if (c < 0x80) {
            if (dp >= dl) return CoderResult.OVERFLOW;
            da[dp] = (byte) c;
            sp++;
            dp++;
            continue;
          }
          if (sgp.parse(c, sa, sp, sl) < 0) return sgp.error();
          return sgp.unmappableResult();
        }
        return CoderResult.UNDERFLOW;
      } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
      }
    }
Beispiel #8
0
  private static final void acopyArray(
      ByteBuffer bb1, int src, ByteBuffer bb2, int dst, int length, int slotLen) {
    byte[] b1 = bb1.array();
    byte[] b2 = bb2.array();
    int offset1 = bb1.arrayOffset();
    int offset2 = bb2.arrayOffset();

    int bSrc = src * slotLen;
    int bDst = dst * slotLen;
    int bLen = length * slotLen;

    arraycopy(b1, offset1 + bSrc, b2, offset2 + bDst, bLen);
  }
Beispiel #9
0
 /** Called from the ExecutionEngine to request serialized dependencies. */
 public byte[] nextDependencyAsBytes(final int dependencyId) {
   final VoltTable vt = m_dependencyTracker.nextDependency(dependencyId);
   if (vt != null) {
     final ByteBuffer buf2 = vt.getTableDataReference();
     byte[] bytes = buf2.array();
     // if a buffer has an offset, just getting the array will give you the wrong thing
     if (buf2.arrayOffset() != 0) {
       bytes = Arrays.copyOfRange(bytes, buf2.arrayOffset(), bytes.length);
     }
     return bytes;
   } else {
     return null;
   }
 }
  private int sendFromManagedBuffer(FileDescriptor fd, ByteBuffer bb, InetSocketAddress target)
      throws IOException {
    int pos = bb.position();
    int lim = bb.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    boolean preferIPv6 = (family != StandardProtocolFamily.INET);
    int written;
    try {
      written =
          send0(
              preferIPv6,
              fd,
              bb.array(),
              bb.arrayOffset() + pos,
              rem,
              target.getAddress(),
              target.getPort());
    } catch (PortUnreachableException pue) {
      if (isConnected()) throw pue;
      written = rem;
    }
    if (written > 0) bb.position(pos + written);
    return written;
  }
Beispiel #11
0
 @Override
 public ByteBuffer readPage(File file, long position, ByteBuffer pageBuffer)
     throws IOException, InterruptedException {
   long start = System.currentTimeMillis();
   RandomAccessFile randomAccessFile = randomAccessFile(file);
   try {
     randomAccessFile.seek(position);
     randomAccessFile.readFully(pageBuffer.array(), pageBuffer.arrayOffset(), pageSizeBytes);
     if (Thread.interrupted()) {
       throw new InterruptedException();
     }
     long stop = System.currentTimeMillis();
     if (LOG.isLoggable(Level.FINE)) {
       LOG.log(
           Level.FINE,
           "Read page at {0} of {1}: {2} msec",
           new Object[] {position, file, stop - start});
     }
   } catch (EOFException e) {
     LOG.log(
         Level.SEVERE,
         "Caught EOFException while reading {0}, position {1}",
         new Object[] {file, position});
     LOG.log(Level.SEVERE, "stack", e);
     throw e;
   } finally {
     randomAccessFile.close();
   }
   return pageBuffer;
 }
Beispiel #12
0
 /**
  * Continues a multi-part transformation (encryption or decryption). The {@code input.remaining()}
  * bytes starting at {@code input.position()} are transformed and stored in the {@code output}
  * buffer.
  *
  * <p>If the {@code output.remaining()} is too small to hold the transformed bytes a {@code
  * ShortBufferException} is thrown. Use {@link Cipher#getOutputSize getOutputSize} to check for
  * the size of the output buffer.
  *
  * @param input the input buffer to transform.
  * @param output the output buffer to store the result within.
  * @return the number of bytes stored in the output buffer.
  * @throws ShortBufferException if the size of the {@code output} buffer is too small.
  */
 protected int engineUpdate(ByteBuffer input, ByteBuffer output) throws ShortBufferException {
   if (input == null) {
     throw new NullPointerException("input == null");
   }
   if (output == null) {
     throw new NullPointerException("output == null");
   }
   int position = input.position();
   int limit = input.limit();
   if ((limit - position) <= 0) {
     return 0;
   }
   byte[] bInput;
   byte[] bOutput;
   if (input.hasArray()) {
     bInput = input.array();
     int offset = input.arrayOffset();
     bOutput = engineUpdate(bInput, offset + position, limit - position);
     input.position(limit);
   } else {
     bInput = new byte[limit - position];
     input.get(bInput);
     bOutput = engineUpdate(bInput, 0, limit - position);
   }
   if (output.remaining() < bOutput.length) {
     throw new ShortBufferException("output buffer too small");
   }
   try {
     output.put(bOutput);
   } catch (java.nio.BufferOverflowException e) {
     throw new ShortBufferException("output buffer too small");
   }
   return bOutput.length;
 }
 /** Get cash on hand, divide it by the number of outlinks and apply. */
 @Override
 public void distributeScoreToOutlinks(
     String fromUrl, WebPage row, Collection<ScoreDatum> scoreData, int allCount) {
   ByteBuffer cashRaw = row.getMetadata().get(CASH_KEY);
   if (cashRaw == null) {
     return;
   }
   float cash = Bytes.toFloat(cashRaw.array(), cashRaw.arrayOffset() + cashRaw.position());
   if (cash == 0) {
     return;
   }
   // TODO: count filtered vs. all count for outlinks
   float scoreUnit = cash / allCount;
   // internal and external score factor
   float internalScore = scoreUnit * internalScoreFactor;
   float externalScore = scoreUnit * externalScoreFactor;
   for (ScoreDatum scoreDatum : scoreData) {
     try {
       String toHost = new URL(scoreDatum.getUrl()).getHost();
       String fromHost = new URL(fromUrl.toString()).getHost();
       if (toHost.equalsIgnoreCase(fromHost)) {
         scoreDatum.setScore(internalScore);
       } else {
         scoreDatum.setScore(externalScore);
       }
     } catch (MalformedURLException e) {
       LOG.error("Failed with the following MalformedURLException: ", e);
       scoreDatum.setScore(externalScore);
     }
   }
   // reset cash to zero
   row.getMetadata().put(CASH_KEY, ByteBuffer.wrap(Bytes.toBytes(0.0f)));
 }
 /**
  * Writes an ASCII string to the stream followed by \0
  *
  * @param str the String to write
  * @throws IOException if the string couldn't be written
  */
 private void writeCString(final String str) throws IOException {
   final ByteBuffer buf = zipEncoding.encode(str);
   final int len = buf.limit() - buf.position();
   out.write(buf.array(), buf.arrayOffset(), len);
   out.write('\0');
   count(len + 1);
 }
Beispiel #15
0
  static void write(
      OioDatagramChannel channel,
      ChannelFuture future,
      Object message,
      SocketAddress remoteAddress) {
    try {
      ChannelBuffer buf = (ChannelBuffer) message;
      int length = buf.readableBytes();
      ByteBuffer nioBuf = buf.toByteBuffer();
      DatagramPacket packet;
      if (nioBuf.hasArray()) {
        // Avoid copy if the buffer is backed by an array.
        packet = new DatagramPacket(nioBuf.array(), nioBuf.arrayOffset(), length);
      } else {
        // Otherwise it will be expensive.
        byte[] arrayBuf = new byte[length];
        buf.getBytes(0, arrayBuf);
        packet = new DatagramPacket(arrayBuf, length);
      }

      if (remoteAddress != null) {
        packet.setSocketAddress(remoteAddress);
      }
      channel.socket.send(packet);
      fireWriteComplete(channel, length);
      future.setSuccess();
    } catch (Throwable t) {
      future.setFailure(t);
      fireExceptionCaught(channel, t);
    }
  }
Beispiel #16
0
    private void updateWithCharBuf() {
      final int reqSize = (int) charEncoder.maxBytesPerChar() * charBuff.position();
      if (byteBuff.capacity() < reqSize) {
        byteBuff = java.nio.ByteBuffer.allocate(2 * reqSize);
      }

      // Make ready for read
      charBuff.flip();

      final CoderResult cr = charEncoder.encode(charBuff, byteBuff, true);
      try {

        if (cr.isError()) cr.throwException();

        // Make ready for read
        byteBuff.flip();

        final byte[] byts = byteBuff.array();
        final int len = byteBuff.remaining();
        final int strt = byteBuff.arrayOffset();
        digest.update(byts, strt, len);

      } catch (final CharacterCodingException e) {
        throw new OXFException(e);
      } catch (java.nio.BufferOverflowException e) {
        throw new OXFException(e);
      } catch (java.nio.BufferUnderflowException e) {
        throw new OXFException(e);
      } finally {
        // Make ready for write
        charBuff.clear();
        byteBuff.clear();
      }
    }
Beispiel #17
0
 /**
  * Creates a new buffer which wraps the specified NIO buffer's current slice. A modification on
  * the specified buffer's content will be visible to the returned buffer.
  */
 public static ByteBuf wrappedBuffer(ByteBuffer buffer) {
   if (!buffer.hasRemaining()) {
     return EMPTY_BUFFER;
   }
   if (buffer.hasArray()) {
     return wrappedBuffer(
             buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining())
         .order(buffer.order());
   } else if (PlatformDependent.hasUnsafe()) {
     if (buffer.isReadOnly()) {
       if (buffer.isDirect()) {
         return new ReadOnlyUnsafeDirectByteBuf(ALLOC, buffer);
       } else {
         return new ReadOnlyByteBufferBuf(ALLOC, buffer);
       }
     } else {
       return new UnpooledUnsafeDirectByteBuf(ALLOC, buffer, buffer.remaining());
     }
   } else {
     if (buffer.isReadOnly()) {
       return new ReadOnlyByteBufferBuf(ALLOC, buffer);
     } else {
       return new UnpooledDirectByteBuf(ALLOC, buffer, buffer.remaining());
     }
   }
 }
    private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
      byte[] sa = src.array();
      int sp = src.arrayOffset() + src.position();
      int sl = src.arrayOffset() + src.limit();

      char[] da = dst.array();
      int dp = dst.arrayOffset() + dst.position();
      int dl = dst.arrayOffset() + dst.limit();

      CoderResult cr = CoderResult.UNDERFLOW;
      if ((dl - dp) < (sl - sp)) {
        sl = sp + (dl - dp);
        cr = CoderResult.OVERFLOW;
      }

      while (sp < sl) {
        char c = decode(sa[sp]);
        if (c == UNMAPPABLE_DECODING) {
          return withResult(CoderResult.unmappableForLength(1), src, sp, dst, dp);
        }
        da[dp++] = c;
        sp++;
      }
      return withResult(cr, src, sp, dst, dp);
    }
    private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
      char[] sa = src.array();
      int sp = src.arrayOffset() + src.position();
      int sl = src.arrayOffset() + src.limit();

      byte[] da = dst.array();
      int dp = dst.arrayOffset() + dst.position();
      int dl = dst.arrayOffset() + dst.limit();
      int len = Math.min(dl - dp, sl - sp);

      while (len-- > 0) {
        char c = sa[sp];
        int b = encode(c);
        if (b == UNMAPPABLE_ENCODING) {
          if (Character.isSurrogate(c)) {
            if (sgp == null) sgp = new Surrogate.Parser();
            if (sgp.parse(c, sa, sp, sl) < 0) {
              return withResult(sgp.error(), src, sp, dst, dp);
            }
            return withResult(sgp.unmappableResult(), src, sp, dst, dp);
          }
          return withResult(CoderResult.unmappableForLength(1), src, sp, dst, dp);
        }
        da[dp++] = (byte) b;
        sp++;
      }
      return withResult(sp < sl ? CoderResult.OVERFLOW : CoderResult.UNDERFLOW, src, sp, dst, dp);
    }
Beispiel #20
0
 @Test
 public void testReturns() {
   Slab slab = new Slab(0, SLAB_SIZE, PAGE_SIZE);
   ByteBuffer[] pages = new ByteBuffer[PAGES];
   // Take all pages
   for (int p = 0; p < PAGES; p++) {
     pages[p] = slab.takePageBuffer();
   }
   // Put them back
   for (int p = 0; p < PAGES; p++) {
     slab.returnPageBuffer(pages[p]);
   }
   // Take them again
   int[] arrayOffsets = new int[PAGES];
   for (int p = 0; p < PAGES; p++) {
     ByteBuffer buffer = slab.takePageBuffer();
     assertNotNull(buffer);
     assertEquals(0, buffer.position());
     assertEquals(PAGE_SIZE, buffer.limit());
     arrayOffsets[p] = buffer.arrayOffset();
   }
   assertNull(slab.takePageBuffer());
   Arrays.sort(arrayOffsets);
   int expectedArrayOffset = 0;
   for (int b = 0; b < PAGES; b++) {
     assertEquals(expectedArrayOffset, arrayOffsets[b]);
     expectedArrayOffset += PAGE_SIZE;
   }
 }
Beispiel #21
0
 /**
  * Returns the byte at the given offset
  *
  * @param buf the buffer to read
  * @param offset the offset at which the byte has to be read
  * @return the byte at the given offset
  */
 public static byte toByte(ByteBuffer buf, int offset) {
   if (buf.isDirect()) {
     return theUnsafe.getByte(((DirectBuffer) buf).address() + offset);
   } else {
     return theUnsafe.getByte(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset);
   }
 }
  private void performTransfer(FileChannel source, IoCallback callback) {
    if (outputStream instanceof BufferWritableOutputStream) {
      try {
        ((BufferWritableOutputStream) outputStream).transferFrom(source);
      } catch (IOException e) {
        callback.onException(exchange, this, e);
      }
    } else {
      ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
      try {
        long pos = source.position();
        long size = source.size();
        while (size - pos > 0) {
          int ret = source.read(buffer);
          if (ret <= 0) {
            break;
          }
          pos += ret;
          outputStream.write(buffer.array(), buffer.arrayOffset(), ret);
          buffer.clear();
        }

        if (pos != size) {
          throw new EOFException("Unexpected EOF reading file");
        }

      } catch (IOException e) {
        callback.onException(exchange, this, e);
      }
    }
  }
  /**
   * Writes the &quot;End of central dir record&quot;.
   *
   * @throws IOException on error
   * @throws Zip64RequiredException if the archive's size exceeds 4 GByte or there are more than
   *     65535 entries inside the archive and {@link Zip64Mode #setUseZip64} is {@link
   *     Zip64Mode#Never}.
   */
  protected void writeCentralDirectoryEnd() throws IOException {
    writeOut(EOCD_SIG);

    // disk numbers
    writeOut(ZERO);
    writeOut(ZERO);

    // number of entries
    int numberOfEntries = entries.size();
    if (numberOfEntries > ZIP64_MAGIC_SHORT && zip64Mode == Zip64Mode.Never) {
      throw new Zip64RequiredException(Zip64RequiredException.TOO_MANY_ENTRIES_MESSAGE);
    }
    if (cdOffset > ZIP64_MAGIC && zip64Mode == Zip64Mode.Never) {
      throw new Zip64RequiredException(Zip64RequiredException.ARCHIVE_TOO_BIG_MESSAGE);
    }

    byte[] num = ZipShort.getBytes(Math.min(numberOfEntries, ZIP64_MAGIC_SHORT));
    writeOut(num);
    writeOut(num);

    // length and location of CD
    writeOut(ZipLong.getBytes(Math.min(cdLength, ZIP64_MAGIC)));
    writeOut(ZipLong.getBytes(Math.min(cdOffset, ZIP64_MAGIC)));

    // ZIP file comment
    ByteBuffer data = this.zipEncoding.encode(comment);
    writeOut(ZipShort.getBytes(data.limit()));
    writeOut(data.array(), data.arrayOffset(), data.limit() - data.position());
  }
 private boolean writeBuffer(final ByteBuffer[] buffers, final IoCallback callback) {
   if (outputStream instanceof BufferWritableOutputStream) {
     // fast path, if the stream can take a buffer directly just write to it
     try {
       ((BufferWritableOutputStream) outputStream).write(buffers);
       return true;
     } catch (IOException e) {
       callback.onException(exchange, this, e);
       return false;
     }
   }
   for (ByteBuffer buffer : buffers) {
     if (buffer.hasArray()) {
       try {
         outputStream.write(buffer.array(), buffer.arrayOffset(), buffer.remaining());
       } catch (IOException e) {
         callback.onException(exchange, this, e);
         return false;
       }
     } else {
       byte[] b = new byte[BUFFER_SIZE];
       while (buffer.hasRemaining()) {
         int toRead = Math.min(buffer.remaining(), BUFFER_SIZE);
         buffer.get(b, 0, toRead);
         try {
           outputStream.write(b, 0, toRead);
         } catch (IOException e) {
           callback.onException(exchange, this, e);
           return false;
         }
       }
     }
   }
   return true;
 }
Beispiel #25
0
  private static void writeEntry(
      OutputStream os, Attributes.Name name, String value, CharsetEncoder encoder, ByteBuffer bBuf)
      throws IOException {
    byte[] out = name.getBytes();
    if (out.length > LINE_LENGTH_LIMIT) {
      throw new IOException(
          Messages.getString(
              "archive.33", name, Integer.valueOf(LINE_LENGTH_LIMIT))); // $NON-NLS-1$
    }

    os.write(out);
    os.write(VALUE_SEPARATOR);

    encoder.reset();
    bBuf.clear().limit(LINE_LENGTH_LIMIT - out.length - 2);

    CharBuffer cBuf = CharBuffer.wrap(value);
    CoderResult r;

    while (true) {
      r = encoder.encode(cBuf, bBuf, true);
      if (CoderResult.UNDERFLOW == r) {
        r = encoder.flush(bBuf);
      }
      os.write(bBuf.array(), bBuf.arrayOffset(), bBuf.position());
      os.write(LINE_SEPARATOR);
      if (CoderResult.UNDERFLOW == r) {
        break;
      }
      os.write(' ');
      bBuf.clear().limit(LINE_LENGTH_LIMIT - 1);
    }
  }
 /** {@inheritDoc} */
 @Override
 public void consume(ByteBuffer byteBuffer) throws IOException {
   outputStream.write(
       byteBuffer.array(),
       byteBuffer.arrayOffset() + byteBuffer.position(),
       byteBuffer.remaining());
 }
 protected OffheapDecodedCell(
     ByteBuffer keyBuffer,
     short rowLength,
     int familyOffset,
     byte familyLength,
     int qualOffset,
     int qualLength,
     long timeStamp,
     byte typeByte,
     ByteBuffer valueBuffer,
     int valueOffset,
     int valueLen,
     long seqId,
     ByteBuffer tagsBuffer,
     int tagsOffset,
     int tagsLength) {
   // The keyBuffer is always onheap
   assert keyBuffer.hasArray();
   assert keyBuffer.arrayOffset() == 0;
   this.keyBuffer = keyBuffer;
   this.rowLength = rowLength;
   this.familyOffset = familyOffset;
   this.familyLength = familyLength;
   this.qualifierOffset = qualOffset;
   this.qualifierLength = qualLength;
   this.timestamp = timeStamp;
   this.typeByte = typeByte;
   this.valueBuffer = valueBuffer;
   this.valueOffset = valueOffset;
   this.valueLength = valueLen;
   this.tagsBuffer = tagsBuffer;
   this.tagsOffset = tagsOffset;
   this.tagsLength = tagsLength;
   setSequenceId(seqId);
 }
Beispiel #28
0
 @Override
 public int read(ByteBuffer dst) throws IOException {
   final int read;
   if (dst.hasArray()) {
     read = mInputStream.read(dst.array(), dst.arrayOffset() + dst.position(), dst.remaining());
     if (read > 0) {
       dst.position(dst.position() + read);
     }
   } else {
     // Since we're allocating a buffer for every read, we want to choose a good size - on
     // Android, the only case where a ByteBuffer won't have a backing byte[] is if it was
     // created wrapping a void * in native code, or if it represents a memory-mapped file.
     // Especially in the latter case, we want to avoid allocating a buffer that could be
     // very large.
     final int possibleToRead =
         Math.min(Math.max(mInputStream.available(), MIN_TMP_BUFFER_SIZE), dst.remaining());
     final int reasonableToRead = Math.min(MAX_TMP_BUFFER_SIZE, possibleToRead);
     byte[] tmpBuf = new byte[reasonableToRead];
     read = mInputStream.read(tmpBuf);
     if (read > 0) {
       dst.put(tmpBuf, 0, read);
     }
   }
   return read;
 }
Beispiel #29
0
    @Override
    protected Action process() throws Exception {
      // Only return if EOF has previously been read and thus
      // a write done with EOF=true
      if (_eof) {
        if (LOG.isDebugEnabled()) LOG.debug("EOF of {}", this);
        // Handle EOF
        _in.close();
        closed();
        _channel.getByteBufferPool().release(_buffer);
        return Action.SUCCEEDED;
      }

      // Read until buffer full or EOF
      int len = 0;
      while (len < _buffer.capacity() && !_eof) {
        int r = _in.read(_buffer.array(), _buffer.arrayOffset() + len, _buffer.capacity() - len);
        if (r < 0) _eof = true;
        else len += r;
      }

      // write what we have
      _buffer.position(0);
      _buffer.limit(len);
      write(_buffer, _eof, this);
      return Action.SCHEDULED;
    }
Beispiel #30
0
 /** Helper method for implementing {@link MessageLite#hashCode()} for bytes field. */
 public static int hashCodeByteBuffer(ByteBuffer bytes) {
   if (bytes.hasArray()) {
     // Fast path.
     int h =
         LiteralByteString.hashCode(
             bytes.capacity(), bytes.array(), bytes.arrayOffset(), bytes.capacity());
     return h == 0 ? 1 : h;
   } else {
     // Read the data into a temporary byte array before calculating the
     // hash value.
     final int bufferSize =
         bytes.capacity() > DEFAULT_BUFFER_SIZE ? DEFAULT_BUFFER_SIZE : bytes.capacity();
     final byte[] buffer = new byte[bufferSize];
     final ByteBuffer duplicated = bytes.duplicate();
     duplicated.clear();
     int h = bytes.capacity();
     while (duplicated.remaining() > 0) {
       final int length =
           duplicated.remaining() <= bufferSize ? duplicated.remaining() : bufferSize;
       duplicated.get(buffer, 0, length);
       h = LiteralByteString.hashCode(h, buffer, 0, length);
     }
     return h == 0 ? 1 : h;
   }
 }