Java Code Examples for com.sun.jna.Memory

12 篇文章 0 订阅

The following code examples are extracted from open source projects. You can click  to vote up the examples you like. Your votes will be used in an intelligent system to get more and better code examples.

Code Example 1:

  10 
vote

From project JavaFTD2XX, under directory /src/com/ftdi/.

Source FTDevice.java

private static FTDevice getXthDevice(int Xth) throws FTD2XXException {
  IntByReference flag=new IntByReference();
  IntByReference devType=new IntByReference();
  IntByReference devID=new IntByReference();
  IntByReference locID=new IntByReference();
  IntByReference ftHandle=new IntByReference();
  Memory devSerNum=new Memory(16);
  Memory devDesc=new Memory(64);
  ensureFTStatus(ftd2xx.FT_GetDeviceInfoDetail(Xth,flag,devType,devID,locID,devSerNum,devDesc,ftHandle));
  return new FTDevice(DeviceType.values()[devType.getValue()],devID.getValue(),locID.getValue(),devSerNum.getString(0),devDesc.getString(0),ftHandle.getValue(),flag.getValue());
}
 

Code Example 2:

  8 
vote

From project JavaFTD2XX, under directory /src/com/ftdi/.

Source FTDevice.java

/** 
 * Open connection with device.
 * @throws FTD2XXException If something goes wrong.
 */
public void open() throws FTD2XXException {
  Memory memory=new Memory(16);
  memory.setString(0,devSerialNumber);
  IntByReference handle=new IntByReference();
  ensureFTStatus(ftd2xx.FT_OpenEx(memory,FTD2XX.FT_OPEN_BY_SERIAL_NUMBER,handle));
  this.ftHandle=handle.getValue();
}
 

Code Example 3:

  8 
vote

From project JavaFTD2XX, under directory /src/com/ftdi/.

Source FTDevice.java

/** 
 * Read all contents of the EEPROM user area as String
 * @return User EEPROM content as String
 * @throws FTD2XXException If something goes wrong.
 */
public String readFullEEPROMUserAreaAsString() throws IOException {
  IntByReference actually=new IntByReference();
  int numberOfBytes=getEEPROMUserAreaSize();
  Memory dest=new Memory(numberOfBytes);
  ensureFTStatus(ftd2xx.FT_EE_UARead(ftHandle,dest,numberOfBytes,actually));
  return dest.getString(0);
}
 

Code Example 4:

  8 
vote

From project JavaFTD2XX, under directory /src/com/ftdi/.

Source FTDevice.java

/** 
 * Write bytes to device.
 * @param bytes Byte array to send
 * @param offset Start index
 * @param length Amount of bytes to write
 * @return Number of bytes actually written
 * @throws FTD2XXException If something goes wrong.
 */
public int write(byte[] bytes,int offset,int length) throws FTD2XXException {
  Memory memory=new Memory(length);
  memory.write(0,bytes,offset,length);
  IntByReference wrote=new IntByReference();
  ensureFTStatus(ftd2xx.FT_Write(ftHandle,memory,length,wrote));
  return wrote.getValue();
}
 

Code Example 5:

  8 
vote

From project JavaFTD2XX, under directory /src/com/ftdi/.

Source FTDevice.java

/** 
 * Read bytes from device.
 * @param bytes Bytes array to store read bytes
 * @param offset Start index.
 * @param lenght Amount of bytes to read
 * @return Number of bytes actually read
 * @throws FTD2XXException If something goes wrong.
 */
public int read(byte[] bytes,int offset,int lenght) throws FTD2XXException {
  Memory memory=new Memory(lenght);
  IntByReference read=new IntByReference();
  ensureFTStatus(ftd2xx.FT_Read(ftHandle,memory,lenght,read));
  memory.read(0,bytes,offset,lenght);
  return read.getValue();
}
 

Code Example 6:

  8 
vote

From project jna, under directory /contrib/platform/src/com/sun/jna/platform/win32/.

Source NTSecApi.java

