com.google.gwt.typedarrays.shared.Uint8Array Java Examples

The following examples show how to use com.google.gwt.typedarrays.shared.Uint8Array. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DjVuText.java    From djvu-html5 with GNU General Public License v2.0 6 votes vote down vote up
private static int prevChar(
  Uint8Array byteArray,
  int    pos)
{
  if(pos >= 0)
  {
    while(--pos >= 0)
    {
      if((byteArray.get(pos) & 0xc0) != 0x80)
      {
        return pos;
      }
    }
  }

  return pos;
}
 
Example #2
Source File: JsHttpProvider.java    From actor-platform with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Promise<HTTPResponse> putMethod(String url, byte[] contents) {
    return new Promise<>(resolver -> {
        JsHttpRequest request = JsHttpRequest.create();
        request.open("PUT", url);
        request.setRequestHeader("Content-Type", "application/octet-stream");
        request.setOnLoadHandler(request1 -> {
            if (request1.getReadyState() == 4) {
                if (request1.getStatus() >= 200 && request1.getStatus() < 300) {
                    resolver.result(new HTTPResponse(request1.getStatus(), null));
                } else {
                    resolver.error(new HTTPError(request1.getStatus()));
                }
            }
        });
        Uint8Array push = TypedArrays.createUint8Array(contents.length);
        for (int i = 0; i < contents.length; i++) {
            push.set(i, contents[i]);
        }
        request.send(push.buffer());
    });
}
 
Example #3
Source File: DjVuText.java    From djvu-html5 with GNU General Public License v2.0 6 votes vote down vote up
private int firstEndSpace(
  final Uint8Array byteArray,
  int          start,
  final int    length)
{
  for(int pos = start + length; --pos >= start;)
  {
    if(
      ((byteArray.get(pos) & 0xc0) != 0x80)
      && !isspace(getChar(byteArray, pos)))
    {
      return nextChar(byteArray, pos);
    }
  }

  return start;
}
 
Example #4
Source File: UrlDownloaderTest.java    From gdx-fireapp with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
    PowerMockito.mockStatic(XMLHttpRequest.class);
    PowerMockito.mockStatic(TypedArrays.class);
    xmlHttpRequest = PowerMockito.mock(XMLHttpRequest.class);
    Mockito.when(XMLHttpRequest.create()).thenReturn(xmlHttpRequest);
    Mockito.doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock invocation) {
            ((ReadyStateChangeHandler) invocation.getArgument(0)).onReadyStateChange(xmlHttpRequest);
            return null;
        }
    }).when(xmlHttpRequest).setOnReadyStateChange(Mockito.any(ReadyStateChangeHandler.class));
    Uint8Array uint8Array = Mockito.mock(Uint8Array.class);
    Mockito.when(xmlHttpRequest.getResponseArrayBuffer()).thenReturn(Mockito.mock(ArrayBuffer.class));
    Mockito.when(uint8Array.length()).thenReturn(10);
    Mockito.when(TypedArrays.createUint8Array(Mockito.any(ArrayBuffer.class))).thenReturn(uint8Array);
}
 
Example #5
Source File: DjVuText.java    From djvu-html5 with GNU General Public License v2.0 6 votes vote down vote up
private static int nextChar(
  Uint8Array byteArray,
  int    pos)
{
  if(pos < byteArray.length())
  {
    while(++pos < byteArray.length())
    {
      if((byteArray.get(pos) & 0xc0) != 0x80)
      {
        return pos;
      }
    }
  }

  return pos;
}
 
