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;
 }
Example #2
0
  // Check if the datagramsocket adaptor can send with a packet
  // that has not been initialized with an address; the legacy
  // datagram socket will send in this case
  private static void test2() throws Exception {
    DatagramChannel sndChannel = DatagramChannel.open();
    sndChannel.socket().bind(null);
    InetSocketAddress sender =
        new InetSocketAddress(InetAddress.getLocalHost(), sndChannel.socket().getLocalPort());

    DatagramChannel rcvChannel = DatagramChannel.open();
    rcvChannel.socket().bind(null);
    InetSocketAddress receiver =
        new InetSocketAddress(InetAddress.getLocalHost(), rcvChannel.socket().getLocalPort());

    rcvChannel.connect(sender);
    sndChannel.connect(receiver);

    byte b[] = "hello".getBytes("UTF-8");
    DatagramPacket pkt = new DatagramPacket(b, b.length);
    sndChannel.socket().send(pkt);

    ByteBuffer bb = ByteBuffer.allocate(256);
    rcvChannel.receive(bb);
    bb.flip();
    CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
    if (!cb.toString().startsWith("h")) throw new RuntimeException("Test failed");

    // Check that the pkt got set with the target address;
    // This is legacy behavior
    if (!pkt.getSocketAddress().equals(receiver)) throw new RuntimeException("Test failed");

    rcvChannel.close();
    sndChannel.close();
  }
Example #3
0
  // Check if DatagramChannel.send while connected can include
  // address without throwing
  private static void test1() throws Exception {

    DatagramChannel sndChannel = DatagramChannel.open();
    sndChannel.socket().bind(null);
    InetSocketAddress sender =
        new InetSocketAddress(InetAddress.getLocalHost(), sndChannel.socket().getLocalPort());

    DatagramChannel rcvChannel = DatagramChannel.open();
    rcvChannel.socket().bind(null);
    InetSocketAddress receiver =
        new InetSocketAddress(InetAddress.getLocalHost(), rcvChannel.socket().getLocalPort());

    rcvChannel.connect(sender);
    sndChannel.connect(receiver);

    ByteBuffer bb = ByteBuffer.allocate(256);
    bb.put("hello".getBytes());
    bb.flip();
    int sent = sndChannel.send(bb, receiver);
    bb.clear();
    rcvChannel.receive(bb);
    bb.flip();
    CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
    if (!cb.toString().startsWith("h")) throw new RuntimeException("Test failed");

    rcvChannel.close();
    sndChannel.close();
  }
 public static void main(String[] arguments) {
   try {
     // read byte data into a byte buffer
     String data = "friends.dat";
     FileInputStream inData = new FileInputStream(data);
     FileChannel inChannel = inData.getChannel();
     long inSize = inChannel.size();
     ByteBuffer source = ByteBuffer.allocate((int) inSize);
     inChannel.read(source, 0);
     source.position(0);
     System.out.println("Original byte data:");
     for (int i = 0; source.remaining() > 0; i++) {
       System.out.print(source.get() + " ");
     }
     // convert byte data into character data
     source.position(0);
     Charset ascii = Charset.forName("US-ASCII");
     CharsetDecoder toAscii = ascii.newDecoder();
     CharBuffer destination = toAscii.decode(source);
     destination.position(0);
     System.out.println("\n\nNew character data:");
     for (int i = 0; destination.remaining() > 0; i++) {
       System.out.print(destination.get());
     }
     System.out.println();
   } catch (FileNotFoundException fne) {
     System.out.println(fne.getMessage());
   } catch (IOException ioe) {
     System.out.println(ioe.getMessage());
   }
 }
 protected PlaceNameChunk createPlaceNameChunk(PlaceNameService service) {
   int numChars = this.textArray.length();
   CharBuffer textBuffer = newCharBuffer(numChars);
   textBuffer.put(this.textArray.toString());
   textBuffer.rewind();
   return new PlaceNameChunk(
       service, textBuffer, this.textIndexArray, this.latlonArray, this.numEntries);
 }
