`

Enum singleton - Elvis Stealer

阅读更多
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;

public class ElvisImpersonator {
    // Byte stream could not have come from real Elvis instance!
    private static final byte[] serializedForm = new byte[] { (byte) 0xac,
            (byte) 0xed, 0x00, 0x05, 0x73, 0x72, 0x00, 0x05, 0x45, 0x6c, 0x76,
            0x69, 0x73, (byte) 0x84, (byte) 0xe6, (byte) 0x93, 0x33,
            (byte) 0xc3, (byte) 0xf4, (byte) 0x8b, 0x32, 0x02, 0x00, 0x01,
            0x4c, 0x00, 0x0d, 0x66, 0x61, 0x76, 0x6f, 0x72, 0x69, 0x74, 0x65,
            0x53, 0x6f, 0x6e, 0x67, 0x73, 0x74, 0x00, 0x12, 0x4c, 0x6a, 0x61,
            0x76, 0x61, 0x2f, 0x6c, 0x61, 0x6e, 0x67, 0x2f, 0x4f, 0x62, 0x6a,
            0x65, 0x63, 0x74, 0x3b, 0x78, 0x70, 0x73, 0x72, 0x00, 0x0c, 0x45,
            0x6c, 0x76, 0x69, 0x73, 0x53, 0x74, 0x65, 0x61, 0x6c, 0x65, 0x72,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,
            0x4c, 0x00, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x74,
            0x00, 0x07, 0x4c, 0x45, 0x6c, 0x76, 0x69, 0x73, 0x3b, 0x78, 0x70,
            0x71, 0x00, 0x7e, 0x00, 0x02 };

    public static void main(String[] args) {
        // Initializes ElvisStealer.impersonator and returns
        // the real Elvis (which is Elvis.INSTANCE)
        Elvis elvis = (Elvis) deserialize(serializedForm);
        Elvis impersonator = ElvisStealer.impersonator;
        elvis.printFavorites();
        impersonator.printFavorites();
    }

    // Returns the object with the specified serialized form
    private static Object deserialize(byte[] sf) {
        try {
            InputStream is = new ByteArrayInputStream(sf);
            ObjectInputStream ois = new ObjectInputStream(is);
            return ois.readObject();
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }
}

 

import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.Arrays;

// Broken singleton - has nontransient object reference field!
public class Elvis implements Serializable {
    public static final Elvis INSTANCE = new Elvis();

    private Elvis() {
    }

    private String[] favoriteSongs = { "Hound Dog", "Heartbreak Hotel" };

    public void printFavorites() {
        System.out.println(Arrays.toString(favoriteSongs));
    }

    private Object readResolve() throws ObjectStreamException {
        return INSTANCE;
    }
}

 

import java.io.Serializable;

public class ElvisStealer implements Serializable {
    static Elvis impersonator;
    private Elvis payload;

    private Object readResolve() {
        // Save a reference to the "unresolved" Elvis instance
        impersonator = payload;
        // Return an object of correct type for favorites field
        return new String[] { "A Fool Such as I" };
    }

    private static final long serialVersionUID = 0;
}

 

from effective java 2nd Item 77

it prints out:

[Hound Dog, Heartbreak Hotel]
[A Fool Such as I]

 

and try to figure out why it prints  [A Fool Such as I].

answer: as when it is being deserialized, the Elvis class is chosed to be instantiated, and its non-transient field favoriteSongs has been changed to [A Fool Such as I].

 

yes, the detailed question is why it still prints out: [Hound Dog, Heartbreak Hotel], hasn't the field favoriteSongs been totally replaced by [A Fool Such as I] ? and what did it steal?

 

here's a little bit complementary explanation:

It is often convenient to serialize objects for convenient communication or to save them for later use. However, deserialized data or code can often be modified without using the provided accessor functions if it does not use cryptography to protect itself. Furthermore, any cryptography would still be client-side security - which is of course a dangerous security assumption.

An attempt to serialize and then deserialize a class containing transient fields will result in NULLs where the non-transient data should be. This is an excellent way to prevent time, environment-based, or sensitive variables from being carried over and used improperly.

 

The opposite operation of the serialization is called deserialization i.e. to extract the data from a series of bytes is s known as deserialization which is also called inflating or unmarshalling .

The given program shows how to read any data or contents from the serialized object or file. It takes a file name and then converts into java object. If any exception occurs during reading the serialized file, it is caught in the catch block.

 

In computer science , in the context of data storage and transmission, serialization is the process of converting a data structure or object into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and "resurrected" later in the same or another computer environment.[ 1] When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object. For many complex objects, such as those that make extensive use of references , this process is not straightforward.

This process of serializing an object is also called deflating or marshalling an object.[ 2] The opposite operation, extracting a data structure from a series of bytes, is deserialization (which is also called inflating or unmarshalling ).

 

 

分享到:
评论

相关推荐

    python-enum34-1.0.4-1.el7.noarch.rpm

    离线安装包,亲测可用

    opc enum 32-64安装包

    OPC Enum 32-64 安装包是一款专为处理32位和64位操作系统设计的OPC组件,确保在不同系统环境下能稳定、高效地工作。 OPC Core Component是OPC技术的核心组成部分,它提供了基本的OPC服务,如数据访问(OPC DA,OPC ...

    Java ---- enum --- 枚举类案例

    Java ---- enum --- 枚举类案例

    Python库 | enum34-1.1.3-py2.py3-none-any.whl

    资源分类:Python库 所属语言:Python 使用前提:需要解压 资源全名:enum34-1.1.3-py2.py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    Python库 | aenum-2.0.1-py2-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:aenum-2.0.1-py2-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    单例实现源码singleton-C++

    enum class Singleton { Instance }; class SingletonInstance { public: SingletonInstance() {} // ... }; inline SingletonInstance& getInstance() { return Singleton::Instance; } ``` 这种...

    Python库 | enum34-0.9.1.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:enum34-0.9.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    Python库 | django-enumfields-0.8.0.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:django-enumfields-0.8.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    singleton-demo.zip

    public enum Singleton { INSTANCE; public void whateverMethod() { // ... } } ``` 以上六种单例模式各有优缺点。饿汉式和静态内部类方式线程安全且在类加载时完成初始化,但可能会造成内存浪费。懒汉式和...

    PyPI 官网下载 | cpp-enum-class-string-idl-0.0.1.tar.gz

    标题中的"PyPI 官网下载 | cpp-enum-class-string-idl-0.0.1.tar.gz"表明这是一个从Python Package Index(PyPI)官方源下载的软件包,名为"cpp-enum-class-string-idl",版本号为0.0.1。PyPI是Python社区用来分发和...

    Python库 | named_enum-1.1.0.tar.gz

    `named_enum-1.1.0.tar.gz` 是一个针对Python编程语言的库,名为 `named_enum` 的版本1.1.0的压缩包。这个库主要关注的是枚举(enumerations)的实现,枚举在编程中是一种强大的数据类型,用于定义一组具有命名常量...

    python-enum-0.4.4-9.el7.noarch.rpm

    官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装

    maven-properties-enum-plugin-1.0.1.jar

    maven-properties-enum-plugin-1.0.1.jar

    PyPI 官网下载 | aenum-2.0.10-py3-none-any.whl

    《PyPI上的Python库——aenum-2.0.10-py3-none-any.whl详解》 在Python编程世界中,PyPI(Python Package Index)是开发者们获取和分享软件包的重要平台。今天我们将深入探讨PyPI上的一款名为"aenum"的Python库,其...

    sclo-python27-python-enum34-1.1.6-1.el7.noarch.rpm

    官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装

    PyPI 官网下载 | aenum-1.2.1.tar.gz

    **PyPI 官网下载 | aenum-1.2.1.tar.gz** 在Python编程语言中,`PyPI`(Python Package Index)是官方的软件仓库,它为开发者提供了存储和分享他们创建的Python模块、库和其他软件工具的平台。用户可以方便地通过`...

    PyPI 官网下载 | data_enum-2.0.1-py3-none-any.whl

    "PyPI 官网下载 | data_enum-2.0.1-py3-none-any.whl" 这个标题表明我们正在处理一个从Python Package Index(PyPI)官方源下载的软件包。"data_enum"是这个包的名字,版本号为2.0.1,"py3-none-any"揭示了它与...

    PyPI 官网下载 | aenum-2.0.6-py2-none-any.whl

    资源来自pypi官网。 资源全名:aenum-2.0.6-py2-none-any.whl

Global site tag (gtag.js) - Google Analytics