/** * @param startByte * @param endByte * @return * @throws Exception * @return true if all the bytes between in the file between startByte and endByte are null, false * otherwise */ private boolean isFilePortionNull(int startByte, int endByte) throws IOException { logger.config("Checking file portion:" + Hex.asHex(startByte) + ":" + Hex.asHex(endByte)); FileInputStream fis = null; FileChannel fc = null; try { fis = new FileInputStream(file); fc = fis.getChannel(); fc.position(startByte); ByteBuffer bb = ByteBuffer.allocateDirect(endByte - startByte); fc.read(bb); while (bb.hasRemaining()) { if (bb.get() != 0) { return false; } } } finally { if (fc != null) { fc.close(); } if (fis != null) { fis.close(); } } return true; }
@Test public void shouldCaptureManyEnemies() { // given givenFl("☼☼☼☼☼" + "☼ ☻☼" + "☼ ☼" + "☼☺ ☼" + "☼☼☼☼☼"); joystick2.act(3, 3); joystick2.down(); game.tick(); joystick2.act(3, 2); joystick2.down(); game.tick(); assertE("☼☼☼☼☼" + "☼ ☻☼" + "☼ ☻☼" + "☼☺ ☻☼" + "☼☼☼☼☼"); // when joystick2.act(3, 3); joystick2.left(); joystick1.act(1, 1); joystick1.right(); game.tick(); // then assertE("☼☼☼☼☼" + "☼ ☻☻☼" + "☼ ☺☼" + "☼☺☺☺☼" + "☼☼☼☼☼"); }
/** Update. */ public void update() { Protocol p = device.getProtocol(); p.setDeviceParms(device.getValues()); updateParameters(); protocolList.setSelectedItem(p); if (protocolList.getSelectedItem() != p) { protocolList.addItem(p); protocolList.setSelectedItem(p); } fixedData.getDocument().removeDocumentListener(this); protocolID.getDocument().removeDocumentListener(this); deviceNotes.getDocument().removeDocumentListener(this); fixedData.setText(device.getFixedData().toString()); Hex id = p.getID(remote); if (id != null) protocolID.setText(id.toString()); else protocolID.setText(null); deviceNotes.setText(device.getNotes()); fixedData.getDocument().addDocumentListener(this); protocolID.getDocument().addDocumentListener(this); deviceNotes.getDocument().addDocumentListener(this); protocolNotes.setText(p.getNotes()); protocolNotes.setCaretPosition(0); }
void put(final URI uri, ArtifactData data) throws Exception { reporter.trace("put %s %s", uri, data); File tmp = createTempFile(repoDir, "mtp", ".whatever"); tmp.deleteOnExit(); try { copy(uri.toURL(), tmp); byte[] sha = SHA1.digest(tmp).digest(); reporter.trace("SHA %s %s", uri, Hex.toHexString(sha)); ArtifactData existing = get(sha); if (existing != null) { reporter.trace("existing"); xcopy(existing, data); return; } File meta = new File(repoDir, Hex.toHexString(sha) + ".json"); File file = new File(repoDir, Hex.toHexString(sha)); rename(tmp, file); reporter.trace("file %s", file); data.file = file.getAbsolutePath(); data.sha = sha; data.busy = false; CommandData cmddata = parseCommandData(data); if (cmddata.bsn != null) { data.name = cmddata.bsn + "-" + cmddata.version; } else data.name = Strings.display(cmddata.title, cmddata.bsn, cmddata.name, uri); codec.enc().to(meta).put(data); reporter.trace("TD = " + data); } finally { tmp.delete(); reporter.trace("puted %s %s", uri, data); } }
public static void testEncode(String input, String encoding) throws XcodeException { String inputdecoded = null; String output = null; String variant = null; if (input == null) { Debug.pass(""); return; } input = input.trim(); if (input.length() == 0 || input.charAt(0) == '#') { Debug.pass(input); return; } try { inputdecoded = new String(Hex.decodeChars(input)); output = Native.encode(inputdecoded, encoding); } catch (XcodeException x) { Debug.fail(input + " ERROR:" + x.getCode() + " " + x.getMessage()); return; } System.out.println(Hex.encode(output.toCharArray())); }
/** * Place rebels, fill hexes starting from closest available hex. Return true if all rebels placed, * false if we run out of planet hexes before all rebels are placed. * * @param rebels list of units to place * @return true iff all rebels placed */ private boolean placeRebels(LinkedList<Unit> rebels, Util.FindHexesAround.Hextype type) { Hex center = null; Hex target = null; Hex prev_target = null; Util.FindHexesAround hex_finder = null; Point p = new Point(C.NEUTRAL, C.NEUTRAL); int p_idx = -1; int x = -1; int y = -1; while (!rebels.isEmpty()) { Unit unit = rebels.pop(); if (unit.y != y || unit.x != x || unit.p_idx != p_idx) { y = unit.y; x = unit.x; p_idx = unit.p_idx; addMessage( new Message( "Rebellion on " + game.getPlanet(p_idx).name + " " + x + "," + y + "!", C.Msg.REBELLION, game.getYear(), game.getPlanet(p_idx))); } Hex hex_tmp = game.getHexFromPXY(unit.p_idx, unit.x, unit.y); if (!hex_tmp.equals(center)) { center = hex_tmp; hex_finder = new Util.FindHexesAround(center, C.NEUTRAL, type, game.getPlanet(p_idx).tile_set_type); prev_target = target; target = hex_finder.next(); } while (target != null && Util.stackSize(target.getStack()) >= C.STACK_SIZE) { target = hex_finder.next(); } prev_target = spot(prev_target, target); if (target == null) { return false; } if (unit.carrier == null) { if (unit.type_data.cargo > 0 && unit.cargo_list.size() > 0) { List<Unit> tmp = new LinkedList<>(); tmp.addAll(unit.cargo_list); for (Unit u : tmp) { unit.disembark(u); center.addUnit(u); } } } else { Unit u = unit.carrier; u.disembark(unit); } center.getStack().remove(unit); game.changeOwnerOfUnit(p, unit); game.relocateUnit(false, unit.p_idx, target.getX(), target.getY(), unit); target.addUnit(unit); game.getUnits().add(unit); // Util.dP(" hex " + target.getX() + "," + target.getY()); } spot(target, prev_target); return true; }
private Hex spot(Hex prev_target, Hex target) { if (prev_target != null && !prev_target.equals(target)) { List<Unit> tmp = prev_target.getStack(); // Util.dP("spot " + prev_target.getX() + "," + prev_target.getY() + " " + tmp.size() + " " + // Util.getFactionName(tmp.get(0).owner)); game.getHexProc().spotProc(prev_target, tmp); prev_target = target; } return prev_target; }
/** * Turn the shas into a readable form * * @param dependencies * @return * @throws Exception */ public List<?> toString(List<byte[]> dependencies) throws Exception { List<String> out = new ArrayList<String>(); for (byte[] dependency : dependencies) { ArtifactData data = get(dependency); if (data == null) out.add(Hex.toHexString(dependency)); else { out.add(Strings.display(data.name, Hex.toHexString(dependency))); } } return out; }
@Test public void shouldSplitLeftAndRight() { givenFl("☼☼☼☼☼" + "☼ ☼" + "☼ ☺ ☼" + "☼ ☼" + "☼☼☼☼☼"); joystick1.act(2, 2); joystick1.left(); game.tick(); assertE("☼☼☼☼☼" + "☼ ☼" + "☼☺☺ ☼" + "☼ ☼" + "☼☼☼☼☼"); joystick1.act(2, 2); joystick1.right(); game.tick(); assertE("☼☼☼☼☼" + "☼ ☼" + "☼☺☺☺☼" + "☼ ☼" + "☼☼☼☼☼"); }
public static byte[] getDecodedId(String groupId) throws IOException { if (!isEncodedGroup(groupId)) { throw new IOException("Invalid encoding"); } return Hex.fromStringCondensed(groupId.split("!", 2)[1]); }
public ArtifactData getCandidateAsync(String arg) throws Exception { reporter.trace("coordinate %s", arg); if (isUrl(arg)) try { ArtifactData data = putAsync(new URI(arg)); data.local = true; return data; } catch (Exception e) { reporter.trace("hmm, not a valid url %s, will try the server", arg); } Coordinate c = new Coordinate(arg); if (c.isSha()) { ArtifactData r = get(c.getSha()); if (r != null) return r; } Revision revision = library.getRevisionByCoordinate(c); if (revision == null) return null; reporter.trace("revision %s", Hex.toHexString(revision._id)); ArtifactData ad = get(revision._id); if (ad != null) { reporter.trace("found in cache"); return ad; } URI url = revision.urls.iterator().next(); ArtifactData artifactData = putAsync(url); artifactData.coordinate = c; return artifactData; }
private String createMac(Key secKey, String ciphertext) throws GeneralSecurityException { Mac mac = Mac.getInstance("HMACSHA256", PROVIDER_NAME); // mac.init(new SecretKeySpec(Base64.encode(secKey.getEncoded()), "AES")); mac.init(secKey); byte[] hmacBytes = mac.doFinal(WeaveUtil.toAsciiBytes(ciphertext)); return WeaveUtil.toAsciiString(Hex.encode(hmacBytes)); }
@Test public void shouldSecondPlayerMoveDownAndFirstUp() { givenFl("☼☼☼☼☼" + "☼ ☻☼" + "☼ ☼" + "☼☺ ☼" + "☼☼☼☼☼"); joystick2.act(3, 3); joystick2.down(); game.tick(); assertE("☼☼☼☼☼" + "☼ ☻☼" + "☼ ☻☼" + "☼☺ ☼" + "☼☼☼☼☼"); joystick1.act(1, 1); joystick1.up(); game.tick(); assertE("☼☼☼☼☼" + "☼ ☻☼" + "☼☺ ☻☼" + "☼☺ ☼" + "☼☼☼☼☼"); }
@Test public void shouldSplitLeftThenUp() { givenFl("☼☼☼☼☼" + "☼ ☼" + "☼ ☺ ☼" + "☼ ☼" + "☼☼☼☼☼"); joystick1.act(2, 2); joystick1.left(); game.tick(); assertE("☼☼☼☼☼" + "☼ ☼" + "☼☺☺ ☼" + "☼ ☼" + "☼☼☼☼☼"); joystick1.act(1, 2); joystick1.up(); game.tick(); assertE("☼☼☼☼☼" + "☼☺ ☼" + "☼☺☺ ☼" + "☼ ☼" + "☼☼☼☼☼"); }
@Test public void shouldHitTheWall() { givenFl("☼☼☼☼☼" + "☼ ☼" + "☼ ☺ ☼" + "☼ ☼" + "☼☼☼☼☼"); joystick1.act(2, 2); joystick1.left(); game.tick(); assertE("☼☼☼☼☼" + "☼ ☼" + "☼☺☺ ☼" + "☼ ☼" + "☼☼☼☼☼"); joystick1.act(1, 2); joystick1.left(); game.tick(); assertE("☼☼☼☼☼" + "☼ ☼" + "☼☺☺ ☼" + "☼ ☼" + "☼☼☼☼☼"); }
public static byte[] hexDecode(String s) { try { return Hex.decodeHex(s.toCharArray()); } catch (DecoderException e) { throw new ClientProblemException( "Hex content is not valid hex: " + e.getMessage() + ": " + s); } }
@Test public void shouldFieldAtStart() { givenFl("☼☼☼☼☼" + "☼ ☼" + "☼ ☺ ☼" + "☼ ☼" + "☼☼☼☼☼"); game.tick(); assertE("☼☼☼☼☼" + "☼ ☼" + "☼ ☺ ☼" + "☼ ☼" + "☼☼☼☼☼"); }
@Override public String toString() { final StringBuilder sb = new StringBuilder(); sb.append("ExtensionDescriptor"); sb.append("{bytes=").append(bytes == null ? "null" : Hex.encodeHex(bytes)); sb.append('}'); return sb.toString(); }
@Test public void shouldTwoPlayersOnBoard() { givenFl("☼☼☼☼☼" + "☼ ☻☼" + "☼ ☼" + "☼☺ ☼" + "☼☼☼☼☼"); game.tick(); assertE("☼☼☼☼☼" + "☼ ☻☼" + "☼ ☼" + "☼☺ ☼" + "☼☼☼☼☼"); }
@Test public void testEncodesMultilineByteArray() { byte input[] = "the quick\r\nbrown fox\n\njumps over the\nlazy dog".getBytes(); String expected = "74686520717569636B0D0A62726F776E20666F78" + "0A0A6A756D7073206F766572207468650A6C617A7920646F67"; assertEquals(expected, Hex.toHex(input)); }
private String operation( String serialNumber, String head, int length, short l, String agreement, ByteBuf in, ChannelHandlerContext ctx) throws ParseException { String content = null; if ("7101".equals(agreement) && length == l) { content = byteToArray(in.readBytes(l - 17).array()); } else if ("5100".equals(agreement) || "4106".equals(agreement) || "9965".equals(agreement) || "3104".equals(agreement) || "2001".equals(agreement) || "2002".equals(agreement) || "2003".equals(agreement) || "9990".equals(agreement) || "9992".equals(agreement) && length == l) { byteToArray(in.readBytes(1).array()); content = byteToArray(in.readBytes(l - 18).array()); String info = new String(Hex.decodeHex(content.toCharArray())); System.out.println(content); System.out.println(info); } else if ("9991".equals(agreement) && "$$".equals(head) && length == l) { content = byteToArray(in.readBytes(l - 17).array()); } else if ("9982".equals(agreement) && "$$".equals(head) && length == l) { content = byteToArray(in.readBytes(l - 17).array()); } else if ("4130".equals(agreement) && "$$".equals(head)) { byteToArray(in.readBytes(1).array()); content = byteToArray(in.readBytes(l - 18).array()); } else { content = byteToArray(in.readBytes(l - 17).array()); String info = new String(Hex.decodeHex(content.toCharArray())); System.out.println(content); System.out.println(info); } return content; }
/** * Makes sure that the response contains an unfiltered file contents. * * <p>This is used to test exclusions and passthroughs in the GzipFilter. * * <p>An example is to test that it is possible to configure GzipFilter to not recompress content * that shouldn't be compressed by the GzipFilter. * * @param requestedFilename the filename used to on the GET request,. * @param testResourceSha1Sum the sha1sum file that contains the SHA1SUM checksum that will be * used to verify that the response contents are what is intended. * @param expectedContentType */ public void assertIsResponseNotGzipFiltered( String requestedFilename, String testResourceSha1Sum, String expectedContentType) throws Exception { System.err.printf("[GzipTester] requesting /context/%s%n", requestedFilename); HttpTester request = new HttpTester(); HttpTester response = new HttpTester(); request.setMethod("GET"); request.setVersion("HTTP/1.0"); request.setHeader("Host", "tester"); request.setHeader("Accept-Encoding", compressionType); if (this.userAgent != null) request.setHeader("User-Agent", this.userAgent); request.setURI("/context/" + requestedFilename); // Issue the request ByteArrayBuffer reqsBuff = new ByteArrayBuffer(request.generate().getBytes()); // Collect the response(s) ByteArrayBuffer respBuff = servletTester.getResponses(reqsBuff); response.parse(respBuff.asArray()); dumpHeaders(requestedFilename + " / Response Headers", response); // Assert the response headers String prefix = requestedFilename + " / Response"; Assert.assertThat(prefix + ".method", response.getMethod(), nullValue()); Assert.assertThat(prefix + ".status", response.getStatus(), is(HttpServletResponse.SC_OK)); Assert.assertThat( prefix + ".header[Content-Length]", response.getHeader("Content-Length"), notNullValue()); Assert.assertThat( prefix + ".header[Content-Encoding] (should not be recompressed by GzipFilter)", response.getHeader("Content-Encoding"), nullValue()); Assert.assertThat( prefix + ".header[Content-Type] (should have a Content-Type associated with it)", response.getHeader("Content-Type"), notNullValue()); Assert.assertThat( prefix + ".header[Content-Type]", response.getHeader("Content-Type"), is(expectedContentType)); ByteArrayInputStream bais = null; DigestOutputStream digester = null; try { MessageDigest digest = MessageDigest.getInstance("SHA1"); bais = new ByteArrayInputStream(response.getContentBytes()); digester = new DigestOutputStream(new NoOpOutputStream(), digest); IO.copy(bais, digester); String actualSha1Sum = Hex.asHex(digest.digest()); String expectedSha1Sum = loadExpectedSha1Sum(testResourceSha1Sum); Assert.assertEquals( requestedFilename + " / SHA1Sum of content", expectedSha1Sum, actualSha1Sum); } finally { IO.close(digester); IO.close(bais); } }
@Test public void shouldHitTheHero() { givenFl("☼☼☼☼☼" + "☼ ☼" + "☼ ☺ ☼" + "☼ ☼" + "☼☼☼☼☼"); joystick1.act(2, 2); joystick1.left(); game.tick(); assertE("☼☼☼☼☼" + "☼ ☼" + "☼☺☺ ☼" + "☼ ☼" + "☼☼☼☼☼"); joystick1.act(1, 2); joystick1.right(); game.tick(); assertE("☼☼☼☼☼" + "☼ ☼" + "☼☺☺ ☼" + "☼ ☼" + "☼☼☼☼☼"); assertEquals(2, player1.heroes.size()); }
private void setupPlayer2() { listener2 = mock(EventListener.class); player2 = new Player(listener2, game); joystick2 = player2.getJoystick(); dice(hero2.getX(), hero2.getY()); game.newGame(player2); hero2.init(game); }
@Test public void shouldSplitDownWhenGoDown() { givenFl("☼☼☼☼☼" + "☼ ☼" + "☼ ☺ ☼" + "☼ ☼" + "☼☼☼☼☼"); joystick1.act(2, 2); joystick1.down(); game.tick(); assertE("☼☼☼☼☼" + "☼ ☼" + "☼ ☺ ☼" + "☼ ☺ ☼" + "☼☼☼☼☼"); }
@Test public void shouldKillWhenPlayerMoveTowardsStanding() { // given givenFl("☼☼☼☼☼" + "☼ ☻☼" + "☼ ☼" + "☼☺ ☼" + "☼☼☼☼☼"); joystick2.act(3, 3); joystick2.down(); game.tick(); assertE("☼☼☼☼☼" + "☼ ☻☼" + "☼ ☻☼" + "☼☺ ☼" + "☼☼☼☼☼"); // when joystick1.act(1, 1); joystick1.right(); game.tick(); // then assertE("☼☼☼☼☼" + "☼ ☻☼" + "☼ ☺☼" + "☼☺☺ ☼" + "☼☼☼☼☼"); }
private void setupPlayer1() { listener1 = mock(EventListener.class); player1 = new Player(listener1, game); joystick1 = player1.getJoystick(); dice(hero1.getX(), hero1.getY()); game.newGame(player1); hero1.init(game); }
@Test public void shouldSplitUpWhenGoUp() { givenFl("☼☼☼☼☼" + "☼ ☼" + "☼ ☺ ☼" + "☼ ☼" + "☼☼☼☼☼"); joystick1.act(2, 2); joystick1.up(); game.tick(); assertE("☼☼☼☼☼" + "☼ ☺ ☼" + "☼ ☺ ☼" + "☼ ☼" + "☼☼☼☼☼"); }
/* (non-Javadoc) * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == protocolList) { Protocol newProtocol = (Protocol) protocolList.getSelectedItem(); Protocol oldProtocol = device.getProtocol(); if (newProtocol != oldProtocol) { oldProtocol.reset(); protocolID.getDocument().removeDocumentListener(this); Hex id = newProtocol.getID(remote); if (id != null) protocolID.setText(id.toString()); else protocolID.setText(null); protocolID.getDocument().addDocumentListener(this); device.setProtocol(newProtocol); updateParameters(); fixedData.getDocument().removeDocumentListener(this); fixedData.setText(newProtocol.getFixedData(newProtocol.getDeviceParmValues()).toString()); fixedData.getDocument().addDocumentListener(this); boolean flag = (newProtocol.getClass() == ManualProtocol.class); fixedData.setEditable(flag); protocolID.setEditable(flag); validate(); protocolNotes.setText(newProtocol.getNotes()); protocolNotes.setCaretPosition(0); protocolNotes.revalidate(); } } if (source == cancelButton) { userAction = JOptionPane.CANCEL_OPTION; setVisible(false); removeParameters(parameters); device.getProtocol().reset(); dispose(); } else if (source == okButton) { userAction = JOptionPane.OK_OPTION; setVisible(false); removeParameters(parameters); device.getProtocol().reset(); dispose(); } else // must be a protocol parameter updateFixedData(); } // actionPerformed
private void callObjectAtLocation(int x, int y) { for (Hex o : _hexes) { if (Math.sqrt((o.getX() - x) * (o.getX() - x) + (o.getY() - y) * (o.getY() - y)) < o.getRadius()) { o.clicked(x, y); return; } } }