id stringlengths 36 36 | text stringlengths 1 1.25M |
|---|---|
9c6f5d45-f952-4c67-98d2-9e63cadaa4f9 | public void testApp()
{
assertTrue( true );
} |
6ab0940e-a985-4747-a079-b5761eb4990b | private static void dumpName(final UsbDevice device)
throws UnsupportedEncodingException, UsbException
{
// Read the string descriptor indices from the device descriptor.
// If they are missing then ignore the device.
final UsbDeviceDescriptor desc = device.getUsbDeviceDescriptor();
final byte iManufacturer = desc.iManufacturer();
final byte iProduct = desc.iProduct();
if (iManufacturer == 0 || iProduct == 0) return;
// Dump the device name
System.out.println(device.getString(iManufacturer) + " "
+ device.getString(iProduct));
} |
4260832c-2c85-4d6d-9581-2cc6c556ea51 | private static void processDevice(final UsbDevice device)
{
// When device is a hub then process all child devices
if (device.isUsbHub())
{
final UsbHub hub = (UsbHub) device;
for (UsbDevice child: (List<UsbDevice>) hub.getAttachedUsbDevices())
{
processDevice(child);
}
}
// When device is not a hub then dump its name.
else
{
try
{
dumpName(device);
}
catch (Exception e)
{
// On Linux this can fail because user has no write permission
// on the USB device file. On Windows it can fail because
// no libusb device driver is installed for the device
System.err.println("Ignoring problematic device: " + e);
}
}
} |
d0f4edea-b863-4b40-851a-28c8d2652c40 | public static void main(final String[] args) throws UsbException
{
// Get the USB services and dump information about them
final UsbServices services = UsbHostManager.getUsbServices();
// Dump the root USB hub
processDevice(services.getRootUsbHub());
} |
c903b24d-9905-4583-84b7-5f478d44b682 | private static void dumpDevice(final UsbDevice device)
{
// Dump information about the device itself
System.out.println(device);
final UsbPort port = device.getParentUsbPort();
if (port != null)
{
System.out.println("Connected to port: " + port.getPortNumber());
System.out.println("Parent: " + port.getUsbHub());
}
// Dump device descriptor
System.out.println(device.getUsbDeviceDescriptor());
// Process all configurations
for (UsbConfiguration configuration: (List<UsbConfiguration>) device
.getUsbConfigurations())
{
// Dump configuration descriptor
System.out.println(configuration.getUsbConfigurationDescriptor());
// Process all interfaces
for (UsbInterface iface: (List<UsbInterface>) configuration
.getUsbInterfaces())
{
// Dump the interface descriptor
System.out.println(iface.getUsbInterfaceDescriptor());
// Process all endpoints
for (UsbEndpoint endpoint: (List<UsbEndpoint>) iface
.getUsbEndpoints())
{
// Dump the endpoint descriptor
System.out.println(endpoint.getUsbEndpointDescriptor());
}
}
}
System.out.println();
// Dump child devices if device is a hub
if (device.isUsbHub())
{
final UsbHub hub = (UsbHub) device;
for (UsbDevice child: (List<UsbDevice>) hub.getAttachedUsbDevices())
{
dumpDevice(child);
}
}
} |
56ff0cf2-bed8-4600-b82e-629fafac9efc | public static void main(final String[] args) throws UsbException
{
// Get the USB services and dump information about them
final UsbServices services = UsbHostManager.getUsbServices();
System.out.println("USB Service Implementation: "
+ services.getImpDescription());
System.out.println("Implementation version: "
+ services.getImpVersion());
System.out.println("Service API version: " + services.getApiVersion());
System.out.println();
// Dump the root USB hub
dumpDevice(services.getRootUsbHub());
} |
cafb5405-be40-43d3-9ff6-ef55d47943f7 | public static UsbDevice findMissileLauncher(UsbHub hub)
{
UsbDevice launcher = null;
for (UsbDevice device: (List<UsbDevice>) hub.getAttachedUsbDevices())
{
if (device.isUsbHub())
{
launcher = findMissileLauncher((UsbHub) device);
if (launcher != null) return launcher;
}
else
{
UsbDeviceDescriptor desc = device.getUsbDeviceDescriptor();
if (desc.idVendor() == VENDOR_ID &&
desc.idProduct() == PRODUCT_ID) return device;
}
}
return null;
} |
901debe9-3277-4829-9146-10eb5048e106 | public static void sendMessage(UsbDevice device, byte[] message)
throws UsbException
{
UsbControlIrp irp = device.createUsbControlIrp(
(byte) (UsbConst.REQUESTTYPE_TYPE_CLASS |
UsbConst.REQUESTTYPE_RECIPIENT_INTERFACE), (byte) 0x09,
(short) 2, (short) 1);
irp.setData(message);
device.syncSubmit(irp);
} |
7aeb49ba-a468-4e1b-a710-2b8fc0b3261f | public static void sendCommand(UsbDevice device, int command)
throws UsbException
{
byte[] message = new byte[64];
message[1] = (byte) ((command & CMD_LEFT) > 0 ? 1 : 0);
message[2] = (byte) ((command & CMD_RIGHT) > 0 ? 1 : 0);
message[3] = (byte) ((command & CMD_UP) > 0 ? 1 : 0);
message[4] = (byte) ((command & CMD_DOWN) > 0 ? 1 : 0);
message[5] = (byte) ((command & CMD_FIRE) > 0 ? 1 : 0);
message[6] = 8;
message[7] = 8;
sendMessage(device, INIT_A);
sendMessage(device, INIT_B);
sendMessage(device, message);
} |
d1c06f36-126d-4704-a971-b65b90541ace | public static char readKey()
{
try
{
String line =
new BufferedReader(new InputStreamReader(System.in)).readLine();
if (line.length() > 0) return line.charAt(0);
return 0;
}
catch (IOException e)
{
throw new RuntimeException("Unable to read key", e);
}
} |
c2ba4f37-b124-42ef-a12b-fdbd9d0e15cb | public static void main(String[] args) throws UsbException
{
// Search for the missile launcher USB device and stop when not found
UsbDevice device = findMissileLauncher(
UsbHostManager.getUsbServices().getRootUsbHub());
if (device == null)
{
System.err.println("Missile launcher not found.");
System.exit(1);
return;
}
// Claim the interface
UsbConfiguration configuration = device.getUsbConfiguration((byte) 1);
UsbInterface iface = configuration.getUsbInterface((byte) 1);
iface.claim(new UsbInterfacePolicy()
{
@Override
public boolean forceClaim(UsbInterface usbInterface)
{
return true;
}
});
// Read commands and execute them
System.out.println("WADX = Move, S = Stop, F = Fire, Q = Exit");
boolean exit = false;
while (!exit)
{
System.out.print("> ");
char key = readKey();
switch (key)
{
case 'w':
sendCommand(device, CMD_UP);
break;
case 'x':
sendCommand(device, CMD_DOWN);
break;
case 'a':
sendCommand(device, CMD_LEFT);
break;
case 'd':
sendCommand(device, CMD_RIGHT);
break;
case 'f':
sendCommand(device, CMD_FIRE);
break;
case 's':
sendCommand(device, 0);
break;
case 'q':
exit = true;
break;
default:
}
}
System.out.println("Exiting");
} |
78f4294d-b646-4978-8c0f-f9b091d0e397 | @Override
public boolean forceClaim(UsbInterface usbInterface)
{
return true;
} |
979ec716-5ee8-4016-9445-8c0b32e994ce | public static void main(String[] args) throws Exception
{
// Find a ADB device to communicate with.
List<AdbDevice> devices = Adb.findDevices();
if (devices.isEmpty())
{
System.err.println("No ADB devices found");
System.exit(1);
return;
}
AdbDevice device = devices.get(0);
// Do some ADB communication
device.open();
try
{
// Send the connect message
Message message = new ConnectMessage(
ConnectMessage.SYSTEM_TYPE_HOST, "12345678", "ADB Demo");
System.out.println("Sending: " + message);
device.sendMessage(message);
// Repeat until we are connected
boolean triedAuthentication = false;
boolean sentPublicKey = false;
boolean connected = false;
while (!connected)
{
message = device.receiveMessage();
System.out.println("Received: " + message);
// If connect message has been received then we are finished
if (message instanceof ConnectMessage)
{
connected = true;
}
// Process auth message
else if (message instanceof AuthMessage)
{
AuthMessage authMessage = (AuthMessage) message;
// Sign token if we didn't tried it already
if (!triedAuthentication)
{
byte[] signature = Adb.signToken(authMessage.getData());
message =
new AuthMessage(AuthMessage.TYPE_SIGNATURE,
signature);
System.out.println("Sending: " + message);
device.sendMessage(message);
triedAuthentication = true;
}
// If token signing already failed then sent public key
else if (!sentPublicKey)
{
byte[] publicKey = Adb.getPublicKey();
message =
new AuthMessage(AuthMessage.TYPE_RSAPUBLICKEY,
publicKey);
System.out.println("Sending: " + message);
device.sendMessage(message);
triedAuthentication = false;
sentPublicKey = true;
}
// Can't authenticate for some reason
else
{
System.err.println("Couldn't authenticate");
System.exit(1);
}
}
// Unknown message received
else
{
System.err.println("Received unexpected message: "
+ message);
System.exit(1);
}
}
// Open "sync:"
message = new OpenMessage(1, "sync:");
System.out.println("Sending: " + message);
device.sendMessage(message);
message = device.receiveMessage();
System.out.println("Received: " + message);
if (!(message instanceof OkayMessage))
{
System.err.println("Open failed");
System.exit(1);
}
int remoteId = ((OkayMessage) message).getRemoteId();
// Close
message = new CloseMessage(1, remoteId);
System.out.println("Sending: " + message);
device.sendMessage(message);
message = device.receiveMessage();
System.out.println("Received: " + message);
}
finally
{
device.close();
}
} |
448e6b4b-1d00-4f1c-986b-b085c3f2233f | public static void dump(UsbDevice device, int level)
{
for (int i = 0; i < level; i += 1)
System.out.print(" ");
System.out.println(device);
if (device.isUsbHub())
{
final UsbHub hub = (UsbHub) device;
for (UsbDevice child: (List<UsbDevice>) hub.getAttachedUsbDevices())
{
dump(child, level + 1);
}
}
} |
3ef2ac9e-53cd-4bb1-acf2-e41e0b88a35b | public static void main(String[] args) throws UsbException
{
UsbServices services = UsbHostManager.getUsbServices();
dump(services.getRootUsbHub(), 0);
} |
eaf11b0c-4fd9-4c2c-8811-ca14ab2d467f | public ConnectMessage(MessageHeader header, byte[] data)
{
super(header, data);
} |
f9627682-3c61-4d18-82cb-f3a36d7a8085 | public ConnectMessage(int version, int maxData, String systemType,
String serialNo, String banner)
{
this(version, maxData, buildIdentity(systemType, serialNo, banner));
} |
37b155e0-c595-4172-96c0-8f14a7e75001 | public ConnectMessage(int version, int maxData, byte[] identity)
{
super(MessageHeader.CMD_CNXN, version, maxData, identity);
} |
21b0fdc6-fc61-41f7-adf0-64ec4b116e4b | public ConnectMessage(String systemType, String serialNo, String banner)
{
this(DEFAULT_PROTOCOL_VERSION, DEFAULT_MAX_DATA, systemType, serialNo,
banner);
} |
f9fa9d6a-a860-4094-90fd-eeba62d16652 | private static byte[] buildIdentity(String systemType, String serialNo,
String banner)
{
if (systemType == null)
throw new IllegalArgumentException("systemType must be set");
if (serialNo == null)
throw new IllegalArgumentException("serialNo must be set");
if (banner == null)
throw new IllegalArgumentException("banner must be set");
return (systemType + ":" + serialNo + ":" + banner + '\0')
.getBytes(Charset.forName("UTF-8"));
} |
592201bf-29b8-41c6-bdfa-9dd8ab924d2f | public int getVersion()
{
return this.header.getArg0();
} |
39ef1f9d-1409-4816-a5bf-0cd5bda1a317 | public int getMaxData()
{
return this.header.getArg1();
} |
1d5013c3-f977-4630-8b59-51c55b65bdb8 | public String getIdentity()
{
int len = this.data.length;
while (len > 0 && this.data[len - 1] == 0) len--;
return new String(this.data, 0, len, Charset.forName("UTF-8"));
} |
ccc03605-6c8c-4e80-82b1-9593349c0843 | public String getSystemType()
{
return getIdentity().split(":")[0];
} |
ac764cfb-9c87-4aeb-8cfb-55e5fe0aeebb | public String getSerialNo()
{
return getIdentity().split(":")[1];
} |
3b4df141-9771-403a-8535-d52c440f8cb8 | public String getBanner()
{
return getIdentity().split(":")[2];
} |
16f3e417-e17e-431e-9a56-471bf712add4 | @Override
public String toString()
{
return String.format("CONNECT(0x%08x, %d, \"%s\")",
getVersion(), getMaxData(), getIdentity());
} |
40603a63-3312-4666-acbf-0cbf12dc0d9b | private static short[] createVendorIds()
{
Set<Short> vendorIds = new HashSet<Short>();
for (short vendorId: FIXED_VENDOR_IDS)
vendorIds.add(vendorId);
File ini = new File(new File(System.getProperty("user.home"),
".android"), "adb_usb.ini");
try
{
if (ini.exists())
{
BufferedReader reader = new BufferedReader(new FileReader(ini));
try
{
String line;
while ((line = reader.readLine()) != null)
{
if (line.startsWith("0x"))
{
vendorIds.add((short) Integer.parseInt(
line.substring(2), 16));
}
}
}
finally
{
reader.close();
}
}
}
catch (IOException e)
{
LOG.log(Level.WARNING,
"adb_usb.ini could not be read. Ignoring it.", e);
}
short[] result = new short[vendorIds.size()];
int i = 0;
for (Short vendorId: vendorIds)
result[i++] = vendorId;
return result;
} |
93ff0810-67b7-4070-aeb6-a6d9092a7c12 | public AdbDevice(UsbInterface iface, byte inEndpoint,
byte outEndpoint)
{
if (iface == null)
throw new IllegalArgumentException("iface must be set");
this.iface = iface;
this.inEndpoint = inEndpoint;
this.outEndpoint = outEndpoint;
} |
484643ac-72e1-4481-899b-392d9778b227 | public void open() throws UsbException
{
this.iface.claim();
} |
eb963b90-da2e-48b7-85fa-2954c177c38f | public void close() throws UsbException
{
this.iface.release();
} |
f89bdc55-7c15-4bd7-b5c4-56d5fa899ec8 | public void sendMessage(Message message) throws UsbException
{
UsbEndpoint outEndpoint =
this.iface.getUsbEndpoint(this.outEndpoint);
UsbPipe outPipe = outEndpoint.getUsbPipe();
MessageHeader header = message.getHeader();
outPipe.open();
try
{
int sent = outPipe.syncSubmit(header.getBytes());
if (sent != MessageHeader.SIZE)
throw new InvalidMessageException(
"Invalid ADB message header size sent: " + sent);
sent = outPipe.syncSubmit(message.getData());
if (sent != header.getDataLength())
throw new InvalidMessageException(
"Data size mismatch in sent ADB message. Should be "
+ header.getDataLength() + " but is " + sent);
}
finally
{
outPipe.close();
}
} |
aea75062-0598-46f9-9619-b02fcfbc2e17 | public Message receiveMessage() throws UsbException
{
UsbEndpoint inEndpoint =
this.iface.getUsbEndpoint(this.inEndpoint);
UsbPipe inPipe = inEndpoint.getUsbPipe();
inPipe.open();
try
{
byte[] headerBytes = new byte[MessageHeader.SIZE];
int received = inPipe.syncSubmit(headerBytes);
if (received != MessageHeader.SIZE)
throw new InvalidMessageException(
"Invalid ADB message header size: " + received);
MessageHeader header = new MessageHeader(headerBytes);
if (!header.isValid())
throw new InvalidMessageException(
"ADB message header checksum failure");
byte[] data = new byte[header.getDataLength()];
received = inPipe.syncSubmit(data);
if (received != header.getDataLength())
throw new InvalidMessageException(
"ADB message data size mismatch. Should be "
+ header.getDataLength() + " but is " + received);
Message message = Message.create(header, data);
if (!message.isValid())
throw new InvalidMessageException(
"ADB message data checksum failure");
return message;
}
finally
{
inPipe.close();
}
} |
b5958819-688a-44ad-af07-12088762a08c | public InvalidMessageException(String message)
{
super(message);
} |
ff7d5871-a310-45ff-84f2-f2720090ef02 | protected Message(int command, int arg0, int arg1, byte[] data)
{
this.data = data;
int checksum = 0;
for (byte b: data)
checksum += b & 0xff;
this.header = new MessageHeader(command, arg0, arg1,
data.length, checksum, command ^ 0xffffffff);
} |
0cc80ac1-565f-42f3-a6ea-ffacb669d4c6 | public Message(MessageHeader header, byte[] data)
{
this.header = header;
this.data = data;
} |
2113d645-58fe-405f-8a95-874cd646c2c9 | public MessageHeader getHeader()
{
return this.header;
} |
28323be6-889d-4279-ac27-62e95461f965 | public byte[] getData()
{
return this.data;
} |
fc1fd8b2-821e-426e-8f21-14cfc1b57962 | public boolean isValid()
{
if (!this.header.isValid()) return false;
int checksum = 0;
for (byte b: this.data)
checksum += b & 0xff;
return checksum == this.header.getDataChecksum();
} |
c80e8795-5635-4802-85b7-f6529f7dfbd6 | public static Message create(MessageHeader header, byte[] data)
{
int command = header.getCommand();
switch (command)
{
case MessageHeader.CMD_CNXN:
return new ConnectMessage(header, data);
case MessageHeader.CMD_AUTH:
return new AuthMessage(header, data);
case MessageHeader.CMD_OPEN:
return new OpenMessage(header, data);
case MessageHeader.CMD_CLSE:
return new CloseMessage(header, data);
case MessageHeader.CMD_OKAY:
return new OkayMessage(header, data);
case MessageHeader.CMD_WRTE:
return new WriteMessage(header, data);
default:
throw new UnsupportedOperationException(String.format(
"Parsing of command 0x%08x not implemented yet",
command));
}
} |
842d941a-6a90-474a-ac19-cd7242c71adc | public OpenMessage(MessageHeader header, byte[] data)
{
super(header, data);
} |
04c6e3d1-a76d-4b0e-ab0b-8a899f0f9a64 | public OpenMessage(int localId, byte[] destination)
{
super(MessageHeader.CMD_OPEN, localId, 0, destination);
} |
06086422-653e-4a81-a4ff-632f95006017 | public OpenMessage(int localId, String destination)
{
this(localId, (destination + '\0').getBytes(Charset.forName("UTF-8")));
} |
54f398b4-f6db-417f-8657-8f1c7e1a1c07 | public int getLocalId()
{
return this.header.getArg0();
} |
f846a005-f7e3-43f7-913b-7409c14adc93 | public String getDestination()
{
int len = this.data.length;
while (len > 0 && this.data[len - 1] == 0) len--;
return new String(this.data, 0, len, Charset.forName("UTF-8"));
} |
c2735687-bc18-4f48-ae8d-45956621fcdb | @Override
public String toString()
{
return String.format("OPEN(%d, \"%s\")", getLocalId(),
getDestination());
} |
b6340da5-55c0-4b85-941e-23a56a7f2d6f | public static List<AdbDevice> findDevices() throws UsbException
{
UsbServices services = UsbHostManager.getUsbServices();
List<AdbDevice> usbDevices = new ArrayList<AdbDevice>();
findDevices(services.getRootUsbHub(), usbDevices);
return usbDevices;
} |
fdcc704b-ced1-473f-a3a4-f4bde2f706a2 | private static void findDevices(UsbHub hub, List<AdbDevice> devices)
{
for (UsbDevice usbDevice: (List<UsbDevice>) hub.getAttachedUsbDevices())
{
if (usbDevice.isUsbHub())
{
findDevices((UsbHub) usbDevice, devices);
}
else
{
checkDevice(usbDevice, devices);
}
}
} |
b6f49d9e-30a7-48b0-8e2d-eaa983f911f9 | private static void checkDevice(UsbDevice usbDevice,
List<AdbDevice> adbDevices)
{
UsbDeviceDescriptor deviceDesc = usbDevice.getUsbDeviceDescriptor();
// Ignore devices from Non-ADB vendors
if (!isAdbVendor(deviceDesc.idVendor())) return;
// Check interfaces of device
UsbConfiguration config = usbDevice.getActiveUsbConfiguration();
for (UsbInterface iface: (List<UsbInterface>) config.getUsbInterfaces())
{
List<UsbEndpoint> endpoints = iface.getUsbEndpoints();
// Ignore interface if it does not have two endpoints
if (endpoints.size() != 2) continue;
// Ignore interface if it does not match the ADB specs
if (!isAdbInterface(iface)) continue;
UsbEndpointDescriptor ed1 =
endpoints.get(0).getUsbEndpointDescriptor();
UsbEndpointDescriptor ed2 =
endpoints.get(1).getUsbEndpointDescriptor();
// Ignore interface if endpoints are not bulk endpoints
if (((ed1.bmAttributes() & UsbConst.ENDPOINT_TYPE_BULK) == 0) ||
((ed2.bmAttributes() & UsbConst.ENDPOINT_TYPE_BULK) == 0))
continue;
// Determine which endpoint is in and which is out. If both
// endpoints are in or out then ignore the interface
byte a1 = ed1.bEndpointAddress();
byte a2 = ed2.bEndpointAddress();
byte in, out;
if (((a1 & UsbConst.ENDPOINT_DIRECTION_IN) != 0) &&
((a2 & UsbConst.ENDPOINT_DIRECTION_IN) == 0))
{
in = a1;
out = a2;
}
else if (((a2 & UsbConst.ENDPOINT_DIRECTION_IN) != 0) &&
((a1 & UsbConst.ENDPOINT_DIRECTION_IN) == 0))
{
out = a1;
in = a2;
}
else continue;
// Create ADB device and add it to the list
AdbDevice adbDevice = new AdbDevice(iface, in, out);
adbDevices.add(adbDevice);
}
} |
e55cf690-43b4-4318-ba00-498716766b16 | private static boolean isAdbVendor(short vendorId)
{
for (short adbVendorId: Vendors.VENDOR_IDS)
if (adbVendorId == vendorId) return true;
return false;
} |
1fa826f5-bd2f-489c-902e-9bda998788ba | private static boolean isAdbInterface(UsbInterface iface)
{
UsbInterfaceDescriptor desc = iface.getUsbInterfaceDescriptor();
return desc.bInterfaceClass() == ADB_CLASS &&
desc.bInterfaceSubClass() == ADB_SUBCLASS &&
desc.bInterfaceProtocol() == ADB_PROTOCOL;
} |
507c7e71-34d1-4222-9284-a60db9300bb7 | public static RSAPrivateKey getPrivateKey() throws IOException,
GeneralSecurityException
{
File file =
new File(System.getProperty("user.home"), ".android/adbkey");
BufferedReader reader = new BufferedReader(new FileReader(file));
try
{
StringBuilder builder = new StringBuilder();
String line = reader.readLine();
while (line != null)
{
if (!line.startsWith("----")) builder.append(line);
line = reader.readLine();
}
byte[] bytes =
DatatypeConverter.parseBase64Binary(builder.toString());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(bytes);
return (RSAPrivateKey) keyFactory.generatePrivate(ks);
}
finally
{
reader.close();
}
} |
995834a6-a4cc-4f93-9999-9aeda0488945 | public static byte[] getPublicKey() throws IOException
{
File file =
new File(System.getProperty("user.home"), ".android/adbkey.pub");
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = new FileInputStream(file);
byte[] buffer = new byte[8192];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
out.write(0);
in.close();
out.close();
return out.toByteArray();
} |
721b4f58-e0b9-46e4-bb8e-ffed974b2c07 | public static byte[] signToken(byte[] token) throws IOException,
GeneralSecurityException
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
stream.write(headerOID);
stream.write(token);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, getPrivateKey());
return cipher.doFinal(stream.toByteArray());
} |
d0550dc4-b937-4bba-88d7-472f203d6557 | public CloseMessage(MessageHeader header, byte[] data)
{
super(header, data);
} |
085d43d1-d406-41dd-a2ee-5f1422581d8a | public CloseMessage(int localId, int remoteId)
{
super(MessageHeader.CMD_CLSE, localId, remoteId, new byte[0]);
} |
f2b275ed-d1f7-4fe0-b380-a82ffc805340 | public int getLocalId()
{
return this.header.getArg0();
} |
f07d92ae-15a1-4fc5-a8d5-a8bb24de6237 | public int getRemoteId()
{
return this.header.getArg1();
} |
b3f5b850-5820-4ffc-a76e-0767bdec9455 | @Override
public String toString()
{
return String.format("CLOSE(%d, %d)", getLocalId(), getRemoteId());
} |
9a5e8374-e506-450e-a8ec-80530dd31266 | public OkayMessage(MessageHeader header, byte[] data)
{
super(header, data);
} |
bca251d2-56d0-48b3-a0c2-fd2a20d53fb1 | public OkayMessage(int remoteId, int localId)
{
super(MessageHeader.CMD_OKAY, remoteId, localId, new byte[0]);
} |
a5957ef5-b4e2-438a-b1bb-1aa1f47d7f9f | public int getLocalId()
{
return this.header.getArg1();
} |
91fb2489-3447-43c8-9dd2-2edcf29fb74c | public int getRemoteId()
{
return this.header.getArg0();
} |
3ad2d13a-a15a-4586-979e-7ebd53c113a5 | @Override
public String toString()
{
return String.format("OKAY(%d, %d)", getRemoteId(), getLocalId());
} |
6eaddc96-cde0-4998-bc89-5fd2d1e81092 | public MessageHeader(int command, int arg0, int arg1, int dataLength,
int dataChecksum, int magic)
{
this.command = command;
this.arg0 = arg0;
this.arg1 = arg1;
this.dataLength = dataLength;
this.dataChecksum = dataChecksum;
this.magic = command ^ 0xffffffff;
} |
0c24b73f-39a1-4a23-b809-fbc0152aca32 | public MessageHeader(byte[] bytes)
{
if (bytes.length != SIZE)
throw new IllegalArgumentException("ADB message header must be "
+ SIZE + " bytes large, not " + bytes.length + " bytes");
ByteBuffer buffer = ByteBuffer.wrap(bytes).
order(ByteOrder.LITTLE_ENDIAN);
this.command = buffer.getInt();
this.arg0 = buffer.getInt();
this.arg1 = buffer.getInt();
this.dataLength = buffer.getInt();
this.dataChecksum = buffer.getInt();
this.magic = buffer.getInt();
} |
31b512f2-58e7-4e6c-9dbf-f125ba5a4b8c | public int getCommand()
{
return this.command;
} |
47fb47f3-8604-4e69-b8fc-df42a2e2adcb | public int getArg0()
{
return this.arg0;
} |
3f2d8ff5-fbd0-4880-8091-6ea1fedfcf1d | public int getArg1()
{
return this.arg1;
} |
2f62676b-f200-4d52-9488-6ade9e62df39 | public int getDataChecksum()
{
return this.dataChecksum;
} |
ab563203-1cb6-4cc1-96a6-6789fa9617e7 | public int getDataLength()
{
return this.dataLength;
} |
759ba05d-8e4a-4527-abce-d251a48d390c | public int getMagic()
{
return this.magic;
} |
dc7c6cae-0a39-4910-b6e2-2d2996189acb | public boolean isValid()
{
return this.magic == (this.command ^ 0xffffffff);
} |
6687859e-dc43-4141-bdc5-8485603d2742 | public byte[] getBytes()
{
ByteBuffer buffer = ByteBuffer.allocate(SIZE);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putInt(this.command);
buffer.putInt(this.arg0);
buffer.putInt(this.arg1);
buffer.putInt(this.dataLength);
buffer.putInt(this.dataChecksum);
buffer.putInt(this.magic);
return buffer.array();
} |
bceea065-7adf-4ff7-b1c0-8d6f03a6111a | public AuthMessage(MessageHeader header, byte[] data)
{
super(header, data);
} |
9279bd9f-712e-4b64-b0ea-2a045db00b4e | public AuthMessage(int type, byte[] data)
{
super(MessageHeader.CMD_AUTH, type, 0, data);
} |
a7c774b9-a31d-476a-a83e-97eca16f3911 | public int getType()
{
return this.header.getArg0();
} |
c52f1e47-b35c-4e0d-b6f0-269baabb4641 | @Override
public String toString()
{
return String.format("AUTH(%d, 0x%s)", getType(),
DatatypeConverter.printHexBinary(getData()));
} |
1051a11b-0088-4c62-a794-45e68d5afcf9 | public WriteMessage(MessageHeader header, byte[] data)
{
super(header, data);
} |
d4a94e6d-d9ce-400b-b774-7164c91ae3c1 | public WriteMessage(int remoteId, byte[] data)
{
super(MessageHeader.CMD_WRTE, remoteId, 0, data);
} |
19142893-0b93-423f-ad87-1b6aef6c70c9 | public WriteMessage(int remoteId, String data)
{
this(remoteId, (data + '\0').getBytes(Charset.forName("UTF-8")));
} |
1ead4e3c-084a-43af-9ac2-a0e2138c72f3 | public int getRemoteId()
{
return this.header.getArg1();
} |
9f8594d8-08f3-4c50-af36-742243d8a4f1 | public String getDataAsString()
{
int len = this.data.length;
while (len > 0 && this.data[len - 1] == 0) len--;
return new String(this.data, 0, len, Charset.forName("UTF-8"));
} |
90e33547-3a10-488d-b531-8279d4cf33d0 | @Override
public String toString()
{
return String.format("WRITE(%d, %s)", getRemoteId(),
DatatypeConverter.printHexBinary(getData()));
} |
6dc22ff8-d8a2-4df2-9a8b-f615321159c5 | public Long getEmployeeId() {
return employeeId;
} |
054bde44-5513-4ad9-8073-e437e347d4ab | public void setEmployeeId(Long employeeId) {
this.employeeId = employeeId;
} |
eac8911c-6325-4fee-bc52-7a5f8ff271a7 | public Double getWorkingHours() {
return workingHours;
} |
5ac610d9-c77e-4b34-8720-0542950b0804 | public void setWorkingHours(Double workingHours) {
this.workingHours = workingHours;
} |
c1fc9d57-34ad-4c99-9d46-d08f8e0e4d80 | public ReportService(Connection connection) {
this.connection = connection;
} |
2e31d05a-c74a-49fb-97eb-bec09cd96e9d | public List<ReportEntry> getReport(Long from, Long to) {
if (from == null) {
throw new IllegalArgumentException("report start date cannot be null");
}
if (to == null) {
throw new IllegalArgumentException("report end date cannot be null");
}
if (to < from) {
throw new IllegalArgumentException("Report end date must be after start date");
}
ResultSet results = null;
try {
if (preparedStatement == null) {
preparedStatement = connection.prepareStatement(REPORT_QUERY);
}
connection.setAutoCommit(false);
preparedStatement.setLong(1, from);
preparedStatement.setLong(2, to);
results = preparedStatement.executeQuery();
long t1 = System.currentTimeMillis();
List<ReportEntry> reportEntries = new LinkedList<ReportEntry>();
while (results.next()) {
ReportEntry reportEntry = new ReportEntry();
reportEntry.setEmployeeId(results.getLong(1));
reportEntry.setWorkingHours(results.getDouble(2));
reportEntries.add(reportEntry);
}
long t2 = System.currentTimeMillis();
long diff = t2 - t1;
System.out.println("diff = " + diff);
return reportEntries;
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
if (results != null) {
results.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
} |
cf8727ec-08cb-49c7-b817-f3a91c3c9148 | public byte[] generateReport(String templatePath, ReportType type, Long from, Long to) throws ServletException, IOException {
if (from == null) {
throw new IllegalArgumentException("report start date cannot be null");
}
if (to == null) {
throw new IllegalArgumentException("report end date cannot be null");
}
if (to < from) {
throw new IllegalArgumentException("Report end date must be after start date");
}
ResultSet results = null;
try {
if (preparedStatement == null) {
preparedStatement = connection.prepareStatement(REPORT_QUERY);
}
connection.setAutoCommit(false);
preparedStatement.setLong(1, from);
preparedStatement.setLong(2, to);
results = preparedStatement.executeQuery();
JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(results);
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("ReportTitle", "Capacity Per Employee");
JasperPrint jasperPrint = JasperFillManager.fillReport(templatePath, parameters, resultSetDataSource);
if (type == ReportType.PDF) {
JasperExportManager.exportReportToPdfStream(jasperPrint, os);
} else {
JRCsvExporter csvExporter = new JRCsvExporter();
csvExporter.setParameter(JRCsvExporterParameter.FIELD_DELIMITER, ",");
csvExporter.setParameter(JRCsvExporterParameter.RECORD_DELIMITER, "\n");
csvExporter.setParameter(JRCsvExporterParameter.JASPER_PRINT, jasperPrint);
csvExporter.setParameter(JRCsvExporterParameter.IGNORE_PAGE_MARGINS, true);
csvExporter.setParameter(JRCsvExporterParameter.OUTPUT_STREAM, os);
csvExporter.exportReport();
}
return os.toByteArray();
} catch (JRException e) {
e.printStackTrace();
throw new RuntimeException("Error occurred in report generation", e);
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
if (results != null) {
results.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
} |
7cd5d944-767b-4113-b2d5-264397726ce1 | @Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
plannerService = new ReportService(connection);
jasperReportTemplate = config.getInitParameter(JASPER_TEMPLATE_FILE);
if (jasperReportTemplate == null) {
throw new IllegalArgumentException("Jasper template cannot be null.");
}
} |
5fbbd09c-01c8-4a00-9055-45f2e527924b | @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String reportDir = getServletContext().getRealPath("/WEB-INF/classes");
String templatePath = reportDir + File.separator + jasperReportTemplate;
String reportType = request.getParameter("reportType");
ReportType type = reportType != null && reportType.equalsIgnoreCase("PDF") ? ReportType.PDF : ReportType.CSV;
Long startDate = Long.parseLong(request.getParameter("start")) / MS_PER_SEC;
Long endDate = Long.parseLong(request.getParameter("end")) / MS_PER_SEC;
byte[] report = plannerService.generateReport(templatePath, type, startDate, endDate);
response.setContentType(String.format("application/%s", type == ReportType.CSV ? "csv" : "pdf"));
response.addHeader("Content-Disposition", "attachment; filename=report."
+ (type == ReportType.CSV ? "csv" : "pdf"));
response.setContentLength(report.length);
OutputStream responseOutputStream = response.getOutputStream();
responseOutputStream.write(report);
} |
6d8da2a1-16a0-4d90-8650-0b16b0d09a26 | @Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
String applicationPath = getServletContext().getRealPath("/WEB-INF/classes");
String fileName = config.getInitParameter(CONFIG_FILE_PARAMETER);
databaseProperties = new Properties();
String configurationPath = applicationPath + File.separator + fileName;
try {
FileInputStream input = new FileInputStream(configurationPath);
databaseProperties.load(input);
} catch (FileNotFoundException e) {
throw new ServletException("Application config file is not found by following path " + configurationPath);
} catch (IOException e) {
throw new ServletException("Cannot load configuration properties");
}
try {
Class.forName(POSTGRES_DRIVER_CLASS);
} catch (ClassNotFoundException e) {
throw new ServletException("Postgres driver class is not found. Please check if it in the Class path.");
}
String host = databaseProperties.getProperty("bbdd.host");
if (host == null) {
host = "127.0.0.1";
}
String port = databaseProperties.getProperty("bbdd.port");
if (port == null) {
port = "5432";
}
String sid = databaseProperties.getProperty("bbdd.sid");
if (sid == null) {
throw new IllegalArgumentException("Database name property (bbdd.sid) can not be null");
}
String user = databaseProperties.getProperty("bbdd.user");
if (user == null) {
throw new IllegalArgumentException("Database username property (bbdd.user) can not be null");
}
String password = databaseProperties.getProperty("bbdd.password");
if (password == null) {
throw new IllegalArgumentException("Database password property (bbdd.password) can not be null");
}
String url = String.format(POSTGRES_URL_PATTERN, host, port, sid);
Properties props = new Properties();
props.setProperty("user", user);
props.setProperty("password", password);
try {
connection = DriverManager.getConnection(url, props);
} catch (SQLException e) {
throw new RuntimeException("Cannot connect to database", e);
}
} |
5080931e-2fc6-4587-8a3e-b79dccf5f54f | public static void main(String args[]) {
ApplicationContext apc = new ClassPathXmlApplicationContext("spring-config.xml");
ShapeService sp = apc.getBean("shapeService",ShapeService.class);
sp.getCircle().setName("PAPA");
System.out.println(sp.getCircle().getName());
System.out.println("==========PROGRAM END============");
/*System.out.println("==========================================");
System.out.println(sp.getTriangle().getName());*/
} |
d9fb1d46-21c4-49ed-b9e7-bd682407bcc3 | public String getName() {
return name;
} |
943cc873-1044-4626-a12b-bab7e924ede4 | public void setName(String name) {
this.name = name;
} |
f152e10a-2f14-423f-aee3-c8cdd79742d8 | public String getName() {
return name;
} |
7a978f16-826f-4799-adfb-87d5f6e2b106 | public void setName(String name) {
this.name = name;
} |
2d53e160-abb1-4d96-b3cf-7ce5b77d3798 | public Circle getCircle() {
return circle;
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.