Example usage for javax.xml.bind DatatypeConverter parseBase64Binary

List of usage examples for javax.xml.bind DatatypeConverter parseBase64Binary

Introduction

In this page you can find the example usage for javax.xml.bind DatatypeConverter parseBase64Binary.

Prototype

public static byte[] parseBase64Binary(String lexicalXSDBase64Binary) 

Source Link

Document

Converts the string argument into an array of bytes.

Usage

From source file:tectonicus.raw.RawChunk.java

private void init(InputStream in, Compression compression) throws Exception {
    clear();/*from  ww  w  .j a v a  2  s .  co m*/

    NBTInputStream nbtIn = null;
    try {
        nbtIn = new NBTInputStream(in, compression);

        Tag tag = nbtIn.readTag();
        if (tag instanceof CompoundTag) {
            CompoundTag root = (CompoundTag) tag;

            CompoundTag level = NbtUtil.getChild(root, "Level", CompoundTag.class);
            if (level != null) {
                blockX = blockY = blockZ = 0;

                IntTag xPosTag = NbtUtil.getChild(level, "xPos", IntTag.class);
                if (xPosTag != null)
                    blockX = xPosTag.getValue().intValue();

                IntTag zPosTag = NbtUtil.getChild(level, "zPos", IntTag.class);
                if (zPosTag != null)
                    blockZ = zPosTag.getValue().intValue();

                ListTag sections = NbtUtil.getChild(level, "Sections", ListTag.class);
                if (sections != null) {
                    // Parse as anvil format
                    parseAnvilData(level);
                } else {
                    // Parse as McRegion format
                    parseMcRegionData(level);
                }

                ListTag entitiesTag = NbtUtil.getChild(level, "Entities", ListTag.class);
                if (entitiesTag != null) {
                    for (Tag t : entitiesTag.getValue()) {
                        if (t instanceof CompoundTag) {
                            CompoundTag entity = (CompoundTag) t;

                            StringTag idTag = NbtUtil.getChild(entity, "id", StringTag.class);
                            if (idTag.getValue().endsWith("Painting")) {
                                StringTag motiveTag = NbtUtil.getChild(entity, "Motive", StringTag.class);
                                IntTag xTag = NbtUtil.getChild(entity, "TileX", IntTag.class);
                                IntTag yTag = NbtUtil.getChild(entity, "TileY", IntTag.class);
                                IntTag zTag = NbtUtil.getChild(entity, "TileZ", IntTag.class);
                                ByteTag oldDir = NbtUtil.getChild(entity, "Dir", ByteTag.class);
                                ByteTag dir = NbtUtil.getChild(entity, "Direction", ByteTag.class);
                                if (oldDir != null && dir == null) {
                                    dir = oldDir;
                                }

                                boolean is18 = false;
                                if (dir == null) {
                                    dir = NbtUtil.getChild(entity, "Facing", ByteTag.class);
                                    is18 = true;
                                }

                                int direction = dir.getValue(); // Have to reverse 0 and 2 for the old Dir tag
                                if (oldDir != null && direction == 0) {
                                    direction = 2;
                                } else if (oldDir != null && direction == 2) {
                                    direction = 0;
                                }

                                int x = xTag.getValue();
                                final int y = yTag.getValue();
                                int z = zTag.getValue();

                                if (is18 && direction == 0) {
                                    z = zTag.getValue() - 1;
                                } else if (is18 && direction == 1) {
                                    x = xTag.getValue() + 1;
                                } else if (is18 && direction == 2) {
                                    z = zTag.getValue() + 1;
                                } else if (is18 && direction == 3) {
                                    x = xTag.getValue() - 1;
                                }

                                final int localX = x - (blockX * WIDTH);
                                final int localY = y - (blockY * HEIGHT);
                                final int localZ = z - (blockZ * DEPTH);

                                //System.out.println("Motive: " + motiveTag.getValue() + " Direction: " + dir.getValue() + " XYZ: " + x + ", " + y + ", " + z + " Local XYZ: " + localX +
                                //", " + localY + ", " + localZ);
                                paintings.add(new TileEntity(-1, 0, x, y, z, localX, localY, localZ,
                                        motiveTag.getValue(), direction));
                            } else if (idTag.getValue().equals("ItemFrame")) {
                                IntTag xTag = NbtUtil.getChild(entity, "TileX", IntTag.class);
                                IntTag yTag = NbtUtil.getChild(entity, "TileY", IntTag.class);
                                IntTag zTag = NbtUtil.getChild(entity, "TileZ", IntTag.class);
                                ByteTag dir = NbtUtil.getChild(entity, "Direction", ByteTag.class);
                                boolean is18 = false;
                                if (dir == null) {
                                    dir = NbtUtil.getChild(entity, "Facing", ByteTag.class);
                                    is18 = true;
                                }

                                String item = "";
                                Map<String, Tag> map = entity.getValue();
                                CompoundTag itemTag = (CompoundTag) map.get("Item");
                                if (itemTag != null) {
                                    ShortTag itemIdTag = NbtUtil.getChild(itemTag, "id", ShortTag.class);
                                    if (itemIdTag == null) {
                                        StringTag stringItemIdTag = NbtUtil.getChild(itemTag, "id",
                                                StringTag.class);
                                        item = stringItemIdTag.getValue();
                                    } else {
                                        if (itemIdTag.getValue() == 358)
                                            item = "minecraft:filled_map";
                                    }
                                }

                                int x = xTag.getValue();
                                final int y = yTag.getValue();
                                int z = zTag.getValue();

                                if (is18 && dir.getValue() == 0) {
                                    z = zTag.getValue() - 1;
                                } else if (is18 && dir.getValue() == 1) {
                                    x = xTag.getValue() + 1;
                                } else if (is18 && dir.getValue() == 2) {
                                    z = zTag.getValue() + 1;
                                } else if (is18 && dir.getValue() == 3) {
                                    x = xTag.getValue() - 1;
                                }

                                final int localX = x - (blockX * WIDTH);
                                final int localY = y - (blockY * HEIGHT);
                                final int localZ = z - (blockZ * DEPTH);

                                //System.out.println(" Direction: " + dir.getValue() + " XYZ: " + x + ", " + y + ", " + z + " Local XYZ: " + localX +
                                //", " + localY + ", " + localZ);

                                itemFrames.add(new TileEntity(-2, 0, x, y, z, localX, localY, localZ, item,
                                        dir.getValue()));
                            }
                        }
                    }
                }

                ListTag tileEntitiesTag = NbtUtil.getChild(level, "TileEntities", ListTag.class);
                if (tileEntitiesTag != null) {
                    for (Tag t : tileEntitiesTag.getValue()) {
                        if (t instanceof CompoundTag) {
                            CompoundTag entity = (CompoundTag) t;

                            StringTag idTag = NbtUtil.getChild(entity, "id", StringTag.class);
                            IntTag xTag = NbtUtil.getChild(entity, "x", IntTag.class);
                            IntTag yTag = NbtUtil.getChild(entity, "y", IntTag.class);
                            IntTag zTag = NbtUtil.getChild(entity, "z", IntTag.class);

                            if (idTag != null && xTag != null && yTag != null && zTag != null) {
                                String id = idTag.getValue();
                                if (id.equals("Sign")) {
                                    String text1 = NbtUtil.getChild(entity, "Text1", StringTag.class)
                                            .getValue();
                                    String text2 = NbtUtil.getChild(entity, "Text2", StringTag.class)
                                            .getValue();
                                    String text3 = NbtUtil.getChild(entity, "Text3", StringTag.class)
                                            .getValue();
                                    String text4 = NbtUtil.getChild(entity, "Text4", StringTag.class)
                                            .getValue();

                                    if (!text1.isEmpty() && FileUtils.isJSONValid(text1)) {
                                        text1 = text1.replaceAll("^[{]|[}]$", "").split(":", 2)[1];
                                        text2 = text2.replaceAll("^[{]|[}]$", "").split(":", 2)[1];
                                        text3 = text3.replaceAll("^[{]|[}]$", "").split(":", 2)[1];
                                        text4 = text4.replaceAll("^[{]|[}]$", "").split(":", 2)[1];
                                    }

                                    text1 = text1.replaceAll("^\"|\"$", ""); //This regex removes begin and end double quotes
                                    if (text1 == null || text1.equals("null"))
                                        text1 = "";

                                    text2 = text2.replaceAll("^\"|\"$", "");
                                    if (text2 == null || text2.equals("null"))
                                        text2 = "";

                                    text3 = text3.replaceAll("^\"|\"$", "");
                                    if (text3 == null || text3.equals("null"))
                                        text3 = "";

                                    text4 = text4.replaceAll("^\"|\"$", "");
                                    if (text4 == null || text4.equals("null"))
                                        text4 = "";

                                    final int x = xTag.getValue();
                                    final int y = yTag.getValue();
                                    final int z = zTag.getValue();

                                    final int localX = x - (blockX * WIDTH);
                                    final int localY = y - (blockY * HEIGHT);
                                    final int localZ = z - (blockZ * DEPTH);

                                    final int blockId = getBlockId(localX, localY, localZ);
                                    final int data = getBlockData(localX, localY, localZ);

                                    signs.add(new RawSign(blockId, data, x, y, z, localX, localY, localZ, text1,
                                            text2, text3, text4));
                                } else if (id.equals("FlowerPot")) {
                                    IntTag dataTag = NbtUtil.getChild(entity, "Data", IntTag.class);
                                    IntTag itemTag = NbtUtil.getChild(entity, "Item", IntTag.class);
                                    final int item;
                                    if (itemTag == null) {
                                        StringTag stringIdTag = NbtUtil.getChild(entity, "Item",
                                                StringTag.class);
                                        if (stringIdTag.getValue().equals("minecraft:sapling"))
                                            item = 6;
                                        else if (stringIdTag.getValue().equals("minecraft:red_flower"))
                                            item = 38;
                                        else
                                            item = 0;
                                    } else {
                                        item = itemTag.getValue();
                                    }

                                    final int x = xTag.getValue();
                                    final int y = yTag.getValue();
                                    final int z = zTag.getValue();

                                    final int localX = x - (blockX * WIDTH);
                                    final int localY = y - (blockY * HEIGHT);
                                    final int localZ = z - (blockZ * DEPTH);

                                    final int blockData = getBlockData(localX, localY, localZ);

                                    final int itemData = dataTag.getValue();

                                    flowerPots.add(new TileEntity(0, blockData, x, y, z, localX, localY, localZ,
                                            itemData, item));
                                } else if (id.equals("Skull")) {
                                    ByteTag skullType = NbtUtil.getChild(entity, "SkullType", ByteTag.class);
                                    ByteTag rot = NbtUtil.getChild(entity, "Rot", ByteTag.class);

                                    StringTag nameTag = null;
                                    StringTag playerId = null;
                                    String name = "";
                                    String UUID = "";
                                    String textureURL = "";
                                    StringTag extraType = NbtUtil.getChild(entity, "ExtraType",
                                            StringTag.class);
                                    CompoundTag owner = NbtUtil.getChild(entity, "Owner", CompoundTag.class);
                                    if (owner != null) {
                                        nameTag = NbtUtil.getChild(owner, "Name", StringTag.class);
                                        name = nameTag.getValue();
                                        playerId = NbtUtil.getChild(owner, "Id", StringTag.class);
                                        UUID = playerId.getValue().replace("-", "");

                                        // Get skin URL
                                        CompoundTag properties = NbtUtil.getChild(owner, "Properties",
                                                CompoundTag.class);
                                        ListTag textures = NbtUtil.getChild(properties, "textures",
                                                ListTag.class);
                                        CompoundTag tex = NbtUtil.getChild(textures, 0, CompoundTag.class);
                                        StringTag value = NbtUtil.getChild(tex, "Value", StringTag.class);
                                        byte[] decoded = DatatypeConverter.parseBase64Binary(value.getValue());
                                        JSONObject obj = new JSONObject(new String(decoded, "UTF-8"));
                                        textureURL = obj.getJSONObject("textures").getJSONObject("SKIN")
                                                .getString("url");
                                    } else if (extraType != null && !(extraType.getValue().equals(""))) {
                                        name = UUID = extraType.getValue();
                                        textureURL = "http://www.minecraft.net/skin/" + extraType.getValue()
                                                + ".png";
                                    }

                                    final int x = xTag.getValue();
                                    final int y = yTag.getValue();
                                    final int z = zTag.getValue();

                                    final int localX = x - (blockX * WIDTH);
                                    final int localY = y - (blockY * HEIGHT);
                                    final int localZ = z - (blockZ * DEPTH);

                                    skulls.add(new TileEntity(skullType.getValue(), rot.getValue(), x, y, z,
                                            localX, localY, localZ, name, UUID, textureURL, null));
                                } else if (id.equals("Beacon")) {
                                    IntTag levels = NbtUtil.getChild(entity, "Levels", IntTag.class);

                                    final int x = xTag.getValue();
                                    final int y = yTag.getValue();
                                    final int z = zTag.getValue();

                                    final int localX = x - (blockX * WIDTH);
                                    final int localY = y - (blockY * HEIGHT);
                                    final int localZ = z - (blockZ * DEPTH);

                                    beacons.add(new TileEntity(0, levels.getValue(), x, y, z, localX, localY,
                                            localZ, 0, 0));
                                } else if (id.equals("Banner")) {
                                    IntTag base = NbtUtil.getChild(entity, "Base", IntTag.class);
                                    ListTag patternList = NbtUtil.getChild(entity, "Patterns", ListTag.class);

                                    final int x = xTag.getValue();
                                    final int y = yTag.getValue();
                                    final int z = zTag.getValue();

                                    final int localX = x - (blockX * WIDTH);
                                    final int localY = y - (blockY * HEIGHT);
                                    final int localZ = z - (blockZ * DEPTH);

                                    String patterns = "";
                                    final int numPatterns = patternList.getValue().size();
                                    if (numPatterns > 0) {
                                        //System.out.println(patternList + "\n");
                                        patterns += "{";
                                        for (int i = 0; i < numPatterns; i++) {
                                            CompoundTag p = NbtUtil.getChild(patternList, i, CompoundTag.class);
                                            StringTag pattern = NbtUtil.getChild(p, "Pattern", StringTag.class);
                                            IntTag color = NbtUtil.getChild(p, "Color", IntTag.class);
                                            patterns += "\"" + pattern.getValue() + "\"" + ": "
                                                    + color.getValue().toString();
                                            if (i < numPatterns - 1)
                                                patterns += ", ";
                                        }
                                        patterns += "}";
                                        //System.out.println(patterns);
                                    }
                                    banners.add(new TileEntity(0, base.getValue(), x, y, z, localX, localY,
                                            localZ, patterns, 0));
                                    //banners.add(new TileEntity(0, base.getValue(), x, y, z, localX, localY, localZ, 0, 0));
                                }
                                //   else if (id.equals("Furnace"))
                                //   {
                                //      
                                //   }
                                //   else if (id.equals("MobSpawner"))
                                //   {
                                //      
                                //   }
                                //   else if (id.equals("Chest"))
                                //   {
                                //      
                                //   }
                            }
                        }
                    }
                }

                //   LongTag lastUpdateTag =
                //      NbtUtil.getChild(level, "LastUpdate", LongTag.class);

                //   ByteTag terrainPopulatedTag = 
                //      NbtUtil.getChild(level, "TerrainPopulated", ByteTag.class);
            }
        }
    } finally {
        if (nbtIn != null)
            nbtIn.close();
        if (in != null)
            in.close();
    }

    /* Old debug: put bricks in the corner of every chunk
    for (int y=0; y<HEIGHT; y++)
    {
       if (blockIds[0][y][0] != BlockIds.AIR)
       {
    if (signs.size() > 0)
       blockIds[0][y][0] = BlockIds.DIAMOND_BLOCK;
    else
       blockIds[0][y][0] = BlockIds.BRICK;
       }
    }
    */
}