/** 
 * String representation of the buffer.
 * @return Unicode string.
 */
public String getString(){
  byte[] data=Buffer.getByteArray(0,Length);
  if (data.length < 2 || data[data.length - 1] != 0) {
    Memory newdata=new Memory(data.length + 2);
    newdata.write(0,data,0,data.length);
    return newdata.getString(0,true);
  }
  return Buffer.getString(0,true);
}
 

Code Example 7:

  7 
vote

From project akuma, under directory /src/main/java/com/sun/akuma/.

Source Daemon.java

private static String resolveSymlink(File link) throws IOException {
  String filename=link.getAbsolutePath();
  for (int sz=512; sz < 65536; sz*=2) {
    Memory m=new Memory(sz);
    int r=LIBC.readlink(filename,m,new NativeLong(sz));
    if (r < 0) {
      int err=Native.getLastError();
      if (err == 22)       return null;
      throw new IOException("Failed to readlink " + link + " error="+ err+ " "+ LIBC.strerror(err));
    }
    if (r == sz)     continue;
    byte[] buf=new byte[r];
    m.read(0,buf,0,r);
    return new String(buf);
  }
  throw new IOException("Failed to readlink " + link);
}
 

Code Example 8:

  7 
vote

From project akuma, under directory /src/main/java/com/sun/akuma/.

Source JavaVMArguments.java

private static String readLine(FILE as,long p,String prefix) throws IOException {
  if (LOGGER.isLoggable(FINEST))   LOGGER.finest(String.format("Reading %s at %X",prefix,p));
  seek64(as,p);
  Memory m=new Memory(1);
  ByteArrayOutputStream buf=new ByteArrayOutputStream();
  int i=0;
  while (true) {
    if (LIBC.fread(m,1,1,as) == 0)     break;
    byte b=m.getByte(0);
    if (b == 0)     break;
    if ((++i) % 100 == 0 && LOGGER.isLoggable(FINEST))     LOGGER.finest(prefix + " is so far " + buf.toString());
    buf.write(b);
  }
  String line=buf.toString();
  if (LOGGER.isLoggable(FINEST))   LOGGER.finest(prefix + " was " + line);
  return line;
}
 

Code Example 9:

  7 
vote

From project Eclipse, under directory /com.mobilesorcery.sdk.core/src/com/mobilesorcery/sdk/internal/.

Source OSPipeInputStream.java

/** 
 * Assumes an open pipe
 */
public int read(byte[] buffer,int offset,int length) throws IOException {
  if (length <= 0) {
    return 0;
  }
  Memory memory=new Memory(length);
  int read=CoreMoSyncPlugin.getDefault().getProcessUtil().pipe_read(fd,memory,length);
  if (read < 0) {
    throw new IOException(MessageFormat.format("Could not read from OS pipe [{0}]",fd));
  }
  if (read == 0) {
    return -1;
  }
  System.arraycopy(memory.getByteArray(0,length),0,buffer,offset,length);
  return read;
}
 

Code Example 10:

  7 
vote

From project Eclipse, under directory/com.mobilesorcery.sdk.ui.targetphone/src/com/mobilesorcery/sdk/ui/targetphone/internal/bt/.

Source SearchBTDeviceDialog.java

public BTTargetPhone open() throws IOException {
  int size=ADDRESS_SIZE + NAME_LENGTH * SIZEOF_SHORT;
  Memory deviceInfo=new Memory(size);
  int result=BTDIALOG.BTD_ERROR;
  try {
    result=BTDIALOG.INSTANCE.btDialog(deviceInfo);
  }
 catch (  Throwable e) {
    throw new IOException(e);
  }
switch (result) {
case BTDIALOG.BTD_OK:
    byte[] addr=deviceInfo.getByteArray(0,ADDRESS_SIZE);
  char[] name=deviceInfo.getCharArray(ADDRESS_SIZE,NAME_LENGTH);
return new BTTargetPhone(name,addr,BTTargetPhone.PORT_UNASSIGNED);
case BTDIALOG.BTD_ERROR:
throw new IOException("General bluetooth dialog error");
default :
return null;
}
}
 

