/**
   * Measure the text, stopping early if the measured width exceeds maxWidth. Return the number of
   * chars that were measured, and if measuredWidth is not null, return in it the actual width
   * measured.
   *
   * @param text The text to measure
   * @param start The offset into text to begin measuring at
   * @param end The end of the text slice to measure.
   * @param measureForwards If true, measure forwards, starting at start. Otherwise, measure
   *     backwards, starting with end.
   * @param maxWidth The maximum width to accumulate.
   * @param measuredWidth Optional. If not null, returns the actual width measured.
   * @return The number of chars that were measured. Will always be <= abs(end - start).
   */
  public int breakText(
      CharSequence text,
      int start,
      int end,
      boolean measureForwards,
      float maxWidth,
      float[] measuredWidth) {
    if (start == 0 && text instanceof String && end == text.length()) {
      return breakText((String) text, measureForwards, maxWidth, measuredWidth);
    }

    char[] buf = TemporaryBuffer.obtain(end - start);
    int result;

    TextUtils.getChars(text, start, end, buf, 0);

    if (measureForwards) {
      result = breakText(buf, 0, end - start, maxWidth, measuredWidth);
    } else {
      result = breakText(buf, 0, -(end - start), maxWidth, measuredWidth);
    }

    TemporaryBuffer.recycle(buf);
    return result;
  }
Example #2
0
 @Test
 public void testDestroyWhileOpen() throws IOException {
   @SuppressWarnings("resource" /* java 7 */)
   final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
   try {
     b.write(new TestRng(getName()).nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 2));
   } finally {
     b.destroy();
   }
 }
Example #3
0
 @Test
 public void testOneBlockAndHalf_BulkWrite() throws IOException {
   final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
   final byte[] test = new TestRng(getName()).nextBytes(TemporaryBuffer.Block.SZ * 3 / 2);
   try {
     b.write(test, 0, 2);
     b.write(test, 2, 4);
     b.write(test, 6, test.length - 6 - 2);
     b.write(test, test.length - 2, 2);
     b.close();
     assertEquals(test.length, b.length());
     {
       final byte[] r = b.toByteArray();
       assertNotNull(r);
       assertEquals(test.length, r.length);
       assertArrayEquals(test, r);
     }
     {
       final ByteArrayOutputStream o = new ByteArrayOutputStream();
       b.writeTo(o, null);
       o.close();
       final byte[] r = o.toByteArray();
       assertEquals(test.length, r.length);
       assertArrayEquals(test, r);
     }
   } finally {
     b.destroy();
   }
 }
Example #4
0
 @Test
 public void testEmpty() throws IOException {
   final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
   try {
     b.close();
     assertEquals(0, b.length());
     final byte[] r = b.toByteArray();
     assertNotNull(r);
     assertEquals(0, r.length);
   } finally {
     b.destroy();
   }
 }
Example #5
0
 @Test
 public void testHeap() throws IOException {
   @SuppressWarnings("resource" /* java 7 */)
   final TemporaryBuffer b = new TemporaryBuffer.Heap(2 * 8 * 1024);
   final byte[] r = new byte[8 * 1024];
   b.write(r);
   b.write(r);
   try {
     b.write(1);
     fail("accepted too many bytes of data");
   } catch (IOException e) {
     assertEquals("In-memory buffer limit exceeded", e.getMessage());
   }
 }
Example #6
0
 @Test
 public void testInCoreLimit_SwitchOnCopy() throws IOException {
   final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
   final byte[] test = new TestRng(getName()).nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 2);
   try {
     final ByteArrayInputStream in =
         new ByteArrayInputStream(
             test,
             TemporaryBuffer.DEFAULT_IN_CORE_LIMIT,
             test.length - TemporaryBuffer.DEFAULT_IN_CORE_LIMIT);
     b.write(test, 0, TemporaryBuffer.DEFAULT_IN_CORE_LIMIT);
     b.copy(in);
     b.close();
     assertEquals(test.length, b.length());
     {
       final byte[] r = b.toByteArray();
       assertNotNull(r);
       assertEquals(test.length, r.length);
       assertArrayEquals(test, r);
     }
     {
       final ByteArrayOutputStream o = new ByteArrayOutputStream();
       b.writeTo(o, null);
       o.close();
       final byte[] r = o.toByteArray();
       assertEquals(test.length, r.length);
       assertArrayEquals(test, r);
     }
   } finally {
     b.destroy();
   }
 }
  /**
   * Return the width of the text.
   *
   * @param text The text to measure
   * @param start The index of the first character to start measuring
   * @param end 1 beyond the index of the last character to measure
   * @return The width of the text
   */
  public float measureText(CharSequence text, int start, int end) {
    if (text instanceof String) {
      return measureText((String) text, start, end);
    }
    if (text instanceof SpannedString || text instanceof SpannableString) {
      return measureText(text.toString(), start, end);
    }
    if (text instanceof GraphicsOperations) {
      return ((GraphicsOperations) text).measureText(start, end, this);
    }

    char[] buf = TemporaryBuffer.obtain(end - start);
    TextUtils.getChars(text, start, end, buf, 0);
    float result = measureText(buf, 0, end - start);
    TemporaryBuffer.recycle(buf);
    return result;
  }
  /**
   * Return the advance widths for the characters in the string.
   *
   * @param text The text to measure
   * @param start The index of the first char to to measure
   * @param end The end of the text slice to measure
   * @param widths array to receive the advance widths of the characters. Must be at least a large
   *     as (end - start).
   * @return the actual number of widths returned.
   */
  public int getTextWidths(CharSequence text, int start, int end, float[] widths) {
    if (text instanceof String) {
      return getTextWidths((String) text, start, end, widths);
    }
    if (text instanceof SpannedString || text instanceof SpannableString) {
      return getTextWidths(text.toString(), start, end, widths);
    }
    if (text instanceof GraphicsOperations) {
      return ((GraphicsOperations) text).getTextWidths(start, end, widths, this);
    }

    char[] buf = TemporaryBuffer.obtain(end - start);
    TextUtils.getChars(text, start, end, buf, 0);
    int result = getTextWidths(buf, 0, end - start, widths);
    TemporaryBuffer.recycle(buf);
    return result;
  }