Example #6
Source File: DataStore.java    From djvu-html5 with GNU General Public License v2.0 6 votes vote down vote up
public void setTile(TileInfo tileInfo, GMap bufferGMap) {
	if (bufferImageData == null || bufferImageData.getWidth() != bufferGMap.getDataWidth()
			|| bufferImageData.getHeight() != bufferGMap.getDataHeight()) {
		bufferImageData = bufferCanvas.getContext2d()
				.createImageData(bufferGMap.getDataWidth(), bufferGMap.getDataHeight());
	}
	Uint8Array imageArray = bufferImageData.getData().cast();
	imageArray.set(bufferGMap.getImageData());
	bufferCanvas.getContext2d().putImageData(bufferImageData, -bufferGMap.getBorder(), 0);

	CanvasElement tile = tiles.get(tileInfo);
	if (tile == null) {
		tile = createImage(bufferGMap.getDataWidth() - bufferGMap.getBorder(), bufferGMap.getDataHeight());
		tiles.put(new TileInfo(tileInfo), tile);
	}
	Context2d c = tile.getContext2d();
	c.setFillStyle("white");
	c.fillRect(0, 0, tileSize, tileSize);
	c.drawImage(bufferCanvas, 0, 0);
	for (Consumer<Integer> listener : tileListeners)
		listener.accept(tileInfo.page);
}
 
Example #7
Source File: PageDecoder.java    From djvu-html5 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Uint8Array getData(String url, ReadyListener listener) {
	FileItem entry = getCachedFile(url);
	if (!entry.downloadStarted) {
		downloadFile(url);
	}
	if (entry.data == null && listener != null)
		entry.listeners.add(listener);
	filesByMRU.remove(entry);
	filesByMRU.add(0, entry);
	return entry.data;
}
 
Example #8
Source File: DjVuText.java    From djvu-html5 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean char_equal(
  byte[] first,
  int    firstPos,
  Uint8Array second,
  int    secondPos)
{    
  if(first[firstPos] != second.get(secondPos++))
  {
    return false;
  }

  if((first[firstPos++] & 0xc0) < 0x80)
  {
    return true;
  }

  // skip the first bytes (assumed to be equal)
  while(
    ((first[firstPos] & 0xc0) == 0x80)
    && ((second.get(secondPos) & 0xc0) == 0x80))
  {
    // both bytes are UTF8 continuation bytes
    if(first[firstPos++] != second.get(secondPos++))
    {
      return false;
    }
  }

  // All continuation bytes up to this position (if any) agree and 
  // at least one of the byteArrays has run out of continuation bytes.
  // The characters are equal if the current bytes are not
  // continuation bytes.
  return (((first[firstPos] & 0xc0) != 0x80)
  && ((second.get(secondPos) & 0xc0) != 0x80));
}
 
Example #9
Source File: DjVuText.java    From djvu-html5 with GNU General Public License v2.0 5 votes vote down vote up
private int nextJavaIdentifier(
  final Uint8Array byteArray,
  int          pos)
{
  while(
    (pos < byteArray.length())
    && !isJavaIdentifier(getChar(byteArray, pos)))
  {
    pos = nextChar(byteArray, pos);
  }

  return pos;
}
 
Example #10
Source File: DjVuText.java    From djvu-html5 with GNU General Public License v2.0 5 votes vote down vote up
private int skipSpaces(
  final Uint8Array byteArray,
  int          pos,
  final int    length)
{
  while((pos < length) && isspace(getChar(byteArray, pos)))
  {
    pos = nextChar(byteArray, pos);
  }

  return pos;
}
 
Example #11
Source File: GBitmap.java    From djvu-html5 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Initialize this map by copying a reference map
 *
 * @param ref map to copy
 * @param aborder number of border pixels
 *
 * @return the initialized map
 */
private GBitmap init(
  final GBitmap ref,
  final int     aborder)
{
  if(this != ref)
  {
    init(
      ref.rows(),
      ref.columns(),
      aborder);
    grays = ref.grays;

    for(int i = 0; i < nrows; i++)
    {
  	  Uint8Array refRow = ref.data.subarray(ref.rowOffset(i) * BYTES_PER_PIXEL,
  			  ref.rowOffset(i) + ncolumns * BYTES_PER_PIXEL);
  	  data.set(refRow, rowOffset(i) * BYTES_PER_PIXEL);
    }
  }
  else if(aborder > border)
  {
    setMinimumBorder(aborder);
  }

  return this;
}
 
Example #12
Source File: GPixmap.java    From djvu-html5 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Initiallize this pixmap with a preallocated buffer.
 *
 * @param data buffer to use
 * @param arows number of rows
 * @param acolumns number of columns
 *
 * @return the initialized pixmap
 */