Example #6
0
  /**
   * Gets a Reader for a text flavor, decoded, if necessary, for the expected charset (encoding).
   * The supported representation classes are <code>java.io.Reader</code>, <code>java.lang.String
   * </code>, <code>java.nio.CharBuffer</code>, <code>[C</code>, <code>java.io.InputStream</code>,
   * <code>java.nio.ByteBuffer</code>, and <code>[B</code>.
   *
   * <p>Because text flavors which do not support the charset parameter are encoded in a
   * non-standard format, this method should not be called for such flavors. However, in order to
   * maintain backward-compatibility, if this method is called for such a flavor, this method will
   * treat the flavor as though it supports the charset parameter and attempt to decode it
   * accordingly. See <code>selectBestTextFlavor</code> for a list of text flavors which do not
   * support the charset parameter.
   *
   * @param transferable the <code>Transferable</code> whose data will be requested in this flavor
   * @return a <code>Reader</code> to read the <code>Transferable</code>'s data
   * @exception IllegalArgumentException if the representation class is not one of the seven listed
   *     above
   * @exception IllegalArgumentException if the <code>Transferable</code> has <code>null</code> data
   * @exception NullPointerException if the <code>Transferable</code> is <code>null</code>
   * @exception UnsupportedEncodingException if this flavor's representation is <code>
   *     java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>, or <code>[B</code> and this
   *     flavor's encoding is not supported by this implementation of the Java platform
   * @exception UnsupportedFlavorException if the <code>Transferable</code> does not support this
   *     flavor
   * @exception IOException if the data cannot be read because of an I/O error
   * @see #selectBestTextFlavor
   * @since 1.3
   */
  public Reader getReaderForText(Transferable transferable)
      throws UnsupportedFlavorException, IOException {
    Object transferObject = transferable.getTransferData(this);
    if (transferObject == null) {
      throw new IllegalArgumentException("getTransferData() returned null");
    }

    if (transferObject instanceof Reader) {
      return (Reader) transferObject;
    } else if (transferObject instanceof String) {
      return new StringReader((String) transferObject);
    } else if (transferObject instanceof CharBuffer) {
      CharBuffer buffer = (CharBuffer) transferObject;
      int size = buffer.remaining();
      char[] chars = new char[size];
      buffer.get(chars, 0, size);
      return new CharArrayReader(chars);
    } else if (transferObject instanceof char[]) {
      return new CharArrayReader((char[]) transferObject);
    }

    InputStream stream = null;

    if (transferObject instanceof InputStream) {
      stream = (InputStream) transferObject;
    } else if (transferObject instanceof ByteBuffer) {
      ByteBuffer buffer = (ByteBuffer) transferObject;
      int size = buffer.remaining();
      byte[] bytes = new byte[size];
      buffer.get(bytes, 0, size);
      stream = new ByteArrayInputStream(bytes);
    } else if (transferObject instanceof byte[]) {
      stream = new ByteArrayInputStream((byte[]) transferObject);
    }

    if (stream == null) {
      throw new IllegalArgumentException(
          "transfer data is not Reader, String, CharBuffer, char array, InputStream, ByteBuffer, or byte array");
    }

    String encoding = getParameter("charset");
    return (encoding == null)
        ? new InputStreamReader(stream)
        : new InputStreamReader(stream, encoding);
  }