From source file:tectonicus.world.World.java

public static ArrayList<Player> loadPlayers(File worldDir, PlayerSkinCache playerSkinCache) {
    File playersDir = Minecraft.findPlayersDir(worldDir);

    System.out.println("Loading players from " + playersDir.getAbsolutePath());

    ArrayList<Player> players = new ArrayList<Player>();
    File[] playerFiles = playersDir.listFiles();
    if (playerFiles != null) {
        for (File playerFile : playerFiles) {
            if (playerFile.getName().endsWith(".dat")) {
                try {
                    Player player = new Player(playerFile);

                    CacheEntry ce = playerSkinCache.getCacheEntry(player.getUUID());
                    if (ce != null) {
                        final long age = System.currentTimeMillis() - ce.fetchedTime;
                        if (age < 1000 * 60 * 60 * 60) // one hour in ms
                        {//from  ww w  .j  a  v  a  2  s .  com
                            player.setName(ce.playerName);
                            player.setSkinURL(ce.skinURL);
                        }
                    } else {
                        if (player.getUUID().equals(player.getName())) {
                            player.setSkinURL("http://www.minecraft.net/skin/" + player.getName() + ".png");
                        } else {
                            String urlString = "https://sessionserver.mojang.com/session/minecraft/profile/"
                                    + player.getUUID();
                            URL url = new URL(urlString);
                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                            connection.setRequestMethod("GET");
                            connection.addRequestProperty("Content-Type", "application/json");
                            connection.setReadTimeout(15 * 1000);
                            connection.connect();

                            BufferedReader reader = new BufferedReader(
                                    new InputStreamReader(connection.getInputStream()));
                            StringBuilder builder = new StringBuilder();

                            String line = null;
                            while ((line = reader.readLine()) != null) {
                                builder.append(line + "\n");
                            }
                            reader.close();
                            JSONObject obj = new JSONObject(builder.toString());
                            player.setName(obj.getString("name"));
                            JSONObject textures = obj.getJSONArray("properties").getJSONObject(0);
                            byte[] decoded = DatatypeConverter
                                    .parseBase64Binary(textures.get("value").toString());
                            obj = new JSONObject(new String(decoded, "UTF-8"));
                            boolean hasSkin = obj.getJSONObject("textures").has("SKIN");
                            String textureUrl = null;
                            if (hasSkin == true)
                                textureUrl = obj.getJSONObject("textures").getJSONObject("SKIN")
                                        .getString("url");
                            player.setSkinURL(textureUrl);
                        }
                    }

                    players.add(player);
                    System.out.println("Loaded " + player.getName());
                } catch (Exception e) {
                    System.err.println("Couldn't load player info from " + playerFile.getName());
                    System.err.println(
                            "You are only allowed to contact the Mojang session server once per minute per player.  Wait for a minute and try again.");
                    //e.printStackTrace();
                }
            }
        }
    }

    System.out.println("\tloaded " + players.size() + " players");

    return players;
}