Code Example 11:

  7 
vote

From project hudson, under directory /hudson-core/src/main/java/hudson/util/.

Source ProcessTree.java

Darwin(){
  try {
    IntByReference _=new IntByReference(sizeOfInt);
    IntByReference size=new IntByReference(sizeOfInt);
    Memory m;
    int nRetry=0;
    while (true) {
      if (LIBC.sysctl(MIB_PROC_ALL,3,NULL,size,NULL,_) != 0)       throw new IOException("Failed to obtain memory requirement: " + LIBC.strerror(Native.getLastError()));
      m=new Memory(size.getValue());
      if (LIBC.sysctl(MIB_PROC_ALL,3,m,size,NULL,_) != 0) {
        if (Native.getLastError() == ENOMEM && nRetry++ < 16)         continue;
        throw new IOException("Failed to call kern.proc.all: " + LIBC.strerror(Native.getLastError()));
      }
      break;
    }
    int count=size.getValue() / sizeOf_kinfo_proc;
    LOGGER.fine("Found " + count + " processes");
    for (int base=0; base < size.getValue(); base+=sizeOf_kinfo_proc) {
      int pid=m.getInt(base + 24);
      int ppid=m.getInt(base + 416);
      super.processes.put(pid,new DarwinProcess(pid,ppid));
    }
  }
 catch (  IOException e) {
    LOGGER.log(Level.WARNING,"Failed to obtain process list",e);
  }
}
 

Code Example 12:

  7 
vote

From project hudson, under directory /hudson-core/src/main/java/hudson/.

Source Util.java

/** 
 * Resolves symlink, if the given file is a symlink. Otherwise return null. <p> If the resolution fails, report an error.
 * @param listener If we rely on an external command to resolve symlink, this is it. (TODO: try readlink(1) available on some platforms)
 */
public static String resolveSymlink(File link,TaskListener listener) throws InterruptedException, IOException {
  if (Functions.isWindows())   return null;
  String filename=link.getAbsolutePath();
  try {
    for (int sz=512; sz < 65536; sz*=2) {
      Memory m=new Memory(sz);
      int r=LIBC.readlink(filename,m,new NativeLong(sz));
      if (r < 0) {
        int err=Native.getLastError();
        if (err == 22)         return null;
        throw new IOException("Failed to readlink " + link + " error="+ err+ " "+ LIBC.strerror(err));
      }
      if (r == sz)       continue;
      byte[] buf=new byte[r];
      m.read(0,buf,0,r);
      return new String(buf);
    }
    throw new IOException("Symlink too long: " + link);
  }
 catch (  LinkageError e) {
    return PosixAPI.get().readlink(filename);
  }
}
 

Code Example 13:

  7 
vote

From project intellij-community, under directory /platform/platform-impl/src/com/intellij/ide/passwordSafe/impl/providers/masterKey/windows/.

Source WindowsCryptUtils.java

/** 
 * Protect the specified byte range
 * @param data the data to protect
 * @return the the protected form the data
 */
public static byte[] protect(byte[] data) throws MasterPasswordUnavailableException {
  if (data.length == 0) {
    return data;
  }
  Memory input=new Memory(data.length);
  input.write(0,data,0,data.length);
  Crypt32.DATA_BLOB in=new Crypt32.DATA_BLOB();
  in.cbData=new W32API.DWORD(data.length);
  in.pbData=input;
  Crypt32.DATA_BLOB out=new Crypt32.DATA_BLOB();
  out.pbData=Pointer.NULL;
  Crypt32 crypt=Crypt32.INSTANCE;
  Kernel32 kernel=Kernel32.INSTANCE;
  boolean rc=crypt.CryptProtectData(in,"Master Key",Pointer.NULL,Pointer.NULL,Pointer.NULL,new W32API.DWORD(0),out);
  if (!rc) {
    W32API.DWORD drc=kernel.GetLastError();
    throw new MasterPasswordUnavailableException("CryptProtectData failed: " + drc.intValue());
  }
 else {
    byte[] output=new byte[out.cbData.intValue()];
    out.pbData.read(0,output,0,output.length);
    kernel.LocalFree(out.pbData);
    return output;
  }
}
 