Example #9
0
 @Test
 public void testOneByte() throws IOException {
   final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
   final byte test = (byte) new TestRng(getName()).nextInt();
   try {
     b.write(test);
     b.close();
     assertEquals(1, b.length());
     {
       final byte[] r = b.toByteArray();
       assertNotNull(r);
       assertEquals(1, r.length);
       assertEquals(test, r[0]);
     }
     {
       final ByteArrayOutputStream o = new ByteArrayOutputStream();
       b.writeTo(o, null);
       o.close();
       final byte[] r = o.toByteArray();
       assertEquals(1, r.length);
       assertEquals(test, r[0]);
     }
   } finally {
     b.destroy();
   }
 }
Example #10
0
 @Test
 public void testLarge_SingleWrite() throws IOException {
   final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
   final byte[] test = new TestRng(getName()).nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 3);
   try {
     b.write(test);
     b.close();
     assertEquals(test.length, b.length());
     {
       final byte[] r = b.toByteArray();
       assertNotNull(r);
       assertEquals(test.length, r.length);
       assertArrayEquals(test, r);
     }
     {
       final ByteArrayOutputStream o = new ByteArrayOutputStream();
       b.writeTo(o, null);
       o.close();
       final byte[] r = o.toByteArray();
       assertEquals(test.length, r.length);
       assertArrayEquals(test, r);
     }
   } finally {
     b.destroy();
   }
 }
Example #11
0
  @Test
  public void testHeapWithEstimatedSize() throws IOException {
    int sz = 2 * Block.SZ;
    try (TemporaryBuffer b = new TemporaryBuffer.Heap(sz / 2, sz)) {
      for (int i = 0; i < sz; i++) {
        b.write('x');
      }
      try {
        b.write(1);
        fail("accepted too many bytes of data");
      } catch (IOException e) {
        assertEquals("In-memory buffer limit exceeded", e.getMessage());
      }

      try (InputStream in = b.openInputStream()) {
        for (int i = 0; i < sz; i++) {
          assertEquals('x', in.read());
        }
        assertEquals(-1, in.read());
      }
    }
  }
Example #12
0
  @Test
  public void testRandomWrites() throws IOException {
    final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
    final TestRng rng = new TestRng(getName());
    final int max = TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 2;
    final byte[] expect = new byte[max];
    try {
      int written = 0;
      boolean onebyte = true;
      while (written < max) {
        if (onebyte) {
          final byte v = (byte) rng.nextInt();
          b.write(v);
          expect[written++] = v;
        } else {
          final int len = Math.min(rng.nextInt() & 127, max - written);
          final byte[] tmp = rng.nextBytes(len);
          b.write(tmp, 0, len);
          System.arraycopy(tmp, 0, expect, written, len);
          written += len;
        }
        onebyte = !onebyte;
      }
      assertEquals(expect.length, written);
      b.close();

      assertEquals(expect.length, b.length());
      {
        final byte[] r = b.toByteArray();
        assertNotNull(r);
        assertEquals(expect.length, r.length);
        assertArrayEquals(expect, r);
      }
      {
        final ByteArrayOutputStream o = new ByteArrayOutputStream();
        b.writeTo(o, null);
        o.close();
        final byte[] r = o.toByteArray();
        assertEquals(expect.length, r.length);
        assertArrayEquals(expect, r);
      }
    } finally {
      b.destroy();
    }
  }