From source file:uk.ac.imperial.clahrc.common.BugzillaProxy.java

@SuppressWarnings("unchecked")
private Map<String, Object> submitAttachment(XmlRpcClient XMLRPCClient, int BugId) {

    Map<String, Object> submitAttachmentResult = null;
    Map<String, Object> AttachmentMap = Collections.checkedMap(new HashMap<String, Object>(), String.class,
            Object.class);
    try {//  ww w .j  ava  2s  .co  m
        AttachmentMap.put("ids", new String[] { String.valueOf(BugId) });
        AttachmentMap.put("data", DatatypeConverter
                .parseBase64Binary(Base64.encodeBytes(this.imageAttachment, 0, this.imageAttachment.length)));
        AttachmentMap.put("file_name", "fileAttachmentForBug_" + BugId);
        AttachmentMap.put("summary", "fileAttachment for - " + this.summary);
        AttachmentMap.put("content_type", this.imageAttachmentContentType);

        submitAttachmentResult = ((Map<String, Object>) XMLRPCClient.execute("Bug.add_attachment",
                new Object[] { AttachmentMap }));
        log.info("bugzillaProxy.submitBug() action submitted a bugAttachment with id: "
                + ((Map<String, Object>) ((Map<String, Object>) submitAttachmentResult).get("attachments"))
                        .keySet().toArray()[0]);

    } catch (Throwable twbl) {
        log.error("Throwable thrown while submitting a bug attachment - Problem Report Was Sent!", twbl);
        log.error("Attempted to report: " + Arrays.toString(AttachmentMap.values().toArray()));
    }
    return submitAttachmentResult;
}

From source file:Utility.EncryptionManager.java

/**
 * this method change the message encrypt String to normal String using
 * 64bits AES/*from w  w  w.j a  v  a2  s  . c om*/
 * 
 * if decpytion fail then returnERROR_PACKET(-1)
 * 
 * @param encryptedData
 * @return
 */
public static String decrypt64bits(String encryptedData) {
    Key key;
    Cipher c;
    boolean ischeck = true;

    byte[] decValue = null;
    while (ischeck) {
        key = generateKey64bits();
        try {
            c = Cipher.getInstance(Settings.ALGO);
            c.init(Cipher.DECRYPT_MODE, key);
            byte[] decordedValue = DatatypeConverter.parseBase64Binary(encryptedData);
            decValue = c.doFinal(decordedValue);
            String decryptedValue = new String(decValue);
            return decryptedValue;
        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            ischeck = false;
        } catch (BadPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    return "/&" + Settings._ERROR_PACKET + "/&@";

}