Code Example 14:

  7 
vote

From project intellij-community, under directory /platform/platform-impl/src/com/intellij/ide/passwordSafe/impl/providers/masterKey/windows/.

Source WindowsCryptUtils.java

/** 
 * Unprotect the specified byte range
 * @param data the data to protect
 * @return the the protected form the data
 */
public static byte[] unprotect(byte[] data) throws MasterPasswordUnavailableException {
  if (data.length == 0) {
    return data;
  }
  Memory input=new Memory(data.length);
  input.write(0,data,0,data.length);
  Crypt32.DATA_BLOB in=new Crypt32.DATA_BLOB();
  in.cbData=new W32API.DWORD(data.length);
  in.pbData=input;
  Crypt32.DATA_BLOB out=new Crypt32.DATA_BLOB();
  out.pbData=Pointer.NULL;
  Crypt32 crypt=Crypt32.INSTANCE;
  Kernel32 kernel=Kernel32.INSTANCE;
  boolean rc=crypt.CryptUnprotectData(in,Pointer.NULL,Pointer.NULL,Pointer.NULL,Pointer.NULL,new W32API.DWORD(0),out);
  if (!rc) {
    W32API.DWORD drc=kernel.GetLastError();
    throw new MasterPasswordUnavailableException("CryptProtectData failed: " + drc.intValue());
  }
 else {
    byte[] output=new byte[out.cbData.intValue()];
    out.pbData.read(0,output,0,output.length);
    kernel.LocalFree(out.pbData);
    return output;
  }
}
 

Code Example 15:

  7 
vote

From project intellij-community, under directory /platform/util/src/com/intellij/openapi/util/io/.

Source FileSystemUtil.java

private JnaUnixMediatorImpl() throws Exception {
  myLibC=(LibC)Native.loadLibrary("c",LibC.class);
  mySharedMem=new Memory(256);
  myModeOffset=SystemInfo.isLinux ? (SystemInfo.is32Bit ? 16 : 24) : SystemInfo.isMac | SystemInfo.isFreeBSD ? 8 : SystemInfo.isSolaris ? (SystemInfo.is32Bit ? 20 : 16) : -1;
  mySizeOffset=SystemInfo.isLinux ? (SystemInfo.is32Bit ? 44 : 48) : SystemInfo.isMac | SystemInfo.isFreeBSD ? (SystemInfo.is32Bit ? 48 : 72) : SystemInfo.isSolaris ? (SystemInfo.is32Bit ? 48 : 40) : -1;
  myTimeOffset=SystemInfo.isLinux ? (SystemInfo.is32Bit ? 72 : 88) : SystemInfo.isMac | SystemInfo.isFreeBSD ? (SystemInfo.is32Bit ? 32 : 40) : SystemInfo.isSolaris ? 64 : -1;
  if (myModeOffset < 0)   throw new IllegalStateException("Unsupported OS: " + SystemInfo.OS_NAME);
}
 

Code Example 16:

  7 
vote

From project jna, under directory /contrib/platform/src/com/sun/jna/platform/win32/.

Source Advapi32Util.java

/** 
 * Set a string array value in registry.
 * @param hKey Parent key.
 * @param name Name.
 * @param arr Array of strings to write to registry.
 */