GPixmap init(
  Uint8Array data,
  int    arows,
  int    acolumns)
{
  nrows       = arows;
  ncolumns    = acolumns;
  this.data   = data;

  return this;
}
 
Example #13
Source File: PageDecoder.java    From djvu-html5 with GNU General Public License v2.0 5 votes vote down vote up
public PageDecoder(final ProcessingContext context, final String url) {
	this.context = context;

	DjvuContext.addViewChangeListener(this::pageChanged);

	URLInputStream.dataSource = this;

	Uint8Array data = getData(url, () -> init(url));
	if (data != null)
		init(url);
}
 
Example #14
Source File: JsFileInput.java    From actor-platform with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Promise<FilePart> read(int fileOffset, int len) {
    return new Promise<>(resolver -> {
        JsFileReader fileReader = JsFileReader.create();
        fileReader.setOnLoaded(message -> {
            Uint8Array array = TypedArrays.createUint8Array(message);
            byte[] data = new byte[len];
            for (int i = 0; i < len; i++) {
                data[i] = (byte) (array.get(i));
            }
            resolver.result(new FilePart(fileOffset, len, data));
        });
        fileReader.readAsArrayBuffer(jsFile.slice(fileOffset, fileOffset + len));
    });
}
 
Example #15
Source File: Conversion.java    From actor-platform with GNU Affero General Public License v3.0 5 votes vote down vote up
public static ArrayBuffer convertBytes(byte[] data) {
    Uint8Array push = TypedArrays.createUint8Array(data.length);
    for (int i = 0; i < data.length; i++) {
        push.set(i, data[i]);
    }
    return push.buffer();
}
 
Example #16
Source File: Conversion.java    From actor-platform with GNU Affero General Public License v3.0 5 votes vote down vote up
public static byte[] convertBytes(ArrayBuffer buffer) {
    Uint8Array array = TypedArrays.createUint8Array(buffer);
    byte[] res = new byte[array.length()];
    for (int i = 0; i < res.length; i++) {
        res[i] = (byte) (array.get(i));
    }
    return res;
}
 
Example #17
Source File: WebSocketConnection.java    From actor-platform with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void doSend(byte[] data) {
    // Log.d(TAG, "doSend");
    if (isClosed) {
        return;
    }
    Uint8Array push = TypedArrays.createUint8Array(data.length);
    for (int i = 0; i < data.length; i++) {
        push.set(i, data[i]);
    }
    send(push);
}
 
Example #18
Source File: PushCryptoHelper.java    From gwt-material with Apache License 2.0 5 votes vote down vote up
public static native Uint8Array Base64ToArrayBuffer(String base64String)/*-{
    var padding = '='.repeat((4 - base64String.length % 4) % 4);
    var base64 = (base64String + padding)
        .replace(/\-/g, '+')
        .replace(/_/g, '/');

    var rawData = window.atob(base64);
    var outputArray = new Uint8Array(rawData.length);

    for (var i = 0; i < rawData.length; ++i) {
        outputArray[i] = rawData.charCodeAt(i);
    }
    return outputArray;
}-*/;
 
Example #19
Source File: URLInputStream.java    From djvu-html5 with GNU General Public License v2.0 4 votes vote down vote up
public URLInputStream init(Uint8Array data) {
	this.data = data;
	offset = 0;
	return this;
}
 
Example #20
Source File: IWPixmap.java    From djvu-html5 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 */
public GPixmap getPixmap()
{
  if(ymap == null)
  {
    return null;
  }

  final int w      = ymap.iw;
  final int h      = ymap.ih;
  final int pixsep = 4;
  final int rowsep = w * pixsep;
  Uint8Array bytes = TypedArrays.createUint8Array(h * rowsep);

  ymap.image(0, bytes, rowsep, pixsep, false);

  if((crmap != null) && (cbmap != null) && (crcb_delay >= 0))
  {
    cbmap.image(1, bytes, rowsep, pixsep, crcb_half);
    crmap.image(2, bytes, rowsep, pixsep, crcb_half);
  }

  // Convert image to RGB
  final GPixmap         ppm   =
    new GPixmap().init(bytes, h, w);
  final GPixelReference pixel = ppm.createGPixelReference(0);

  for(int i = 0; i < h;)
  {
    pixel.setOffset(i++, 0);

    if((crmap != null) && (cbmap != null) && (crcb_delay >= 0))
    {
      pixel.YCC_to_RGB(w);
    }
    else
    {
      for(int x = w; x-- > 0; pixel.incOffset())
      {
        pixel.setGray(127 - pixel.getBlue());
      }
    }
  }

  return ppm;
}
 