Example #7
0
 @Override
 int read(final TextInput ti) throws IOException {
   int c = -1;
   while (++c < 4) {
     final int ch = ti.readByte();
     if (ch < 0) break;
     cache[c] = (byte) ch;
     outc.position(0);
     inc.position(0);
     inc.limit(c + 1);
     csd.reset();
     final CoderResult cr = csd.decode(inc, outc, true);
     if (cr.isMalformed()) continue;
     // return character
     int i = 0;
     final int os = outc.position();
     for (int o = 0; o < os; ++o) i |= outc.get(o) << (o << 3);
     return i;
   }
   return c == 0 ? -1 : invalid();
 }
  /**
   * Writes any remaining output to the output buffer and resets the converter to its initial state.
   *
   * @param output char array to receive flushed output.
   * @param outStart start writing to output array at this offset.
   * @param outEnd stop writing to output array at this offset (exclusive).
   * @exception MalformedInputException if the output to be flushed contained a partial or invalid
   *     multibyte character sequence. flush will write what it can to the output buffer and reset
   *     the converter before throwing this exception. An additional call to flush is not required.
   * @exception ConversionBufferFullException if output array is filled before all the output can be
   *     flushed. flush will write what it can to the output buffer and remember its state. An
   *     additional call to flush with a new output buffer will conclude the operation.
   */
  public int flush(char[] output, int outStart, int outEnd)
      throws MalformedInputException, ConversionBufferFullException {

    byteOff = charOff = 0;
    if (outStart >= outEnd || outStart >= output.length) throw new ConversionBufferFullException();
    if (dst != null && dst.array() == output) dst.position(outStart).limit(outEnd);
    else dst = CharBuffer.wrap(output, outStart, outEnd - outStart);

    CoderResult cr = null;
    try {
      if (src != null) cr = decoder.decode((ByteBuffer) src.clear(), dst, true);
      assert !cr.isUnmappable();
      if (cr.isMalformed()) {
        badInputLength = cr.length();
        reset();
        throw new MalformedInputException();
      }
    } catch (IllegalStateException ise) {
      if (src != null) cr = decoder.reset().decode(src, dst, true);
    }
    try {
      cr = decoder.flush(dst);
    } catch (Exception e) {
      assert false;
    } finally {
      byteOff = 0;
      charOff = dst.position();
      src = null;
    }
    if (cr.isOverflow()) throw new ConversionBufferFullException();

    // Return the length written to the output buffer
    if (cr.isUnderflow()) {
      int written = charOff - outStart;
      reset();
      return written;
    }
    assert false;
    return -1; // should be never reached
  }
  /**
   * Converts an array of bytes containing characters in an external encoding into an array of
   * Unicode characters. This method allows a buffer by buffer conversion of a data stream. The
   * state of the conversion is saved between calls to convert. Among other things, this means
   * multibyte input sequences can be split between calls. If a call to convert results in an
   * exception, the conversion may be continued by calling convert again with suitably modified
   * parameters. All conversions should be finished with a call to the flush method.
   *
   * @return the number of bytes written to output.
   * @param input byte array containing text to be converted.
   * @param inStart begin conversion at this offset in input array.
   * @param inEnd stop conversion at this offset in input array (exclusive).
   * @param output character array to receive conversion result.
   * @param outStart start writing to output array at this offset.
   * @param outEnd stop writing to output array at this offset (exclusive).
   * @exception MalformedInputException if the input buffer contains any sequence of bytes that is
   *     illegal for the input character set.
   * @exception UnknownCharacterException for any character that that cannot be converted to
   *     Unicode. Thrown only when converter is not in substitution mode.
   * @exception ConversionBufferFullException if output array is filled prior to converting all the
   *     input.
   */
  public int convert(byte[] input, int inStart, int inEnd, char[] output, int outStart, int outEnd)
      throws UnknownCharacterException, MalformedInputException, ConversionBufferFullException {

    byteOff = inStart;
    charOff = outStart;
    // throw exceptions compatible to legacy ByteToCharXxx converters
    if (inStart >= inEnd) return 0;
    if (inStart >= input.length) throw new ArrayIndexOutOfBoundsException(inStart);
    if (outStart >= outEnd || outStart >= output.length) throw new ConversionBufferFullException();

    if (src != null && src.array() == input) src.position(inStart).limit(inEnd);
    else src = ByteBuffer.wrap(input, inStart, inEnd - inStart);
    if (dst != null && dst.array() == output) dst.position(outStart).limit(outEnd);
    else dst = CharBuffer.wrap(output, outStart, outEnd - outStart);

    CoderResult cr;
    try {
      cr = decoder.decode(src, dst, false);
    } catch (IllegalStateException ise) {
      cr = decoder.reset().decode(src, dst, false);
    } finally {
      byteOff = src.position();
      charOff = dst.position();
    }
    if (cr.isUnmappable()) {
      badInputLength = cr.length();
      throw new UnknownCharacterException();
    }
    if (cr.isMalformed()) {
      badInputLength = cr.length();
      throw new MalformedInputException();
    }
    if (cr.isOverflow()) throw new ConversionBufferFullException();

    // Return the length written to the output buffer
    if (cr.isUnderflow()) return charOff - outStart;
    return -1; // should be never reached
  }