public static void registrySetStringArray(HKEY hKey,String name,String[] arr){
  int size=0;
  for (  String s : arr) {
    size+=s.length() * Native.WCHAR_SIZE;
    size+=Native.WCHAR_SIZE;
  }
  size+=Native.WCHAR_SIZE;
  int offset=0;
  Memory data=new Memory(size);
  for (  String s : arr) {
    data.setString(offset,s,true);
    offset+=s.length() * Native.WCHAR_SIZE;
    offset+=Native.WCHAR_SIZE;
  }
  for (int i=0; i < Native.WCHAR_SIZE; i++) {
    data.setByte(offset++,(byte)0);
  }
  int rc=Advapi32.INSTANCE.RegSetValueEx(hKey,name,0,WinNT.REG_MULTI_SZ,data.getByteArray(0,size),size);
  if (rc != W32Errors.ERROR_SUCCESS) {
    throw new Win32Exception(rc);
  }
}
 

Code Example 17:

  6 
vote

From project DJ-Native-Swing, under directory /jna_WindowUtils/src/com/sun/jna/examples/.

Source WindowUtils.java

@Override protected void paintDirect(BufferedImage buf,Rectangle bounds){
  Window window=SwingUtilities.getWindowAncestor(this);
  X11 x11=X11.INSTANCE;
  X11.Display dpy=x11.XOpenDisplay(null);
  X11.Window win=getDrawable(window);
  Point offset=new Point();
  win=getContentWindow(window,dpy,win,offset);
  X11.GC gc=x11.XCreateGC(dpy,win,new NativeLong(0),null);
  Raster raster=buf.getData();
  int w=bounds.width;
  int h=bounds.height;
  if (buffer == null || buffer.getSize() != w * h * 4) {
    buffer=new Memory(w * h * 4);
    pixels=new int[w * h];
  }
  for (int y=0; y < h; y++) {
    for (int x=0; x < w; x++) {
      raster.getPixel(x,y,pixel);
      int alpha=pixel[3] & 0xFF;
      int red=pixel[2] & 0xFF;
      int green=pixel[1] & 0xFF;
      int blue=pixel[0] & 0xFF;
      pixels[y * w + x]=(alpha << 24) | (blue << 16) | (green << 8)| red;
    }
  }
  X11.XWindowAttributes xwa=new X11.XWindowAttributes();
  x11.XGetWindowAttributes(dpy,win,xwa);
  X11.XImage image=x11.XCreateImage(dpy,xwa.visual,32,X11.ZPixmap,0,buffer,w,h,32,w * 4);
  buffer.write(0,pixels,0,pixels.length);
  offset.x+=bounds.x;
  offset.y+=bounds.y;
  x11.XPutImage(dpy,win,gc,image,0,0,offset.x,offset.y,w,h);
  x11.XFree(image.getPointer());
  x11.XFreeGC(dpy,gc);
  x11.XCloseDisplay(dpy);
}
 

Code Example 18:

  6 
vote

From project fiji, under directory /misc/FFMPEG_IO/plugin/src/main/java/fiji/ffmpeg/.

Source IO.java

protected void writeVideoFrame(ImageProcessor ip,AVStream st) throws IOException {
  int outSize=0;
  if (ip == null) {
  }
 else {
    if (codecContext.pix_fmt == bufferFramePixelFormat)     fillImage(frame,ip);
 else {
      fillImage(bufferFrame,ip);
      convertFrom();
    }
  }
  AVOutputFormat tmpFmt=new AVOutputFormat(formatContext.oformat);
  if ((tmpFmt.flags & AVFORMAT.AVFMT_RAWPICTURE) != 0) {
    avCodec.av_init_packet(packet);
    packet.flags|=AVCODEC.PKT_FLAG_KEY;
    packet.stream_index=st.index;
    packet.data=frame.getPointer();
    packet.size=frame.size();
    if (avFormat.av_interleaved_write_frame(formatContext,packet) != 0)     throw new IOException("Error while writing video frame");
  }
 else {
    if (videoOutbutMemory == null)     videoOutbutMemory=new Memory(videoOutbut.length);
    outSize=avCodec.avcodec_encode_video(codecContext,videoOutbut,videoOutbut.length,frame);
    if (outSize > 0) {
      avCodec.av_init_packet(packet);
      AVFrame tmpFrame=new AVFrame(codecContext.coded_frame);
      packet.pts=avUtil.av_rescale_q(tmpFrame.pts,new AVUTIL.AVRational.ByValue(codecContext.time_base),new AVUTIL.AVRational.ByValue(st.time_base));
      if (tmpFrame.key_frame == 1)       packet.flags|=AVCODEC.PKT_FLAG_KEY;
      packet.stream_index=st.index;
      videoOutbutMemory.write(0,videoOutbut,0,outSize);
      packet.data=videoOutbutMemory;
      packet.size=outSize;
      if (avFormat.av_interleaved_write_frame(formatContext,packet) != 0)       throw new IOException("Error while writing video frame");
      st.pts.val=packet.pts;
    }
  }
}
 