Example #21
Source File: IWPixmap.java    From djvu-html5 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GPixmap getPixmap(
    int     subsample,
    GRect   rect,
    GPixmap retval)
  {
    if(ymap == null)
    {
      return null;
    }

    if(retval == null)
    {
      retval = new GPixmap();
    }

    final int    w      = rect.width();
    final int    h      = rect.height();
    final int    pixsep = 4;
    final int    rowsep = w * pixsep;
    final Uint8Array bytes  = retval.init(h, w, null).data;

    ymap.image(subsample, rect, 0, bytes, rowsep, pixsep, false);

    if((crmap != null) && (cbmap != null) && (crcb_delay >= 0))
    {
      cbmap.image(subsample, rect, 1, bytes, rowsep, pixsep, crcb_half);
      crmap.image(subsample, rect, 2, bytes, rowsep, pixsep, crcb_half);
    }

    final GPixelReference pixel = retval.createGPixelReference(0);

    for(int i = 0; i < h;)
    {
      pixel.setOffset(i++, 0);

      if((crmap != null) && (cbmap != null) && (crcb_delay >= 0))
      {
        pixel.YCC_to_RGB(w);
      }
      else
      {
        for(int x = w; x-- > 0; pixel.incOffset())
        {
          pixel.setGray(127 - bytes.get(pixel.getOffset()));
        }
      }
    }

    return retval;
  }
 
Example #22
Source File: IWMap.java    From djvu-html5 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * DOCUMENT ME!
 *
 * @param index DOCUMENT ME!
 * @param img8 DOCUMENT ME!
 * @param rowsize DOCUMENT ME!
 * @param pixsep DOCUMENT ME!
 * @param fast DOCUMENT ME!
 */
void image(
  int          index,
  final Uint8Array img8,
  int          rowsize,
  int          pixsep,
  boolean          fast)
{
  final Int16Array data16    = TypedArrays.createInt16Array(bw * bh);
  final Int16Array liftblock = TypedArrays.createInt16Array(1024);
  int           pidx      = 0;
  IWBlock[]     block     = blocks;
  int           blockidx  = 0;
  int           ppidx     = 0;

  for(int i = 0; i < bh; i += 32, pidx += (32 * bw))
  {
    for(int j = 0; j < bw; j += 32)
    {
      block[blockidx].write_liftblock(liftblock, 0, 64);
      blockidx++;

      ppidx = pidx + j;

      for(int ii = 0, p1idx = 0; ii++ < 32; p1idx += 32, ppidx += bw)
      {
        Int16Array src = liftblock.subarray(p1idx, p1idx + 32);
        data16.set(src, ppidx);
      }
    }
  }

  if(fast)
  {
    backward(data16, 0, iw, ih, bw, 32, 2);
    pidx = 0;

    for(int i = 0; i < bh; i += 2, pidx += bw)
    {
      for(int jj = 0; jj < bw; jj += 2, pidx += 2)
      {
      	short s = data16.get(pidx);
      	data16.set(pidx + bw, s);
      	data16.set(pidx + bw + 1, s);
      	data16.set(pidx + 1, s);
      }
    }
  }
  else
  {
    backward(data16, 0, iw, ih, bw, 32, 1);
  }

  pidx = 0;

  for(int i = 0, rowidx = index; i++ < ih; rowidx += rowsize, pidx += bw)
  {
    for(int j = 0, pixidx = rowidx; j < iw; pixidx += pixsep)
    {
      int x = (data16.get(pidx + (j++)) + 32) >> 6;

      if(x < -128)
      {
        x = -128;
      }
      else if(x > 127)
      {
        x = 127;
      }

      img8.set(pixidx, x);
    }
  }
}
 
