本文整理匯總了Java中android.net.LocalSocket類的典型用法代碼示例。如果您正苦於以下問題:Java LocalSocket類的具體用法?Java LocalSocket怎麽用?Java LocalSocket使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
LocalSocket類屬於android.net包,在下文中一共展示了LocalSocket類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createSockets
import android.net.LocalSocket; //導入依賴的package包/類
protected void createSockets() throws IOException {
final String LOCAL_ADDR = "net.majorkernelpanic.streaming-";
for (int i=0;i<10;i++) {
try {
mSocketId = new Random().nextInt();
mLss = new LocalServerSocket(LOCAL_ADDR+mSocketId);
break;
} catch (IOException e1) {}
}
mReceiver = new LocalSocket();
mReceiver.connect( new LocalSocketAddress(LOCAL_ADDR+mSocketId));
mReceiver.setReceiveBufferSize(500000);
mReceiver.setSoTimeout(3000);
mSender = mLss.accept();
mSender.setSendBufferSize(500000);
}
示例2: run
import android.net.LocalSocket; //導入依賴的package包/類
@Override
public void run() {
while(true) {
try {
final LocalSocket socket = localServerSocket.accept();
final WorkerThread workerThread = new WorkerThread(socket, socketHandler);
workerThread.setName("SuasUnixSocket " + threadId.incrementAndGet());
workerThread.setDaemon(true);
workerThread.start();
} catch (IOException e) {
Log.e(LOG_TAG, "Server thread error", e);
e.printStackTrace();
break;
}
}
}
示例3: onSecured
import android.net.LocalSocket; //導入依賴的package包/類
@Override
protected void onSecured(LocalSocket socket) throws IOException {
LeakyBufferedInputStream leakyIn = new LeakyBufferedInputStream(
socket.getInputStream(),
SENSING_BUFFER_SIZE);
if (mHandlers.isEmpty()) {
throw new IllegalStateException("No handlers added");
}
for (int i = 0, N = mHandlers.size(); i < N; i++) {
HandlerInfo handlerInfo = mHandlers.get(i);
leakyIn.mark(SENSING_BUFFER_SIZE);
boolean matches = handlerInfo.magicMatcher.matches(leakyIn);
leakyIn.reset();
if (matches) {
LogUtil.d("Matches!" + handlerInfo.handler.getClass().getSimpleName());
SocketLike socketLike = new SocketLike(socket, leakyIn);
handlerInfo.handler.onAccepted(socketLike);
return;
}
}
throw new IOException("No matching handler, firstByte=" + leakyIn.read());
}
示例4: enforcePermission
import android.net.LocalSocket; //導入依賴的package包/類
private static void enforcePermission(Context context, LocalSocket peer)
throws IOException, PeerAuthorizationException {
Credentials credentials = peer.getPeerCredentials();
int uid = credentials.getUid();
int pid = credentials.getPid();
if (LogUtil.isLoggable(Log.VERBOSE)) {
LogUtil.v("Got request from uid=%d, pid=%d", uid, pid);
}
String requiredPermission = Manifest.permission.DUMP;
int checkResult = context.checkPermission(requiredPermission, pid, uid);
if (checkResult != PackageManager.PERMISSION_GRANTED) {
throw new PeerAuthorizationException(
"Peer pid=" + pid + ", uid=" + uid + " does not have " + requiredPermission);
}
}
示例5: onSecured
import android.net.LocalSocket; //導入依賴的package包/類
@Override
protected void onSecured(LocalSocket socket) throws IOException {
LeakyBufferedInputStream leakyIn = new LeakyBufferedInputStream(
socket.getInputStream(),
SENSING_BUFFER_SIZE);
if (mHandlers.isEmpty()) {
throw new IllegalStateException("No handlers added");
}
for (int i = 0, N = mHandlers.size(); i < N; i++) {
HandlerInfo handlerInfo = mHandlers.get(i);
leakyIn.mark(SENSING_BUFFER_SIZE);
boolean matches = handlerInfo.magicMatcher.matches(leakyIn);
leakyIn.reset();
if (matches) {
SocketLike socketLike = new SocketLike(socket, leakyIn);
handlerInfo.handler.onAccepted(socketLike);
return;
}
}
throw new IOException("No matching handler, firstByte=" + leakyIn.read());
}
示例6: write
import android.net.LocalSocket; //導入依賴的package包/類
private static void write(final LocalSocket socket) {
new Thread() {
@Override
public void run() {
super.run();
try {
final int VERSION = 2;
BufferedOutputStream outputStream = new BufferedOutputStream(socket.getOutputStream());
while (true) {
Bitmap bitmap = screenshot();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, byteArrayOutputStream);
outputStream.write(2);
writeInt(outputStream, byteArrayOutputStream.size());
outputStream.write(byteArrayOutputStream.toByteArray());
outputStream.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
示例7: init
import android.net.LocalSocket; //導入依賴的package包/類
public void init() {
try {
localLoop = new LocalServerSocket("videoserver");
localReceiver = new LocalSocket();
localReceiver.connect(localLoop.getLocalSocketAddress());
localReceiver.setReceiveBufferSize(LOCAL_BUFF_SIZE);
localReceiver.setSendBufferSize(LOCAL_BUFF_SIZE);
localSender = localLoop.accept();
localSender.setReceiveBufferSize(LOCAL_BUFF_SIZE);
localSender.setSendBufferSize(LOCAL_BUFF_SIZE);
Log.d(LOG_TAG, "Done: init()");
}catch(IOException e) {
Log.e(LOG_TAG, "Error in initializing local socket: " + e);
}
}
示例8: send
import android.net.LocalSocket; //導入依賴的package包/類
/**
* Sends data to the socket.
*
* @param data Data to send. Data size is defined by the length of the
* array.
* @throws IOException
*/
public void send(byte[] data) throws IOException {
// Use local copy of the socket, ensuring it's not going to NULL while
// we're working with it. If it gets closed, while we're in the middle
// of data transfer - it's OK, since it will produce an exception, and
// the caller will gracefully handle it.
//
// Same technique is used everywhere in this class where mSocket member
// is touched.
LocalSocket socket = mSocket;
if (socket == null) {
Logw("'send' request on closed Socket " + mChannelName);
throw new ClosedChannelException();
}
socket.getOutputStream().write(data);
}
示例9: close
import android.net.LocalSocket; //導入依賴的package包/類
/**
* Closes the socket.
*
* @return true if socket has been closed in this call, or false if it had
* been already closed when this method has been called.
*/
public boolean close() {
// This is the only place in this class where we will null the socket
// object. Since this method can be called concurrently from different
// threads, lets do this under the lock.
LocalSocket socket;
synchronized (this) {
socket = mSocket;
mSocket = null;
}
if (socket != null) {
try {
// Force all I/O to stop before closing the socket.
socket.shutdownInput();
socket.shutdownOutput();
socket.close();
if (DEBUG) Log.d(TAG, "Socket is closed for " + mChannelName);
return true;
} catch (IOException e) {
Loge("Exception " + e + " while closing Socket for " + mChannelName);
}
}
return false;
}
示例10: processRequestsUntilStopped
import android.net.LocalSocket; //導入依賴的package包/類
private void processRequestsUntilStopped(LocalSocket fdrecv, ReadableByteChannel status, Writer control) throws IOException, InterruptedException {
FdReq fileOps;
while ((fileOps = intake.take()) != FdReq.STOP) {
FdResp response = null;
try {
try {
response = sendFdRequest(fileOps, control, status, fdrecv);
if (!responses.offer(response, IO_TIMEOUT, TimeUnit.MILLISECONDS))
FdCompat.closeDescriptor(response.fd);
} catch (IOException ioe) {
responses.offer(new FdResp(fileOps, ioe.getMessage(), null), IO_TIMEOUT, TimeUnit.MILLISECONDS);
throw ioe;
}
} catch (InterruptedException ie) {
if (response != null)
FdCompat.closeDescriptor(response.fd);
throw ie;
}
}
}
示例11: createSockets
import android.net.LocalSocket; //導入依賴的package包/類
protected void createSockets() throws IOException {
final String LOCAL_ADDR = "net.majorkernelpanic.streaming-";
for (int i=0;i<10;i++) {
try {
mSocketId = new Random().nextInt();
mLss = new LocalServerSocket(LOCAL_ADDR+mSocketId);
break;
} catch (IOException e1) {}
}
mReceiver = new LocalSocket();
mReceiver.connect( new LocalSocketAddress(LOCAL_ADDR+mSocketId));
mReceiver.setReceiveBufferSize(1000000);
mReceiver.setSoTimeout(3000);
mSender = mLss.accept();
mSender.setSendBufferSize(1000000);
}
示例12: createSockets
import android.net.LocalSocket; //導入依賴的package包/類
protected void createSockets() throws IOException {
final String LOCAL_ADDR = "edu.ucsb.cs.capstone.letmypeoplecode.smartrover.streaming-";
for (int i=0;i<10;i++) {
try {
mSocketId = new Random().nextInt();
mLss = new LocalServerSocket(LOCAL_ADDR+mSocketId);
break;
} catch (IOException e1) {}
}
mReceiver = new LocalSocket();
mReceiver.connect( new LocalSocketAddress(LOCAL_ADDR+mSocketId));
mReceiver.setReceiveBufferSize(500000);
mReceiver.setSoTimeout(3000);
mSender = mLss.accept();
mSender.setSendBufferSize(500000);
}
示例13: simplestGet
import android.net.LocalSocket; //導入依賴的package包/類
@Test(timeout = 1000)
public void simplestGet() throws IOException, InterruptedException {
// given
Service service = startService();
// when
LocalSocket clientSocket = new LocalSocket();
clientSocket.connect(new LocalSocketAddress("doesn't matter now"));
OutputStream os = clientSocket.getOutputStream();
os.write(GET_1.getBytes());
os.close();
// then
InputStream is = clientSocket.getInputStream();
String response = IOUtils.toString(is);
System.out.println("response: " + response);
String expectedBody = new Test1().test();
assertThat(response, containsString(expectedBody));
}
示例14: simplestPost
import android.net.LocalSocket; //導入依賴的package包/類
@Test(timeout = 1000)
public void simplestPost() throws IOException, InterruptedException {
// given
Service service = startService();
// when
LocalSocket clientSocket = new LocalSocket();
clientSocket.connect(new LocalSocketAddress("doesn't matter now"));
OutputStream os = clientSocket.getOutputStream();
os.write(POST_1.getBytes());
os.close();
// then
InputStream is = clientSocket.getInputStream();
String response = IOUtils.toString(is);
System.out.println("response: " + response);
String expectedBody = new TestPost().post(11, "somename");
assertThat(response, containsString(expectedBody));
}
示例15: shouldParseLongGetRequests
import android.net.LocalSocket; //導入依賴的package包/類
@Test(timeout = 1000)
public void shouldParseLongGetRequests() throws IOException {
// given
Service service = startService();
// when
LocalSocket clientSocket = new LocalSocket();
clientSocket.connect(new LocalSocketAddress("doesn't matter now"));
OutputStream os = clientSocket.getOutputStream();
os.write(GET_LONG_1.getBytes()); // two long requests
os.write(GET_LONG_1.getBytes());
os.close();
// then
InputStream is = clientSocket.getInputStream();
String response = IOUtils.toString(is);
System.out.println("response: " + response);
String expectedBody = new Test1().test();
assertThat(response, containsString(expectedBody));
}