Code Example 19:

  6 
vote

From project jna, under directory /contrib/platform/src/com/sun/jna/platform/win32/.

Source Advapi32Util.java

private boolean read(){
  if (_done || _dwRead > 0) {
    return false;
  }
  IntByReference pnBytesRead=new IntByReference();
  IntByReference pnMinNumberOfBytesNeeded=new IntByReference();
  if (!Advapi32.INSTANCE.ReadEventLog(_h,WinNT.EVENTLOG_SEQUENTIAL_READ | _flags,0,_buffer,(int)_buffer.size(),pnBytesRead,pnMinNumberOfBytesNeeded)) {
    int rc=Kernel32.INSTANCE.GetLastError();
    if (rc == W32Errors.ERROR_INSUFFICIENT_BUFFER) {
      _buffer=new Memory(pnMinNumberOfBytesNeeded.getValue());
      if (!Advapi32.INSTANCE.ReadEventLog(_h,WinNT.EVENTLOG_SEQUENTIAL_READ | _flags,0,_buffer,(int)_buffer.size(),pnBytesRead,pnMinNumberOfBytesNeeded)) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
      }
    }
 else {
      close();
      if (rc != W32Errors.ERROR_HANDLE_EOF) {
        throw new Win32Exception(rc);
      }
      return false;
    }
  }
  _dwRead=pnBytesRead.getValue();
  _pevlr=_buffer;
  return true;
}
 

Code Example 20:

  6 
vote

From project jna, under directory /contrib/platform/src/com/sun/jna/platform/.

Source WindowUtils.java

protected void paintDirect(BufferedImage buf,Rectangle bounds){
  Window window=SwingUtilities.getWindowAncestor(this);
  X11 x11=X11.INSTANCE;
  X11.Display dpy=x11.XOpenDisplay(null);
  X11.Window win=getDrawable(window);
  Point offset=new Point();
  win=getContentWindow(window,dpy,win,offset);
  X11.GC gc=x11.XCreateGC(dpy,win,new NativeLong(0),null);
  Raster raster=buf.getData();
  int w=bounds.width;
  int h=bounds.height;
  if (buffer == null || buffer.size() != w * h * 4) {
    buffer=new Memory(w * h * 4);
    pixels=new int[w * h];
  }
  for (int y=0; y < h; y++) {
    for (int x=0; x < w; x++) {
      raster.getPixel(x,y,pixel);
      int alpha=pixel[3] & 0xFF;
      int red=pixel[2] & 0xFF;
      int green=pixel[1] & 0xFF;
      int blue=pixel[0] & 0xFF;
      pixels[y * w + x]=(alpha << 24) | (blue << 16) | (green << 8)| red;
    }
  }
  X11.XWindowAttributes xwa=new X11.XWindowAttributes();
  x11.XGetWindowAttributes(dpy,win,xwa);
  X11.XImage image=x11.XCreateImage(dpy,xwa.visual,32,X11.ZPixmap,0,buffer,w,h,32,w * 4);
  buffer.write(0,pixels,0,pixels.length);
  offset.x+=bounds.x;
  offset.y+=bounds.y;
  x11.XPutImage(dpy,win,gc,image,0,0,offset.x,offset.y,w,h);
  x11.XFree(image.getPointer());
  x11.XFreeGC(dpy,gc);
  x11.XCloseDisplay(dpy);
}
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值