Package java.nio

Examples of java.nio.CharBuffer


    byte[] binary = new byte[]
      { (byte)0x23, (byte)0x98, (byte)0x13, (byte)0xE4, (byte)0x76, (byte)0x41,
        (byte)0xB2, (byte)0xC9, (byte)0x7F, (byte)0x0A, (byte)0xA6, (byte)0xD8 };

    ByteBuffer binaryBuf = ByteBuffer.wrap(binary);
    CharBuffer encoded = IndexableBinaryStringTools.encode(binaryBuf);
    ByteBuffer decoded = IndexableBinaryStringTools.decode(encoded);
    assertEquals("Round trip decode/decode returned different results:"
                 + System.getProperty("line.separator")
                 + "original: " + binaryDump(binaryBuf)
                 + System.getProperty("line.separator")
View Full Code Here


  public void testEncodedSortability() {
    Random random = newRandom();
    byte[] originalArray1 = new byte[MAX_RANDOM_BINARY_LENGTH];
    ByteBuffer originalBuf1 = ByteBuffer.wrap(originalArray1);
    char[] originalString1 = new char[MAX_RANDOM_BINARY_LENGTH];
    CharBuffer originalStringBuf1 = CharBuffer.wrap(originalString1);
    char[] encoded1 = new char[IndexableBinaryStringTools.getEncodedLength(originalBuf1)];
    CharBuffer encodedBuf1 = CharBuffer.wrap(encoded1);
    byte[] original2 = new byte[MAX_RANDOM_BINARY_LENGTH];
    ByteBuffer originalBuf2 = ByteBuffer.wrap(original2);
    char[] originalString2 = new char[MAX_RANDOM_BINARY_LENGTH];
    CharBuffer originalStringBuf2 = CharBuffer.wrap(originalString2);
    char[] encoded2 = new char[IndexableBinaryStringTools.getEncodedLength(originalBuf2)];
    CharBuffer encodedBuf2 = CharBuffer.wrap(encoded2);
    for (int testNum = 0 ; testNum < NUM_RANDOM_TESTS ; ++testNum) {
      int numBytes1 = random.nextInt(MAX_RANDOM_BINARY_LENGTH - 1) + 1; // Min == 1
      originalBuf1.limit(numBytes1);
      originalStringBuf1.limit(numBytes1);
     
View Full Code Here

    }
  }
 
  public void testEmptyInput() {
    byte[] binary = new byte[0];
    CharBuffer encoded = IndexableBinaryStringTools.encode(ByteBuffer.wrap(binary));
    ByteBuffer decoded = IndexableBinaryStringTools.decode(encoded);
    assertNotNull("decode() returned null", decoded);
    assertEquals("decoded empty input was not empty", decoded.limit(), 0);
  }
View Full Code Here

  }
 
  public void testAllNullInput() {
    byte[] binary = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    ByteBuffer binaryBuf = ByteBuffer.wrap(binary);
    CharBuffer encoded = IndexableBinaryStringTools.encode(binaryBuf);
    assertNotNull("encode() returned null", encoded);
    ByteBuffer decodedBuf = IndexableBinaryStringTools.decode(encoded);
    assertNotNull("decode() returned null", decodedBuf);
    assertEquals("Round trip decode/decode returned different results:"
                 + System.getProperty("line.separator")
View Full Code Here

  public void testRandomBinaryRoundTrip() {
    Random random = newRandom();
    byte[] binary = new byte[MAX_RANDOM_BINARY_LENGTH];
    ByteBuffer binaryBuf = ByteBuffer.wrap(binary);
    char[] encoded = new char[IndexableBinaryStringTools.getEncodedLength(binaryBuf)];
    CharBuffer encodedBuf = CharBuffer.wrap(encoded);
    byte[] decoded = new byte[MAX_RANDOM_BINARY_LENGTH];
    ByteBuffer decodedBuf = ByteBuffer.wrap(decoded);
    for (int testNum = 0 ; testNum < NUM_RANDOM_TESTS ; ++testNum) {
      int numBytes = random.nextInt(MAX_RANDOM_BINARY_LENGTH - 1) + 1 ; // Min == 1
      binaryBuf.limit(numBytes);
View Full Code Here

    ByteBuffer begBuf = ByteBuffer.wrap(keyBits);
    // Ensure that the backing char[] array is large enough to hold the encoded
    // Binary String
    char[] encodedBegArray
      = new char[IndexableBinaryStringTools.getEncodedLength(begBuf)];
    CharBuffer encodedBegBuf = CharBuffer.wrap(encodedBegArray);
    IndexableBinaryStringTools.encode(begBuf, encodedBegBuf);
    return new String(encodedBegArray);
  }
View Full Code Here

        int lineCount = 0;
        try {
            RandomAccessFile raf = new RandomAccessFile(file, "r");
            FileChannel fc = raf.getChannel();
            MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
            CharBuffer cb = Charset.forName(System.getProperty("file.encoding")).decode(bb);
            Matcher target = null;
            Matcher any = null;
            Matcher lines = FULL_LINE_PATTERN.matcher(cb);
            Matcher text = textSearch == null ? null : textSearch.matcher("");
            boolean hit = false;
            max = Math.min(max, MAX_SEARCH_RESULTS);
            while(lines.find()) {
                ++lineCount;
                if(target == null) {
                    if(DEFAULT_ANY_START.matcher(cb.subSequence(lines.start(), lines.end())).find()) {
                        target = getDefaultPatternForLevel(targetLevel).matcher("");
                        any = DEFAULT_ANY_START.matcher("");
                    } else {
                        target = getUnknownPatternForLevel(targetLevel).matcher("");
                        any = UNKNOWN_ANY_START.matcher("");
                    }
                }
                if(start != null && start.intValue() > lineCount) {
                    continue;
                }
                if(stop != null && stop.intValue() < lineCount) {
                    continue;
                }
                CharSequence line = cb.subSequence(lines.start(), lines.end());
                target.reset(line);
                if(target.find()) {
                    if(text != null) {
                        text.reset(line);
                        if(!text.find()) {
View Full Code Here

                        ByteBuffer buf = ctx.getBuffer();
                        buf.flip();
                        buf.limit(buf.limit() - matchCount);

                        CharsetDecoder decoder = ctx.getDecoder();
                        CharBuffer buffer = decoder.decode(buf);
                        decoded = new String(buffer.array());
                    } else {
                        int overflowPosition = ctx.getOverflowLength();
                        throw new IllegalStateException("Line is too long: " + overflowPosition);
                    }
                } catch (CharacterCodingException cce) {
View Full Code Here

                            ByteBuffer buf = ctx.getBuffer();
                            buf.flip();
                            buf.limit(buf.limit() - matchCount);

                            CharsetDecoder decoder = ctx.getDecoder();
                            CharBuffer buffer = decoder.decode(buf);
                            decoded = new String(buffer.array());
                        } else {
                            int overflowLength = ctx.getOverflowLength();
                            throw new IllegalStateException("Line is too long: " + overflowLength);
                        }
                    } catch (CharacterCodingException cce) {
View Full Code Here

                        // It's in the range, so process the file
                        RandomAccessFile raf = new RandomAccessFile(logFiles[i], "r");
                        FileChannel fc = raf.getChannel();
                        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
                        CharBuffer cb = Charset.forName("US-ASCII").decode(bb); //todo: does Jetty use a different charset on a foreign PC?
                        Matcher lines = FULL_LINE_PATTERN.matcher(cb);
                        Matcher target = ACCESS_LOG_PATTERN.matcher("");
                        SimpleDateFormat format = (start == 0 && end == 0) ? null : new SimpleDateFormat(ACCESS_LOG_DATE_FORMAT);
                        int max = maxResults == null ? MAX_SEARCH_RESULTS : Math.min(maxResults.intValue(), MAX_SEARCH_RESULTS);

                        while(lines.find()) {
                            ++lineCount;
                            ++fileCount;
                            if(capped) {
                                continue;
                            }
                            CharSequence line = cb.subSequence(lines.start(), lines.end());
                            target.reset(line);
                            if(target.find()) {
                                if(host != null && !host.equals(target.group(GROUP_HOST))) {
                                    continue;
                                }
View Full Code Here

TOP

Related Classes of java.nio.CharBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.