Example #23
Source File: GMap.java    From djvu-html5 with GNU General Public License v2.0 4 votes vote down vote up
public Uint8Array getImageData() {
  return data;
}
 
Example #24
Source File: DjVuText.java    From djvu-html5 with GNU General Public License v2.0 4 votes vote down vote up
private int startsWith(
  byte[]        substringArray,
  final int     from,
  final boolean match_case)
{
  if(substringArray.length == 0)
  {
    return from;
  }

  int end = from;
  int pos = 0;
  Uint8Array substring = convertArray(substringArray);

  for(int c0 = getChar(substring, 0); end < textByteArray.length();)
  {
    int c1 = getChar(textByteArray, end);

    if(
      (c0 != c1)
      && (match_case
      || (((c0 & 0xffff) != c0) || ((c1 & 0xffff) != c1)
      || (Character.toUpperCase((char)c0) != Character.toUpperCase(
        (char)c1)))))
    {
      return from;
    }

    pos   = nextChar(substring, pos);
    end   = nextChar(textByteArray, end);

    if(pos >= substring.length())
    {
      return end;
    }

    c0 = getChar(substring, pos);

    if(!isJavaIdentifier(c0))
    {
      c1 = getChar(textByteArray, end);

      if(isJavaIdentifier(c1))
      {
        return from;
      }

      if(isspace(c0))
      {
        pos = skipSpaces(substring, pos);

        if(pos >= substring.length())
        {
          return end;
        }

        c0 = getChar(substring, pos);

        if(isJavaIdentifier(c0))
        {
          end = nextJavaIdentifier(textByteArray, end);
        }
        else
        {
          end = skipSpaces(textByteArray, end);
        }
      }
    }
  }

  return from;
}
 
Example #25
Source File: DjVuText.java    From djvu-html5 with GNU General Public License v2.0 4 votes vote down vote up
private int skipSpaces(
  final Uint8Array byteArray,
  int          pos)
{
  return skipSpaces(byteArray, pos, byteArray.length());
}
 
Example #26
Source File: DjVuText.java    From djvu-html5 with GNU General Public License v2.0 4 votes vote down vote up
private int getChar(
  Uint8Array byteArray,
  int    pos)
{
  int value = byteArray.get(pos++);
  if (value > 0 && (value >> 7) == 0)
    return value;

  switch(value & 0xc0)
  {
    case 0x80 :
      throw new IllegalStateException("Invalid UTF8");
    case 0x40 :
      return value;
    default :
      value = (value << 6) | (byteArray.get(pos++) & 0x7f);

      if((value & 0x800) == 0)
      {
        return value & 0x7ff;
      }

      value = (value << 6) | (byteArray.get(pos++) & 0x7f);

      if((value & 0x10000) == 0)
      {
        return value & 0xffff;
      }

      value = (value << 6) | (byteArray.get(pos++) & 0x7f);

      if((value & 0x200000) == 0)
      {
        return value & 0x1fffff;
      }

      value = (value << 6) | (byteArray.get(pos++) & 0x7f);

      if((value & 0x4000000) == 0)
      {
        return value & 0x3ffffff;
      }

      return (value << 6) | (byteArray.get(pos++) & 0x7f);
  }
}
 
Example #27
Source File: DjVuText.java    From djvu-html5 with GNU General Public License v2.0 4 votes vote down vote up
private Uint8Array convertArray(byte[] bytes) {
	Uint8Array result = TypedArrays.createUint8Array(bytes.length);
	for (int i = 0; i < bytes.length; i++)
		result.set(i, bytes[i]);
	return result;
}
 
Example #28
Source File: WebSocketConnection.java    From actor-platform with GNU Affero General Public License v3.0 4 votes vote down vote up
public native void send(Uint8Array message) /*-{
    if (message == null)
        return;

    [email protected]::jsWebSocket.send(message);
}-*/;
 
Example #29
Source File: DataSource.java    From djvu-html5 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * @return full contents of requested resource or {@code null} if it's not
 *         currently available. In the latter case, if a
 *         {@code readyListener} is given, it will be notified when when the
 *         resource becomes available.
 */
Uint8Array getData(String url, ReadyListener readyListener);