Example #10
0
  /**
   * Check if the first X characters of a byte stream match a String.
   *
   * @param data The byte array to process
   * @param pattern The String to match
   * @return True if the pattern was found, false otherwise
   */
  private static boolean bytesEqualsString(byte[] data, String pattern) {
    byte[] bytes = new byte[pattern.length()];
    Charset csets = Charset.forName("US-ASCII");
    boolean fin = false;
    int currChar = 0;

    // remove any CR and/or LF characters at the beginning of the article
    // data
    while (!fin) {
      if (currChar >= data.length) break;

      byte in = data[currChar];
      ByteBuffer bb = ByteBuffer.wrap(new byte[] {(byte) in});
      CharBuffer cb = csets.decode(bb);
      char c = cb.charAt(0);

      if (data.length > 0 && (c == '\n' || c == '\r')) currChar++;
      else fin = true;

      if (data.length == 0) fin = true;
    }

    // extract bytes (chars) to check from article data
    for (int i = 0; i < bytes.length && i < data.length; i++, currChar++) {
      byte in = data[currChar];
      bytes[i] = (byte) in;
    }

    // decode byte data to characters
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    CharBuffer cb = csets.decode(bb);

    // compare these characters to the pattern String
    for (int i = 0; i < pattern.length(); i++) if (cb.charAt(i) != pattern.charAt(i)) return false;

    return true;
  }
Example #11
0
  /** Generic Decoder. */
  private static final class Generic extends TextDecoder {
    /** Input cache. */
    private final byte[] cache = new byte[4];
    /** Input buffer. */
    private final ByteBuffer inc = ByteBuffer.wrap(cache);
    /** Output buffer. */
    private final CharBuffer outc = CharBuffer.wrap(new char[4]);
    /** Charset decoder. */
    private final CharsetDecoder csd;

    /**
     * Constructor.
     *
     * @param enc encoding
     * @throws IOException I/O exception
     */
    private Generic(final String enc) throws IOException {
      try {
        csd = Charset.forName(enc).newDecoder();
      } catch (final Exception ex) {
        throw new EncodingException(ex);
      }
    }

    @Override
    int read(final TextInput ti) throws IOException {
      int c = -1;
      while (++c < 4) {
        final int ch = ti.readByte();
        if (ch < 0) break;
        cache[c] = (byte) ch;
        outc.position(0);
        inc.position(0);
        inc.limit(c + 1);
        csd.reset();
        final CoderResult cr = csd.decode(inc, outc, true);
        if (cr.isMalformed()) continue;
        // return character
        int i = 0;
        final int os = outc.position();
        for (int o = 0; o < os; ++o) i |= outc.get(o) << (o << 3);
        return i;
      }
      return c == 0 ? -1 : invalid();
    }
  }
Example #12
0
 public static void write(CharBuffer cb, char ch) {
   if (ch < 0x80) {
     cb.append((char) ch);
   } else if (ch < 0x800) {
     cb.append((char) (0xc0 + (ch >> 6)));
     cb.append((char) (0x80 + (ch & 0x3f)));
   } else {
     cb.append((char) (0xe0 + (ch >> 12)));
     cb.append((char) (0x80 + ((ch >> 6) & 0x3f)));
     cb.append((char) (0x80 + (ch & 0x3f)));
   }
 }
  @Test
  public <R extends Readable> void test8_excludeJREClassesFromMockingForSafety() throws Exception {
    new MockUp<R>() {
      @Mock
      int read(CharBuffer cb) {
        return 123;
      }
    };

    CharBuffer buf = CharBuffer.allocate(10);
    int r1 =
        new Readable() {
          @Override
          public int read(CharBuffer cb) {
            return 1;
          }
        }.read(buf);
    assertEquals(123, r1);

    int r2 = new StringReader("test").read(buf);
    assertEquals(4, r2);
  }
 protected CharSequence getText(int index) {
   int beginIndex = textIndexArray[index];
   int endIndex = (index + 1 < numEntries) ? textIndexArray[index + 1] : textArray.length();
   return this.textArray.subSequence(beginIndex, endIndex);
 }